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.

16 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.