tabnas-json5 (Rust)
The JSON5 grammar plugin for the
tabnas parsing engine, crate
tabnas_json5.
JSON5 is JSON plus comments (//, /* */),
trailing commas, single-quoted strings, unquoted IdentifierName keys
(with \uXXXX escapes decoded), hex numbers, leading and trailing
decimal points, explicit + signs, Infinity and NaN, and string
line continuations. The plugin layers on the relaxed-JSON base grammar
of tabnas-jsonic and then tightens
it toward the specification: no implicit top-level a:1 or 1,2
forms, no auto-close at the end of the source, no unquoted text at a
value position, and no numeric keys. The default option set is a strict
JSON5 configuration; hash comments, backtick strings and octal, binary
or separator numbers are off and opt-in.
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. All three share one
grammar file, ../json5-grammar.jsonic, and
all three pass the vendored official json5/json5-tests corpus.
Use
Or build an instance and reuse it, parsing through parse_with, the
entry point that applies the requireValue rule:
Configure one with Json5Options; every field is a boolean and the
defaults are strict JSON5:
The plugin form installs on any instance that carries the jsonic grammar, and takes its options as the engine's plugin-option bag, keyed as the TypeScript plugin spells them:
Parse errors are the engine's TabnasError, re-exported as
Json5Error, with code, row, col and a report that shows the
offending source with a caret. json5_empty and json5_no_value are
the two codes this plugin adds; the rest are the engine's.
Install
The engine and the base grammars are on crates.io, the engine as
tabnas-parser, whose library is named tabnas in code:
All three entries are needed. A crate's dependencies are not passed on
to its dependents, so tabnas-json5 alone does not put tabnas or
tabnas_jsonic in your extern prelude, and the examples above that name
tabnas_jsonic::make would not resolve. Only Json5Error is
re-exported.
In this repository the engine and jsonic are taken by path from sibling
checkouts instead, and jsonic takes json the same way, so clone
https://github.com/tabnas/parser, https://github.com/tabnas/json and
https://github.com/tabnas/jsonic next to it. The test suite additionally
needs https://github.com/tabnas/support beside the repository, for the
shared fixture runner. The release workflow swaps those paths for
crates.io versions when it publishes this crate.
Differences from the canonical TypeScript
Every parse result is the TypeScript one: the shared fixtures in
../test/spec and the vendored corpus in
../test/json5-tests hold all three runtimes to
it, both the values and the rejections. What differs is the shape of the
API and a few points where the engine has no way to say what the
TypeScript engine says:
- The entry point is
parse_with, as in Go. TypeScript wraps theparsemethod on the instance to apply therequireValuerule. The Rust engine runs aparser.starthook instead of the parse rather than before it, so the rule lives in the package-levelparse_with(andparse, andparse_reader_withfor a reader), the counterpart of the GoParse(j, src). A directTabnas::parseon the instance parses the same JSON5, string line continuations included, but reports an empty source with the engine's generic code. - Options are a struct.
Json5Optionsholds the nine booleans with Rust names;to_valueandfrom_valuemap them to the TypeScript keys the plugin bag uses. - The
Infinityfamily is a value definition.Infinity,NaNand their signed forms are realf64values in the parse result;Value::to_jsonrenders them asnull, so readValue::Numberdirectly when they matter. - Key order is document order. An
IndexMapkeeps every key where it arrived; a JavaScript object enumerates integer-like keys first. - 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. The fold is executed rather than only described:a_lone_surrogate_folds_to_the_replacement_characterintests/json5_test.rspins"\uD800"to U+FFFD and pins a complete surrogate pair to the single astral character it denotes. The Go port folds the same way, for the same reason: a UTF-8 string cannot hold an unpaired surrogate. It is pinned in a test rather than in the shared divergence register because that register compares JSON cells, and every reader but JavaScript's folds\ud800to U+FFFD, so the row would read as no divergence at all. - Unquoted keys reach the letters this crate's Unicode tables know.
An ECMAScript 5.1
IdentifierStartis a Unicode letter, and the specification names no Unicode version, so each runtime answers from the tables its platform ships. This crate reads them through theregexcrate, which is a version behind a recent Node and a version ahead of Go 1.24, so a handful of recently added letters open a key here and not in Go, and a smaller handful open one in the canonical and not here. The measured table is in../DIVERGENCE.md, andunquoted_keys_follow_this_crates_unicode_tablesintests/json5_test.rsholds this column to it. Nothing in the parse rule differs: the runtimes disagree about which characters are letters, not about what a key may be. - Nesting stops at 127 containers. A deeper source gets the
cancelcode, where the canonical runtime and the Go port accept it. The bound comes from the base grammar, and it guards the caller: the parse loop runs flat, while the value it hands back walks its own nesting to convert and again to drop, one frame per level and both outside this crate.nesting_is_capped_at_the_budget_jsonic_installsintests/json5_test.rspins the boundary.
One difference from the specification is shared by every port and
rooted upstream: a literal control character inside a string literal is
rejected with unprintable, as the TypeScript and Go ports reject it.
U+2028 and U+2029 stay legal inside a string in all three, which is
what the specification asks for, and the shared ../test/spec/strings.tsv
fixture executes that agreement in each of them.
Build and test
The engine, the base grammars 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, through the shared runner with a fresh
parser per row for the opts column; the vendored json5/json5-tests
corpus against its generated oracle, both halves and the derived
truncation and trailing-garbage probes; the divergence register; and
the in-language tests for what a fixture cannot express: the API, the
decoded keys, the error positions, the shared default parser under
threads, and that reusing an instance is what makes a parse cheap.
License
MIT.