Files
rust_browser/tests/external/js262/fixtures/json-parse-basic.js
Zachary D. Rowitsch 0c21feb090
Some checks failed
ci / fast (linux) (push) Failing after 11m14s
Implement JSON.stringify and JSON.parse with full spec compliance
Add the JSON global object with both methods following the sentinel
NativeFunction + interpreter interception pattern. The implementation
includes a dedicated recursive descent JSON parser (strict spec: no
trailing commas, no single quotes, no hex/octal, surrogate pair support),
JSON.stringify with replacer function/array, space indentation, toJSON,
boxed primitive unwrapping, and circular reference detection via ptr_eq.

Safety: depth-limited to 512 levels for both parse and stringify to
prevent stack overflow from malicious input. Space string truncation
uses char boundaries to avoid panics on multi-byte UTF-8.

138 unit tests, 2 conformance tests (js262).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 17:00:47 -05:00

22 lines
606 B
JavaScript

// JSON.parse basic tests
console.log(JSON.parse("null") === null);
console.log(JSON.parse("true"));
console.log(JSON.parse("false"));
console.log(JSON.parse("42"));
console.log(JSON.parse("-2.75"));
console.log(JSON.parse('"hello"'));
var obj = JSON.parse('{"a": 1, "b": "two"}');
console.log(obj.a);
console.log(obj.b);
var arr = JSON.parse("[1, 2, 3]");
console.log(arr.length);
console.log(arr[0]);
console.log(arr[2]);
// Roundtrip
var orig = {x: 1, y: [true, null, "z"]};
var rt = JSON.parse(JSON.stringify(orig));
console.log(rt.x);
console.log(rt.y[0]);
console.log(rt.y[1]);
console.log(rt.y[2]);