4 ms·
Despite the name, JSON is not isomorphic to the definition of JSON object literals. It is by design a subset. This provides both future-proofing against later c
by jerf 18y ago
Despite the name, JSON is not isomorphic to the definition of JSON object literals. It is by design a subset. This provides both future-proofing against later changes in ECMAScript (adding keywords), and simplifies the grammar for conforming parsers. Simpler grammars mean less opportunity for errors and non-conformant implementations.
"A hack around would be to regexp a replace to add quotes, and then JSON.parse it,"
That is an absolutely terrible, terrible, terrible idea. Do not do that, especially if you're shipping around a "few billion JSON objects a day"! Running regexps over things that require a grammar to fully understand is asking for disaster.
"IMHO, JSON.parse should function exactly as eval does for de-serializing js objects."
The entire purpose is to get away from "eval". How do you like '{"test": window.close()}" in your JSON?
JSON is a cross-language serialization format that was inspired heavily by the fact that Perl, Python, and Javascript had very similar object literals and underlying data structures. It is not simply Javascript in a fancy spec, and you're missing the point if you think it is or treat it like it is. If you're just moving JS around, then ship whatever you like down. Back in the days before anybody had heard of JSON, I used to do that; as long as you're in control of the JS it works fine. (It's useless for communication between different entities, though.)
- axod 18y agoPlease explain why the data format needs quotes around keys. And yes, I'm just sending data from server to client. I couldn't care less if it conforms to some spec or not. I want an efficient, useful data format. >> The entire purpose is to get away from "eval". How do you like '{"test": window.close()}" in your JSON? You're absolutely correct. but that is simple to protect against with a regexp, which is how the json parser at json.org works - regexp it, then eval(). My point is, that for my use case, the native JSON parser will mean an increase in bandwidth. Which is a pain - especially as there isn't a good reason for it.
- tlrobinson 18y agoHe did explain why: "This provides both future-proofing against later changes in ECMAScript (adding keywords), and simplifies the grammar for conforming parsers. Simpler grammars mean less opportunity for errors and non-conformant implementations." If it didn't require quotes, currently valid JSON may not be valid in the future if a new keyword which was use as a JSON object key was used unquoted. And there's the added complexity of supporting both quoted and unquoted keys.
- axod 18y agoA data format should NOT be concerned with language keywords.
- jsonscripter 18y agoUnless the data format is written in a language...