Repository navigation
feat: JSON5 numbers, string escapes and property names - #89
Merged
Merged
Conversation
… NaN Numbers like `.5`, `5.` and `5.e3` now parse, and so do `Infinity`, `-Infinity`, `+Infinity`, `NaN` and `-NaN`. Two new `ParseOptions` switches gate them: `allow_bare_decimal_point_numbers` and `allow_non_finite_numbers`. Both default to `true`, like the other JSON5 switches. The `serde_json` conversion turns `.5` into `0.5` and `5.` into the float `5.0`. `serde_json` cannot hold `Infinity` or `NaN`, so the deserializer reports them as out of range, and the AST and CST conversions keep their fallback to a string. A property name like `Infinity_count` or `NaN-key` still scans as one word, and `[true-1]` still parses as `[true, -1]` when commas can be missing.
Strings now accept the JSON5 escapes: `\'` in a double-quoted string, `\"` in a single-quoted string, `\v`, `\0`, `\xHH`, a backslash before a line terminator (a line continuation), and a backslash before any other character except a digit, which gives that character. A new `ParseOptions` switch, `allow_extended_string_escapes`, gates them and defaults to `true`. With it off, the old errors for `\'` and `\"` stay. A line comment now also ends at a lone `\r`, which is a line terminator in JSON5 and in JavaScript, and error messages count it as a line break. U+2028 and U+2029 do not end a line comment, so this crate keeps agreeing with other JSONC parsers. The CST keeps a lone `\r` as whitespace, so the text round-trips unchanged.
JSON5 property names follow ECMAScript identifiers. So `$` can appear
anywhere, and the reserved words `true`, `false` and `null` are valid
names: `{ $id: 1 }` and `{ true: 1 }` now parse, in both the AST and
the serde parsers. `allow_loose_object_property_names` already gates
unquoted names, so this adds no new switch. A name that starts with a
keyword, like `true$` or `NaN$`, now scans as one word.
A lone `\r` now ends a line comment, but the CST stored it as whitespace, so edits like `append` and sorting treated the comment as still open and could place a new value inside the comment.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No unresolved review issues remain, and the review indicates approval readiness.
Review effort: Lite
Findings: None
What changed in this PR
Adds configurable JSON5 number formats, extended string escapes, loose property names, and CR line-comment handling across parsing and conversion layers.
Changes:
- Supports bare decimal and non-finite numbers.
- Adds extended string escapes and loose object keys.
- Updates scanner, parser, AST, serde, CST, and error handling.
| File | Description |
|---|---|
src/tokens.rs |
Recognizes loose property-name tokens. |
src/string.rs |
Decodes extended JSON5 escapes. |
src/serde.rs |
Supports serde conversions and tests. |
src/scanner.rs |
Scans new JSON5 syntax and CR comments. |
src/parser.rs |
Handles expanded object-key syntax. |
src/parse_to_value.rs |
Adds value parsing coverage. |
src/parse_to_ast.rs |
Propagates options and builds AST nodes. |
src/lib.rs |
Updates configuration documentation. |
src/errors.rs |
Adds error kinds and CR-aware positions. |
src/cst/mod.rs |
Preserves formatting and converts new literals. |
src/common.rs |
Normalizes bare decimal numbers. |
src/ast.rs |
Converts JSON5 numbers to serde values. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
3 tasks done
2 tasks done
kingsleydon
added a commit
to Dstack-TEE/private-ai-gateway
that referenced
this pull request
Oct 16, 2026
Explicitly disable the three new JSON5 parsing options for strict JSON and JSONC, matching upstream's strict-mode example. Keep the JSON5 defaults and the existing CST editing path. Cover rejection of the new syntax in JSONC and byte-preserving JSON5 edits with extended numbers, escapes, comments and CR line endings. Update one refusal transcript: unquoted $include is now valid JSON5, so OpenClaw reports its existing unsupported-include conflict instead of a syntax error. The configuration is still refused without edits. Upstream: dprint/jsonc-parser#89
kingsleydon
added a commit
to Dstack-TEE/private-ai-gateway
that referenced
this pull request
Oct 16, 2026
…#408) * chore(deps): bump jsonc-parser from 0.33.2 to 0.34.0 in /apps/desktop Bumps [jsonc-parser](https://github.com/dprint/jsonc-parser) from 0.33.2 to 0.34.0. - [Commits](dprint/jsonc-parser@0.33.2...0.34.0) --- updated-dependencies: - dependency-name: jsonc-parser dependency-version: 0.34.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * fix(desktop): adapt agent-bridge to jsonc-parser 0.34 Explicitly disable the three new JSON5 parsing options for strict JSON and JSONC, matching upstream's strict-mode example. Keep the JSON5 defaults and the existing CST editing path. Cover rejection of the new syntax in JSONC and byte-preserving JSON5 edits with extended numbers, escapes, comments and CR line endings. Update one refusal transcript: unquoted $include is now valid JSON5, so OpenClaw reports its existing unsupported-include conflict instead of a syntax error. The configuration is still refused without edits. Upstream: dprint/jsonc-parser#89 --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kingsley <[email protected]>
zkochan
added a commit
to pnpm/pnpm
that referenced
this pull request
Nov 20, 2026
…CST (#16339) <!-- A maintainer will only start reviewing once CodeRabbit has approved and CI is green. Please wait for those to pass before expecting human review. --> ## Summary When pnpm saves a `package.json5`, it now edits the file in place. Only the values that changed are rewritten, so comments, quotes, number spellings like `0x10`, and layout elsewhere stay as they were. New keys and strings follow the quoting of the file, so bare keys stay bare and single quotes stay single. Until now a save re-serialized the whole manifest and then put the comments back by matching lines. The squash body of #15260 calls out that exact formatting isn't preserved after an edit, and #15731 got the style close. This goes the rest of the way by editing the concrete syntax tree from `jsonc-parser`, which is the crate dprint uses. Its 0.34.0 release parses JSON5 numbers, escapes, and property names (dprint/jsonc-parser#89, which I wrote with this change in mind). Reading is unchanged. The value still comes from the `json5` crate, with its depth limit. The CST is only used to write, and the result is parsed again with the `json5` crate and compared to the value being saved. If the CST can't parse the file, or the edited text doesn't decode to that value, the save falls back to the current writer. One known case is a bare key that starts with `true_`, `false_`, or `null_`, like `{null_value: 1}`, which jsonc-parser still rejects. Keys like `nullable` or `trueish` edit in place. This adds `jsonc-parser` as a new workspace dependency. The `json5` crate maps text to a value and a value back to new text, so anything that isn't part of the value, like comments and quote style, is gone before a write. jsonc-parser keeps every token of the file in a tree that can be edited and printed back, and it reads JSON5 since 0.34.0. I asked for it on purpose, per the dependency rule in `pnpm/AGENTS.md`. It's MIT licensed and has no dependencies with the `cst` feature alone. Two behavior changes to be aware of. Existing keys keep the order they have in the file, like `package.yaml` saves already do, so a save no longer re-sorts a dependency map that's out of order on disk. A new key goes right after the key before it in sorted order. Also, when a key is added to an object written on one line, jsonc-parser makes that object multiline. The old writer rewrote the whole file in that case too. Related to #15849, which bumps `jsr.jsonc` by rewriting the version literal. Both touch `serialization.rs`. ## Squash Commit Body ```text Saving a package.json5 now edits the file through the jsonc-parser CST instead of re-serializing it and restoring comments by line. Only the values that differ from the file on disk are rewritten. Comments, quote characters, number spellings, and layout elsewhere are kept. The sync walks the value on disk and the value to write together: - Objects: changed values recurse, removed keys are deleted, and a new key is inserted after the key that precedes it in the target, so sorted dependency maps stay sorted. - Arrays: shared positions recurse, extra elements are removed from the end, and new elements are appended. - Existing keys keep their order on disk, as package.yaml already does, so an unsorted dependency map is no longer re-sorted on save. - An object or array that empties or gets its first entry is edited in place too, so comments inside it are kept. - A changed string keeps its own quote character. - Anything else is replaced with the new value. jsonc-parser writes new strings and keys in double quotes. The sync requotes them to the quote most strings in the file use, single on a tie, and writes identifier keys bare when the file has any bare key. Bare names are written last, because decoding a bare string literal panics inside jsonc-parser and object lookups decode every name. The json5 crate still reads every manifest. It parses the file before the CST does, which also keeps the CST parser, which recurses without a limit, away from deeply nested input. The edited text is parsed again with the json5 crate and compared with the value being saved. If the CST cannot parse the file, or the edit does not decode to that value, the save uses the previous writer, so no file gets worse. jsonc-parser 0.34.0 is the first release that parses JSON5 numbers, escapes, and property names (dprint/jsonc-parser#89). It is added to the workspace with only the cst feature. ``` ## Checklist - [x] New features are implemented only in the Rust pnpm v12 CLI. - [x] Added a changeset. It targets `pacquet` with a minor bump. - [x] Added or updated tests. Unit tests for the sync in `pnpm/crates/package-manifest/src/json5/sync/tests.rs`, a fallback test in `pnpm/crates/package-manifest/src/tests/json5.rs`, and the CLI tests in `pnpm/crates/cli/tests/suite/package_json5.rs` now assert the whole file a save writes. --- Made with agent tooling: - Claude Code 2.1.283 (claude-opus-5-5) - Reviewed with Codex CLI 0.153.4 (gpt-6-astra) - Reviewed with Grok CLI 1.0.41 (grok-4.6-build) <!-- codesmith:footer --> --- <a href="https://app.blacksmith.sh/pnpm/codesmith/pnpm/pr/16339?autoLogin=true&ref=codesmith_pr_footer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v2.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-light-v2.svg"><img alt="View with [code]smith" src="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v2.svg"></picture></a> <a href="https://backend.blacksmith.sh/track/enable-autofix?expires=1793196780&installation_model_id=426870&pr_number=16339&ref=codesmith_pr_footer&repository=pnpm%2Fpnpm&return_to=https%3A%2F%2Fgithub.com%2Fpnpm%2Fpnpm%2Fpull%2F16339&signature=a35da8441ccf5ae07493453ec093cafb51ce68c49db9382dbd994c22f6fcf8a3"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img alt="Autofix with [code]smith" src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"></picture></a> <sup>Need help on this PR? Tag <code>@codesmith-bot</code> with what you need. Autofix is disabled.</sup> <!-- codesmith:autofix:disabled --> <!-- /codesmith:footer --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * JSON5 package manifests retain existing formatting, comments, quote styles, and key order when values are edited. * New keys and strings follow the file’s existing style where possible. Updates can add or remove object fields and adjust array contents. * When changes cannot be safely applied to the existing text, the manifest uses the standard serialization fallback, while retaining comments where possible. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Zoltan Kochan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds JSON5 syntax that the parser rejects today. After it, every valid file in the json5/json5-tests corpus parses, except
todo/unicode-escaped-unquoted-key.json5, which the corpus itself files undertodo/.true.5,-.5,5.,5.e3allow_bare_decimal_point_numbers(new)Infinity,-Infinity,NaNallow_non_finite_numbers(new)"it\'s",'\x41','\v','\0',\then a line breakallow_extended_string_escapes(new)$and keywords in unquoted property names{ $id: 1 },{ true: 1 }allow_loose_object_property_names(existing)\rends a line comment// note\r1Each commit is one area and passes the tests alone.
Behavior notes
serde_jsoncannot holdInfinityorNaN. The serde deserializer reports them as "Number is out of range", the same as1e400. The AST and CST conversions use the existing fallback to a string.serde_jsonconversion,.5gives0.5, and5.gives the float5.0. The AST and the CST keep the raw text.\ris a line terminator in JSON5 and in JavaScript, and Microsoft jsonc-parser also ends a comment there. Error positions now count it as a line break. The CST stores it as whitespace, so the text round-trips.[true-1]still parses as[true, -1]when commas can be missing.Infinity_countandNaN-keystill scan as one word.true$now also scans as one word, where 0.33.2 gave an error.allow_extended_string_escapes: false, the old errors for\'in a double-quoted string and\"in a single-quoted string stay.Breaking change
ParseOptionsandScannerOptionsget three new pub fields, so an exhaustive struct literal no longer compiles.ParseErrorKindgetsBareDecimalPointNumbersNotAllowedandNonFiniteNumbersNotAllowed.ParseStringErrorKindgetsExpectedTwoHexDigits.This is the same kind of change as #61 (0.28.0) and #63 (0.29.0), so it needs a 0.x minor release. To check the semver breaks:
Downstream: dprint-plugin-json
dprint-plugin-json 0.24.0 uses
ParseOptions::default(), andgen_string_litrewrites string text without the new escapes. When it moves to this release, it would change string values:'say \"hi\"'gives output that does not parse. The dprint-plugin-json test suite still passes against this branch, so its tests do not catch this.There are two fixes:
gen_string_litthe new escapes. This does not need the new switch, so it can merge before this release.allow_extended_string_escapes: false. This needs this release.I can send a PR for option 1.
Testing
cargo testwith--features serde,--features preserve_order,--all-featuresand--release --all-features, on each commit.CstRootNode::parse. Each parsed file round-trips byte for byte.json5crate.false, these still apply:$and keyword property names\rafter a line comment