JSON: The JavaScript subset that isn't
timelessrepo.com
timelessrepo.com
Plus sensationalistic title and pedantic introduction.
> Plus sensationalistic title and pedantic introduction.
This, not so useful (and unkind)...
Had the author written merely the tl;dr line above do you think his important observation would have gotten the attention it deserves?
The author outlined his thought process to show that his observation was actually based on non-trivial research.
While I like tl;dr's, I appreciate intros that put things in context, and I'm more likely to trust observations whose underpinnings are spelled out.
So really, the title should be: “JSON: The ES3 subset that isn’t.”
Issue reported: https://github.com/judofyr/timeless/issues/57
> {"JSON":"rocks!"}
SyntaxError: Parse errorActually, the "good example" should be : var obj = {"JSON":"ro cks!"}
Meta: Right after I originally wrote this comment I saw that tomstuart answered too, so I decided to delete my comment. Well, it appears that so did Tom, and suddenly there were no replies here.
> {"JSON":"rocks!"}
I don't think this is such a good point. While the example is not a valid expression, it is a perfectly good JS value/statement, like (almost) all other JSON. Wikipedia might go a bit too far in saying that all JSON formatted text is legal JS code, but (IMHO) it still is a perfectly good subset.The unicode part is a clear error and should be fixed.
This particular case can be patched by replacing "</" with "<\/".
I'm not saying that JSON isn't a part of JS; of course it is! I'm just saying that it isn't a strict subset of JS and that you can't depend on eval() doing the right thing.
Using JSONP with jQuery, you have to directly eval() the response from the server, and you don't control what the server sends - it could send invalid javascript with these code points in it. You can't fix that from the client-side, jQuery can't fix that.
The servers have to be patched.
The user agent evals the JSONP response before anything else happens: the server might return the string 'yourCallback({"JSON":"rocks!"});', which gets evaled to a function invocation whose argument is whatever the string '{"JSON":"rocks!"}' evals to. If that string doesn't successfully eval (as per the OP), your JSONP is broken.
<script src="/whatever?callback=foo"></script>
The source of which is something like foo({"a":"b"})
Therefore, the hash supplied to foo() must be valid JavaScript.Humans prefer easily processable and storable information, and for this reason they like to think JSON is JavaScript Object Notation.
This reminds of how ECMA doesn't like to be called the _European_ Computer Manufacturer's Association, since they have now grown beyond that, and prefer being called Ecma.