Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JSON.parse() problem

206 views
Skip to first unread message

Christian Grigis

unread,
Dec 31, 2009, 11:56:07 AM12/31/09
to
Hello,

I am doing some tests with the CVS version of Rhino 1.7R3, and I am
running into an issue with the JSON native object.

The problem involves the object returned when parsing a dictionary, if
the keys happen to be (string representations of) numbers. More
specifically:

js> var o1 = {"a": 100}
js> var o2 = {"1": 100}
js> var s1 = JSON.stringify(o1)
js> var s2 = JSON.stringify(o2)
js> s1
{"a":100}
js> s2
{"1":100}

So far, so good.

js> var n1 = JSON.parse(s1)
js> var n2 = JSON.parse(s2)
js> n1["a"]
100
js> n2["1"]
js> typeof(n2["1"])
undefined

The value with key "1" on n2 is strangely undefined. The attribute is
there though:

js> for (var k in n2) print(k, typeof(k), n2[k])
1 string undefined

And the value is hidden there somewhere as well, because:

js> JSON.stringify(n1)
{"a":100}
js> JSON.stringify(n2)
{"1":100}

Summarized, the two following statements do not return the same thing:

js> JSON.parse(JSON.stringify({"a": 100}))["a"]
100
js> JSON.parse(JSON.stringify({"1": 100}))["1"]
js>

As a comparison, the same thing work as expected with Prototype 1.6.1
JSON routines:

js> Object.toJSON({"a": 100}).evalJSON()["a"]
100
js> Object.toJSON({"1": 100}).evalJSON()["1"]
100

Is this a known problem? Am I missing something?

Thank you, best regards, and Happy New Year!

-Christian
--
Christian Grigis
Senior Software Engineer
NEXThink S.A. -- http://www.nexthink.com/

Rapha

unread,
Jan 1, 2010, 9:33:45 PM1/1/10
to
Hi Christian,

I've filed a bug and submitted a patch for that.

https://bugzilla.mozilla.org/show_bug.cgi?id=537483

Cheers,
Raphael

On Jan 1, 3:56 am, Christian Grigis <christian.gri...@nexthink.com>
wrote:

Norris Boyd

unread,
Jan 2, 2010, 9:24:25 PM1/2/10
to
I've committed the patch. Thanks, Raphael, for fixing, and thanks,
Christian for reporting.

--N

Christian Grigis

unread,
Jan 4, 2010, 8:32:17 AM1/4/10
to
Works great.
Thank you Raphael and Norris for the very prompt response!

-Christian

0 new messages