tags:

views:

48

answers:

5

per the debate in this post: json-conversion-in-javascript

+2  A: 

Per the JSON RFC, an array is indeed legal top-level JSON-text: "A JSON text is a serialized object or array."

Dustin Getz
A: 

Yes.

Is there really a need to debate this? Any JS data structure is valid JSON.

Sorpigal
Not exactly true. functions are data types in javascript, yet they are not allowed by the JSON spec.
David
This is wrong. JSON is a quite limited subset of JavaScript.
Matthew Flaschen
I knew someone would quibble about functions. It's true.
Sorpigal
@Sorpigal, that's not the only exception. You can't directly represent dates or regex literals either. So "any" is definitely wrong.
Matthew Flaschen
{a='b'} fails twice--must quote the name, and must use double quotes. this is actually relevant because boost's JSON-parsing functionality fails to parse both of these cases.
Dustin Getz
A: 

yes, try it out here.

http://www.jsonlint.com/

and put in [{}]

hvgotcodes
It's even easier than that. Put in `[]` and it will validate.
Sorpigal
@sorpigal true dat
hvgotcodes
A: 

This is from the ECMAScript specification.

JSONText :
    JSONValue

JSONValue :
    JSONNullLiteral 
    JSONBooleanLiteral 
    JSONObject 
    JSONArray 
    JSONString 
    JSONNumber
ChaosPandion
This is a little misleading, though, because ECMAScript allows you to parse JSON strings that are not top-level texts. According to the RFC, "A JSON text is a serialized object or array."
Matthew Flaschen
@Matthew - Weird, I wonder how Crockford feels about that. How will they reconcile the differences between the RFC and ECMA?
ChaosPandion
@Chaos, I just looked, and found they're aware of the difference. From ECMAScript 5 §15.12, "The top level JSONText production of the ECMAScript JSON grammar may consist of any JSONValue rather than being restricted to being a JSONObject or a JSONArray as specified by RFC 4627." I don't know if IETF will change the RFC.
Matthew Flaschen
+1  A: 

Yes, but you should consider making the root an object instead in some scenarios, due to JSON hijacking. This is an information disclosure vulnerability based on overriding the array constructor in JavaScript.

Matthew Flaschen
awesome article
Dustin Getz