Skip to content

Add JSON parsing and stringifying to Rust JSG - #8193

Merged
bartlomieju merged 5 commits into
mainfrom
biwanczuk/rust-jsg-json
Nov 5, 2026
Merged

bartlomieju merged 5 commits into
mainfrom
biwanczuk/rust-jsg-json

Conversation

@bartlomieju

Copy link
Copy Markdown
Collaborator

jsg::json::parse() and jsg::json::stringify() do what C++ JSG's
jsg::Lock::parseJson() and serializeJson() do: JSON.parse() without a
reviver and JSON.stringify() without a replacer, through v8::JSON, so
a script that replaces the JSON global doesn't affect them.
jsg::json::stringify_with_gap() also indents the result, as the third
argument to JSON.stringify() does.

Invalid JSON leaves a SyntaxError pending. Stringifying runs
JavaScript (getters, toJSON() and Proxy traps), which can throw, as
can a cycle or a BigInt; the exception stays pending. Both return a
pending error then. While an exception or a termination is already
pending, the shims check exceptionPending() and call nothing in V8,
as rule 1 requires. A value JSON can't represent, which
JSON.stringify() returns undefined for, gives the string "undefined",
since v8::JSON::Stringify() converts its result to a string. C++ JSG
returns the same, so Rust JSG matches it, and the docs say so.

Rust creates the V8 strings for the text and the gap, so a text or a
gap longer than v8::String::MAX_LENGTH fails with
Error::invalid_string_length(), as converting a &str does, and the
shims, value_json_parse and local_json_stringify, take handles and
call V8 only. The conversion of a V8 string to a rust::String moves
out of unwrap_string into a helper that both use.

Tests check parsing against JSON.parse() (including non-ASCII text, a
lone surrogate and an own proto), SyntaxErrors for invalid JSON,
stringifying against JSON.stringify() (escapes, lone surrogates,
toJSON(), dropped members), the "undefined" results, exceptions from
a getter, toJSON(), a Proxy trap, a cycle and a BigInt, indentation
compared with JSON.stringify(), including a gap longer than 10
characters, a replaced JSON global, and that nothing runs while
execution is terminating. pending_exception.rs checks that parsing
and stringifying fail with a pending error while an exception is
pending, and that stringifying then runs no toJSON().

jsg::json::parse() and jsg::json::stringify() do what C++ JSG's
jsg::Lock::parseJson() and serializeJson() do: JSON.parse() without a
reviver and JSON.stringify() without a replacer, through v8::JSON, so
a script that replaces the JSON global doesn't affect them.
jsg::json::stringify_with_gap() also indents the result, as the third
argument to JSON.stringify() does.

Invalid JSON leaves a SyntaxError pending. Stringifying runs
JavaScript (getters, toJSON() and Proxy traps), which can throw, as
can a cycle or a BigInt; the exception stays pending. Both return a
pending error then. While an exception or a termination is already
pending, the shims check exceptionPending() and call nothing in V8,
as rule 1 requires. A value JSON can't represent, which
JSON.stringify() returns undefined for, gives the string "undefined",
since v8::JSON::Stringify() converts its result to a string. C++ JSG
returns the same, so Rust JSG matches it, and the docs say so.

Rust creates the V8 strings for the text and the gap, so a text or a
gap longer than v8::String::MAX_LENGTH fails with
Error::invalid_string_length(), as converting a &str does, and the
shims, value_json_parse and local_json_stringify, take handles and
call V8 only. The conversion of a V8 string to a rust::String moves
out of unwrap_string into a helper that both use.

Tests check parsing against JSON.parse() (including non-ASCII text, a
lone surrogate and an own __proto__), SyntaxErrors for invalid JSON,
stringifying against JSON.stringify() (escapes, lone surrogates,
toJSON(), dropped members), the "undefined" results, exceptions from
a getter, toJSON(), a Proxy trap, a cycle and a BigInt, indentation
compared with JSON.stringify(), including a gap longer than 10
characters, a replaced JSON global, and that nothing runs while
execution is terminating. pending_exception.rs checks that parsing
and stringifying fail with a pending error while an exception is
pending, and that stringifying then runs no toJSON().
@bartlomieju
bartlomieju requested review from a team as code owners November 3, 2026 19:32

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of f48cb823.

This review, of 2b080936

✅ Approved — No issues found: the JSON shims respect the pending-exception rule, the string-conversion refactor keeps behaviour the same, and the tests are thorough.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 2b080936

@codecov-commenter

codecov-commenter commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 13.11475% with 53 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.84%. Comparing base (348bcf4) to head (03971b9).

Files with missing lines Patch % Lines
src/rust/jsg/json.rs 0.00% 36 Missing ⚠️
src/rust/jsg/ffi.c++ 33.33% 16 Missing ⚠️
src/rust/jsg/v8.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8193      +/-   ##
==========================================
- Coverage   68.86%   68.84%   -0.02%     
==========================================
  Files         746      747       +1     
  Lines      195095   195150      +55     
  Branches    30289    30292       +3     
==========================================
  Hits       134357   134357              
- Misses      42965    43018      +53     
- Partials    17773    17775       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found: the JSON shims respect the pending-exception rule, the string-conversion refactor keeps behaviour the same, and the tests are thorough.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: f48cb823 — same changes as 2b080936, so this is that commit's review (no new review was run; line numbers are as of 2b080936)

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 1cf56202.

This review, of fc61057d

✅ Approved — No issues found: the JSON shims respect the pending-exception rule, the string-conversion refactor keeps behaviour the same, and the tests are thorough.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: fc61057d — same changes as 2b080936, so this is that commit's review (no new review was run; line numbers are as of 2b080936)

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 03971b9f.

This review, of 1cf56202

✅ Approved — No issues found: the JSON shims respect the pending-exception rule, the string-conversion refactor keeps behaviour the same, and the tests are thorough.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 1cf56202 — same changes as 2b080936, so this is that commit's review (no new review was run; line numbers are as of 2b080936)

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found. The only change since the last clean review is a merge resolution in src/rust/AGENTS.md that correctly lists the new json module.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues. Rust creates the input and gap strings, so overlong strings fail with invalid_string_length, and nothing calls into V8 while an exception or a termination is pending.


📖 AGENTS.md: ✅ LGTM

The src/rust/AGENTS.md crate table now lists the json module, and the entry is accurate.


📋 Code Quality: ✅ LGTM

The JSON shims respect the pending-exception rule, and the string-conversion helper moved out of unwrap_string behaves the same as before. The tests are thorough.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The README section matches the behaviour, including the "undefined" result for values JSON can't represent.


Reviewed commit: 03971b9f

@bartlomieju
bartlomieju merged commit 4417666 into main Nov 5, 2026
23 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/rust-jsg-json branch November 5, 2026 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants