Wednesday, October 01, 2008

Firefox 3.1 will have native JSON support

For those unaware of what JSON (JavaScript Object Notation) is. JSON is a method to exchange data (think more about the content of variables) across multiple programming languages. At first it was only available for JavaScript, but now almost every language has its parser. It started as way to improve the use of 'eval' in JavaScript to parse data obtained through Ajax or other ways. The big problem with 'eval' was it is very easy to create a security hole. JSON eliminates that and it is possible to exchange data without fear.

So what's the buzz? Firefox 3.0 already contained native JSON support! Yes it did, but it was only available for code with elevated privileges. So only add-on developers could use it. Also it isn't too easy to remeber the code.

<script>
// encode to string using native implementation
var Ci = Components.interfaces;
var Cc = Components.classes;

var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
var testNative = nativeJSON.encode(testObj);
</script>
So you could without problems say the native JSON support was limited and only for testing it for further releases. Normally the developer release of Firefox 3.1 beta 1 will give native support for JSON available for web developers. Just like Internet Explorer 8.0 beta 2 already supports JSON natively. Also Webkit is already looking to implement JSON natively.

So it will not take to long, before you just can parse and serialize data natively. Here is also the code that would do the job.
<script>
var jsObjString = "{\"memberNull\" : null, \"memberNum\" : 3"};
var jsObjStringParsed = JSON.parse(jsObjString);
var jsObjStringBack = JSON.stringify(jsObjStringParsed);
</script>

0 reacties: