Repository navigation
Add JSON parsing and stringifying to Rust JSG - #8193
Conversation
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().
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
✅ 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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
# Conflicts: # src/rust/AGENTS.md
There was a problem hiding this comment.
✅ 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
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().