outaTiME

JSON 2.0: Libraries and browser support

Posted in development, javascript, json by outaTiME on January 31, 2008

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

He links to an updated library by Douglas Crockford, JSON2.js, that differs from the first version by using “a single base object (JSON) instead of extending all native object prototypes”.

JAVASCRIPT:

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

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

JAVASCRIPT:

  1.  
  2. var nativeJSON = Components.classes[“@mozilla.org/dom/json;1″]
  3.     .createInstance(Components.interfaces.nsIJSON);
  4. nativeJSON.encode({name: “John”, location: “Boston”});
  5. // => "{‘name’:'John’,'location’:'Boston’}"
  6. nativeJSON.decode(“{‘name’:'John’,'location’:'Boston’}”);
  7. // => {name: "John", location: "Boston"}
  8.  

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.

(Via Ajaxian.)

Leave a Reply