Closed Bug 538301 Opened 15 years ago Closed 14 years ago

Recursive JSON.stringify throws InternalError rather than TypeError

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 578273

People

(Reporter: gibbonjabber, Unassigned)

Details

Attachments

(1 file)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729) Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729) as shown in the script in the test html page. Reproducible: Always Steps to Reproduce: <html><head><title>JSON.stringify array bug</title> <script> test = {a: [{b: 'replaceThisWith_a[0]'}]}; test.a[0].b = test.a[0]; // create recursive structure alert('uneval(test) = ' + uneval(test)); // displays "({a:[#1={b:#1#}]})" alert('JSON = ' + JSON.stringify(test)); // throws "InternalError: too much recursion" error </script> </head> <body> You should see one alert of uneval(test), but you never see the second alert of JSON.stringify(test) because it throws an "InternalError: too much recursion" error. </body></html> I discovered this while running firebug and greasemonkey. I tested the html example in a new incarnation of Firefox with no extensions.
Attached file test .html file
I can reproduce on Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.3a5pre) Gecko/20100411 Minefield/3.7a5pre but didn't check the specification.
Assignee: nobody → general
Component: General → JavaScript Engine
QA Contact: general → general
JSON (the spec) does not support self-referential data structures, period. If you noticed, uneval returned a string that uses a Spidermonkey extension to ECMAScript object literals. JSON serialization can't do that, though, since the result wouldn't be something JSON consumers could understand. Note that in other UAs even uneval fails on this data structure.
Status: UNCONFIRMED → RESOLVED
Closed: 15 years ago
Resolution: --- → INVALID
Well, not quite -- spec requires the mode of failure to be a TypeError, not an Error (or InternalError). I don't think we can actually rely on normal recursion-prevention efforts to implement JSON.stringify; we need something like the stack variable ES5 defines in the stringify algorithm.
Status: RESOLVED → UNCONFIRMED
OS: Windows Vista → All
Hardware: x86 → All
Resolution: INVALID → ---
Summary: JSON.stringify throws "InternalError: too much recursion" for a simple array example with a little recursion that works fine with uneval. → Recursive JSON.stringify throws InternalError rather than TypeError
Status: UNCONFIRMED → NEW
Ever confirmed: true
Status: NEW → RESOLVED
Closed: 15 years ago14 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: