Thursday, January 31st, 2008

JSON 2.0: Libraries and browser support

Category: Browsers, JavaScript, JSON, Library

John is at it again, writing a piece on recent news surrounding JSON.

He links to an updated library by Douglas Crockford,

  1. JSON.stringify({name: "John", location: "Boston"});
  2. // => "{'name':'John','location':'Boston'}"
  3. JSON.parse("{'name':'John','location':'Boston'}");
  4. // => {name: "John", location: "Boston"}

It also turns out that Mozilla implemented this functionality in the browser (time for a wrapper):


  1. var nativeJSON = Components.classes[";1"]
  2.     .createInstance(Components.interfaces.nsIJSON);
  3. nativeJSON.encode({name: "John", location: "Boston"});
  4. // => "{'name':'John','location':'Boston'}"
  5. nativeJSON.decode("{'name':'John','location':'Boston'}");
  6. // => {name: "John", location: "Boston"}

And in conclusion:

The final, and most important, step is being worked on right now – a way to access native JSON encoding and decoding from web pages. How it’ll be accessible is up to some debate (as having its naming conflict with an existing object would be a really bad thing). Regardless, there should be something within the browser by the time the Firefox 3 betas wrap-up.

Posted by Dion Almaer at 5:00 am

4.5 rating from 17 votes


Comments feed TrackBack URI

would be better in security.

Comment by phpcs — January 31, 2008

I’m no Asa Dotzler, heavens forbid no, but I reckon passing a JS object to an XPCOM object and then passing it back would make it slower due to unnecessary conversion overhead. Perhaps a pure JS solution would be quicker. Just my speculation.

Comment by Jordan — January 31, 2008

Leave a comment

You must be logged in to post a comment.