New JavaScript features with native JSON support and JavaScript 1.8.1 additions
With the impending release of Firefox 3.5, I thought I’d cover the new things in JavaScript 1.8.1 – part of that is the very exciting support for native JSON, and you know what? They’re not the only ones supporting it!
Native JSON
JSON (JavaScript Object Notation) has become a very popular light-weight data format, and also almost a de-facto way for sending data cross-domains. It’s a great way to work with information, and the content is very easy to grasp. There’s also support built into virtually every technology to serialize/deserialize JSON content and to work further with it.
However, till very recently, there has been no native way in JavaScript to do this. Another problem is that you send/receive JSON as a string, so you need to use the no-no eval
to turn it into a JavaScript object. Using eval
is a great security risk, and the possible result is that you could get malicious code in which would then be executed immediately.
Therefore, to complement your JSON code, you would need some function to make sure the JSON object is safe, and the result there is extra code and, especially, extra execution time.
However, times they are a-changin’!
With Firefox 3.5 and Internet Explorer 8, native JSON support is built in! In those web browsers, you don’t need to worry about those above mentioned security risks or performance implications, because it’s all natively taken care of. There are two methods on a global JSON object to do this:
JSON.parse
JSON.parse
turns a string into a proper JavaScript object:
var JSONString = '{"name" : "Robert", "lastName" : "Nyman"}', JSONObject = JSON.parse(JSONString);
JSON.stringify
JSON.stringify
turns a JavaScript object into a string:
var obj = { name : "Robert", lastName : "Nyman" }, JSONString = JSON.stringify(obj); // Result: {"name":"Robert","lastName":"Nyman"}
JavaScript 1.8.1
Mozilla has also decided to make some minor additions to JavaScript with JavaScript 1.8.1.
Object.getPrototypeOf
Object.getPrototypeOf
is a way to get what object a certain object is prototype of, i.e. from where does it inherit things. Basically, for those used to the __proto__
of doing that, this is a standardized way to achieve the same thing. John Resig goes into a little more how this can be useful in his article on Object.getPrototypeOf.
String extras
There are tree additional methods to string objects for trimming text. They are trim
, trimLeft
and trimRight
.
var someString = " Monkeys like bananas ", someStringTrimmed = someString.trim(); // Result: "Monkeys like bananas" var someString = " Monkeys like bananas ", someStringTrimmedLeft = someString.trimLeft(); // Result: "Monkeys like bananas " var someString = " Monkeys like bananas ", someStringTrimmedRight = someString.trimRight(); // Result: " Monkeys like bananas"
Test cases and sample code
As you are probably aware of now, I’m quite happy with my JavaScript tests & Compatibility tables, and in there I have now added compatibility tables for these new additions, and also a test page for JavaScript 1.8.1 and a test page for native JSON.
My takes
Generally, JavaScript 1.8.1 includes some niceties that are good to have, but aren’t really issues. Native JSOn support on the other hand will lead to a safer and better performing web, and that is quite exciting!
Also, kudos to IE 8 to be the first officially released web browser supporting it. However, I’m a bit saddened that native JSON is not in the new Safari 4, Google Chrome 2 or upcoming Opera 10, because we really need it, and I hope they implement it soon.
I think you made a mistake in your example for JSON.stringify π
Stijn,
Thank you! It's fixed now. π
Nice overview! Cheers!
Isn't the comment result for someString.trim() wrong ?
The result hasn't been trimmed.
Samisin,
Glad you liked it!
ChS,
You are absolutely right! Sorry for being so sloppy – I'm just a bit tired and getting ready to celebrate midsummer. π
Hi Robert, nice one. Would be nice as well to see a compatibility page adding vice-versa on the top of the test case:
http://vice-versa.googlecode.com/svn/trunk/build/…
π
I'm so excited about this! Once I started using JSON, I never went back to XML (via AJAX). More JSON support is great!
Andrea,
Thanks! And yes, it would be nice, although I try to keep this as independent as possible. π
Dean,
Absolutely, I'm very happy about it as well!
I'm still trying to figure out what the effects are when using the native JSON functions in a non-UTF8 setting. All non-ASCII characters are converted to uxxxx character-references regardless of whether they are valid characters in the current charset or not; that's something to be aware of. The simple argument that you always should use UTF8 simply doesn't always apply.
One thing worth noting is that at least in some cases (Firefox 3.5 for example) using the JSON.parse function is rapidly faster than eval. I did a quick bunch of tests here and you can see the difference. Thanks for writing such a useful blog, including this post, I'm a new reader, but I've really enjoyed your content!
Regards,
Randy
[…] New JavaScript features with native JSON support and JavaScript 1.8.1 additions […]
Tino,
Absolutely, it's a very valid concern – and naturally you should be able to use various character sets. At this point, though, I don't know more about it.
Randy,
Definitely, it's lightning-fast, which is why native implementations are generally such a good thing!
And thanks for the kind words, I really appreciate it! π
[…] New JavaScript features with native JSON support and JavaScript 1.8.1 additions […]
[…] 18: New JavaScript features with native JSON support and JavaScript 1.8.1 additions […]
Hi robert,
I’ve checked Safari 4.0.5 and Opera 10.53 on you test page:
http://www.robertnyman.com/javascript/javascript-native-json.html
Both supports native json!
Best regards
Christian
Christian,
Thanks, I have now updated the tests!