3 ms·
On issue 28, it says that when you do JSON.stringify(Object('hi')) The issue reporter says it should return '"hi"' but returns '{"0":"h","1":"i"}' In my J
by firefoxman1 15y ago
On issue 28, it says that when you do
JSON.stringify(Object('hi'))
The issue reporter says it should return '"hi"' but returns '{"0":"h","1":"i"}'
In my JS console, if I do
Object('hi')
It returns { '0': 'h', '1': 'i' }
So to me, it looks like JSON2 correctly serializes the result of Object('hi'). Am I missing something?
https://github.com/douglascrockford/JSON-js/issues/28 https://github.com/douglascrockford/JSON-js/issues/28
- jdd 15y agoMake sure you're not on a page with external scripts that could be mucking it up. If you're recent modern browser `JSON.stringify(Object('hi'))` should produce '"hi"'. See http://cl.ly/2J3B0e1z1o3e0T3x2b0l http://cl.ly/2J3B0e1z1o3e0T3x2b0l.
- firefoxman1 15y agoRight, JSON.stringify(Object('hi')) does that, but just plain Object('hi') without wrapping in stringify returns { '0': 'h', '1': 'i' } Is it part of the JSON spec that you first call .toString() on array-like objects?
- jdd 15y ago`Object('hi')` produces the equivalent of `new String('hi')`. The stringify behavior is spec'ed here http://es5.github.com/#x15.12.3 http://es5.github.com/#x15.12.3 [Step 4b of the abstract operation Str(key, holder) algorithm]. See http://cl.ly/2E1l2X3X3U1B2N1d3G1k http://cl.ly/2E1l2X3X3U1B2N1d3G1k.
- firefoxman1 15y agoAhh that makes sense. I appreciate your research finding that part in the spec.