tabnas-jsonic (Rust)
The jsonic relaxed-JSON grammar plugin for the
tabnas parsing engine, crate
tabnas_jsonic.
jsonic accepts standard JSON and then relaxes it for humans: unquoted
keys (a:1), implicit objects and arrays (a:1,b:2, x,y,z),
comments, trailing commas, single and backtick quoted strings, multiline
strings, and path diving (a:b:1 is {"a":{"b":1}}). The standard-JSON
core (val / map / list / pair / elem) comes from the
tabnas-json plugin; this crate
installs that core and weaves the relaxed alternates and lifecycle
actions around it.
This is the Rust port of the canonical TypeScript implementation in
../ts; the TypeScript version is authoritative and this crate
tracks it. The Go port is in ../go.
Use
Or build an instance and reuse it:
Configure one with a closure over the engine's typed options; the
grammar is installed against the finished options, so an option that
adds or removes an alternate (map.child, list.child, list.pair)
takes effect:
The strict variant, Jsonic.make('json') in TypeScript, is
make_json(): standard JSON only, with the lexer tightened as well as
the grammar filtered.
To layer another grammar on the jsonic core, install the plugin on your
own instance, or through use_plugin so a derived instance rebuilds it:
Parse errors are the engine's TabnasError, re-exported as
JsonicError, with code, row, col and a report that shows the
offending source with a caret under the [jsonic/<code>] tag.
Install
Neither the engine nor the JSON core is published to a registry, so both
are consumed as sibling checkouts, the standard tabnas development
model. Clone https://github.com/tabnas/parser and
https://github.com/tabnas/json next to this repository and point at
them:
[]
= { = "../jsonic/rs" }
= { = "tabnas-parser", = "../parser/rs" }
Both entries are needed. A crate's dependencies are not passed on to its
dependents, so tabnas-jsonic alone does not put tabnas in your extern
prelude, and the examples above that name tabnas::Tabnas would not
resolve. Only JsonicError is re-exported. The test suite additionally
needs https://github.com/tabnas/support beside the repository, for the
shared fixture runner.
Differences from the canonical TypeScript
Every relaxation and every parse result is the TypeScript one; the
shared fixtures in ../test/spec hold all three
runtimes to it. What differs is the shape of the API and a few points
where the host language has no way to say what JavaScript says:
- Configuration is a closure, not an options object.
make_withmutates the engine's typedOptions; the callableJsonicfacade and its string-form options have no Rust counterpart, andmake_json()stands in forJsonic.make('json'). - Metadata is typed. Under
info.map,info.listandinfo.textthe result carriesMapRef,ListRefandTextwrappers, as the Go port does, in place of the non-enumerable marker property. They serialize as the plain value. - A list's
child$rides on aListRef. A JavaScript array can take a property; aVeccannot, so withlist.childon, a list that received a bare:valuebecomes aListRefwhosechildholds it. - A pair inside a list leaves no trace. With
list.property, TypeScript stores[1,2,a:3]as an array with a propertya, whichJSON.stringifydrops. Here the pair is parsed and discarded, as in Go, so the serialized value agrees. - Key order is document order. An
IndexMapkeeps every key where it arrived, the behaviour TypeScript reaches only withmap.ordered. The option is accepted and changes nothing. - Strictness is a
checkhook. The strict number grammar is a negative lookahead in TypeScript, and theregexcrate has no lookaround, somake_jsonbinds the positive pattern plus an inversion, the shape the Go port uses. - Nesting past 127 levels is rejected with the error code
cancel, counting lists, maps and the implicit maps of a pair dive alike. The engine walks a value with the call stack to display, convert, or drop it, and a source a few thousand levels deep ended the process; the TypeScript and Go ports have no limit. The number is the onetabnas-jsonandserde_jsonuse. empty()keeps the five rules with no alternates, as the GoEmptydoes, so a parse on it fails withunexpectedwhereJsonic.empty()in TypeScript, which has no rules at all, returnsundefined.- Lone surrogates fold to U+FFFD, and the regular expression dialect
is the
regexcrate's. Both come from the engine, and both are recorded there.
Build and test
The engine, the JSON core and the fixture runner are path dependencies on sibling checkouts, so there is nothing to fetch:
Or, from the repository root, make test-rs. For what CI would say,
including formatting and the lockfile check, run ci/rust/run.sh.
The suite runs every shared ../test/spec/*.tsv fixture, the same files
the TypeScript and Go suites run: the standard-shaped ones through the
shared runner, the token-stream corpus, the utility fixtures, the
divergence register, and a tripwire that fails when a fixture is run by
nobody. Beside them are the in-language tests for what a fixture cannot
express: plugin layering, the typed metadata wrappers, key order, safe
keys, comment suffixes, rule selectors, strict mode, the shared default
parser under threads, and that parse reuses its instance.
License
MIT.