Monday, February 16th, 2009

Native JSON in Firefox 3.1; Joins IE 8

Category: Browsers, JSON

>

The native JSON API is part of the upcoming 3.1 revision of ECMAScript, so we should see it adopted in browsers pretty quickly. It’s also API compatible with json2.js, as you note, so many many web users will get the performance win without apps needing to update.

I suspect that the performance advantage for native JSON is even more pronounced on the encoding side, but I don’t have tests to hand to back it up.

This was a comment by Mike Shaver, VP of Engineering at Mozilla (disclaimer: where I work).

The comment related to the post on the native JSON support in Firefox 3.1:

In case you haven’t heard, one of Firefox 3.1’s awesome new features will be native JSON support. This is totally sweet for two reasons:

  1. eval’ing JSON in the browser is unsafe. Using native JSON parsing protects you against possible code execution.
  2. Safely eval’ing JSON with a 3rd party library can be orders of magnitude slower. Native JSON parsing is much faster.

How does native JSON work compared to plain old eval? Simple:

var jsonString = '{"name":"Ryan", "address":"Mountain View, CA"}';
var person = JSON.parse(jsonString);
// 'person' is now a JavaScript object with 2 properties; name and address

Pretty easy huh? And here’s how to get a JSON string from an object:

var personString = JSON.stringify(person);
// 'personString' now holds the string '{"name":"Ryan", "address":"Mountain View, CA"}'

“But wait!”, you say. “How is it safer? How much faster is it compared to eval?”. Ok, I’ll show you.

Gotta love it when browsers implement something that just starts to make apps faster without potentially knowing about it.

Related Content:

Posted by Dion Almaer at 4:05 am
8 Comments

++++-
4.3 rating from 33 votes

8 Comments »

Comments feed TrackBack URI

Thats cool news, but you can always use http://www.json.org/json_parse.js without eval and working in all browsers (works really good).

Comment by wilq32 — February 16, 2009

THe native JSON implementation in FF 3.1 currently fails to recognize the second parameter to parse. Sites that use this thus break in FF 3.1. I filed a bug and gladly it looks like it will be fixed https://bugzilla.mozilla.org/show_bug.cgi?id=476374

Comment by Malde — February 16, 2009

Cool.

JS libraries like jQuery will have to update to take advantage of this, right?

Comment by Nosredna — February 16, 2009

This is great, however, since users will still be on old browsers we will still need to worry about them :( That is probably the most frustrating thing about web development.

Comment by cnizz — February 16, 2009

stringify? are you kidding me? Next we’ll hear: “{‘l’:'o’}”.objectify()
.
right?
.
**sigh**
.
It’s like they try to be different from other languages. Never heard of decode or encode?

Comment by ibolmo — February 16, 2009

@ibolmo This is the quasi standard API from http://json.org/json2.js

The file above will use the native implementation if it is present, by the way.

Comment by Malde — February 17, 2009

@Malde, I don’t need a .js. I want someone to explain why they would think stringify is a good method name.
.
/me cries

Comment by ibolmo — February 17, 2009

@Nosredna you better believe jQuery will have a check for native json support and use it if it’s available.

Comment by reconbot — February 17, 2009

Leave a comment

You must be logged in to post a comment.