Live data from Hacker News

Why is JSON so popular? Developers want out of the syntax business

blog.mongolab.com

131–133 of 133 posts

Re: Why is JSON so popular? Developers want out of the syntax business

#131
post #129
post #126

Earlier quoted context omitted.

The moment you use eval to parse "JSON" data you _are_ trusting content from the client. eval _executes_ javascript, JSON just happens to be mostly compatible with JS object and array literal syntax so it "Just Works". Because eval is executing the data it is using the full JS parser. That means that while '{"name":"bill"}' works as expected '{"name": window.location = "myevildownload.com"}' does too. JSON.parse is b…

> The moment you use eval to parse "JSON" data you _are_ trusting content from the client. eval _executes_ javascript, JSON just happens to be mostly compatible with JS object and array literal syntax so it "Just Works". not nesseserily , this attack could be easily mitigated if supposed JSON string is first parsed and validated on server. and only then send back to eval() on browser. so it is therefore not inherentl…

Your server side validation would have to be a full JSON parser. So in order to use eval, you're adding a full server side parse of the data on each request, increasing server load, and request latency (i've seen sites sending megs of json to the browser).

All so that you can save 6 characters of typing to load the JSON less efficiently on the client side.

Of course because people _do_ do this most engines these days preflight calls to eval to see if they can be parsed as a subset of pseude-JSON. Note: this doesn't make it safe, any inject xss is not valid json so will still be a hole, and these preparsers try to bail out quickly so treat only a minimal subset of JSON. In JavaScriptCore (so all webkit browsers other than chrome) you can't have escaped characters in string literals nor any non-ascii characters anywhere.

Re: Why is JSON so popular? Developers want out of the syntax business

#132
XML was a solution to a problem that didn't exist.

Actually, no, I take that back. XML was a solution that was created for a problem that was devised to justify the work. It's insanity for the sake of itself.

JSON is simply a serialized object format for a popular programming language - that happened to fit the general case very well - yet it's also very much closer to S-expressions, which seem to be the most efficient way to model data in the general case.

It makes me wonder how people manage to convince themselves that a format like XML is somehow a "good idea."

Re: Why is JSON so popular? Developers want out of the syntax business

#133
post #128
post #113

Earlier quoted context omitted.

If you think XML is writable, you are a bigger man than me.

You need to understand the initial use case for XML. It was invented for document-oriented markup languages like HTML, MathML, Docbook etc. You can definitely write XHTML by hand, and a JSON-based syntax for the same kind of documents (which mixed content and so on) would be a lot harder to read and write.

My understanding is that XML was derived from document-oriented SGML, to beat SGML into a form that would work well with XSL and XPath.

But I'd like to point out that the way SGML-derived markup distinguishes attributes and child nodes is entirely arbitrary. You could as easily make attributes child nodes - it's all in how you interpret what's written. Likewise, you can "convert" SGML to JSON (or YAML or S-expr or whatever) very easily, bearing in mind that attributes and child nodes sit in the same space with each other - a well-formed XHTML document, for example, can be re-expressed in JSON without ambiguity, since tags have a well-specified, unambiguous list of allowed children and attributes - just give text nodes the name "text" and you're golden.

Post reply on HN