{"id":222,"date":"2009-02-12T09:28:42","date_gmt":"2009-02-12T17:28:42","guid":{"rendered":"http:\/\/blog.mozilla.org\/webdev\/?p=222"},"modified":"2017-03-08T14:50:09","modified_gmt":"2017-03-08T22:50:09","slug":"native-json-in-firefox-31","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/","title":{"rendered":"Native JSON in Firefox 3.1"},"content":{"rendered":"<p>In case you haven&#8217;t heard, one of Firefox 3.1&#8217;s awesome new features will be <a href=\"https:\/\/developer.mozilla.org\/En\/Using_JSON_in_Firefox\">native JSON<\/a> support. This is totally sweet for two reasons:<\/p>\n<ol>\n<li>eval&#8217;ing JSON in the browser is <a href=\"http:\/\/yuiblog.com\/blog\/2007\/04\/10\/json-and-browser-security\/\">unsafe<\/a>. Using native JSON parsing protects you against possible code execution.<\/li>\n<li>Safely eval&#8217;ing JSON with a 3rd party library can be orders of magnitude <a href=\"http:\/\/starkravingfinkle.org\/blog\/2008\/02\/extension-developers-native-json-parsing\/\">slower<\/a>. Native JSON parsing is much faster.<\/li>\n<\/ol>\n<p>How does native JSON work compared to plain old eval? Simple:<\/p>\n<pre>var jsonString = '{\"name\":\"Ryan\", \"address\":\"Mountain View, CA\"}';\r\nvar person = JSON.parse(jsonString);\r\n\/\/ 'person' is now a JavaScript object with 2 properties; name and address<\/pre>\n<p>Pretty easy huh? And here&#8217;s how to get a JSON string from an object:<\/p>\n<pre>var personString = JSON.stringify(person);\r\n\/\/ 'personString' now holds the string '{\"name\":\"Ryan\", \"address\":\"Mountain View, CA\"}'<\/pre>\n<p>&#8220;But wait!&#8221;, you say. &#8220;How is it safer? How much faster is it compared to eval?&#8221;. Ok, I&#8217;ll show you.<\/p>\n<p>Native JSON parsing in Firefox 3.1 is safer because it does not support objects with functions. Attempting to convert an object with functions into a JSON string will only convert its properties and not its functions. And any malformed JSON string will result in a parse error instead of possible code execution.<\/p>\n<p>Now, regarding speed, native JSON parsing is faster, <strong>much<\/strong> faster. Instead of pretty charts and graphs, I&#8217;ll give you a real-world example.<\/p>\n<p>The <a href=\"http:\/\/graphs-stage2.mozilla.org\/graph.html\">new Graph Server<\/a> uses a JSON API to fetch test information and results, so I figured it would be a good application to benchmark. So I wrapped our code that parses the JSON response with some Firebug profiler calls:<\/p>\n<pre>    console.time('parsejson');\r\n    var obj = eval('(' + resp + ')');\r\n    console.timeEnd('parsejson');<\/pre>\n<p>Loading a test&#8217;s results (array with 3,000 indexes, 24k gzipped) gave me a time of 125ms. (Repeated tests yielded results +\/- 5ms). Then I changed eval to JSON.parse:<\/p>\n<pre>    console.time('parsejson');\r\n    var obj = JSON.parse(resp);\r\n    console.timeEnd('parsejson');<\/pre>\n<p>Which resulted in an average time of 40ms! That&#8217;s about 2.7 times faster with 1 line of code changed. Not bad!<\/p>\n<p>Granted, a difference of 80ms isn&#8217;t that much, but in an AJAX (or, more accurately, AJAJ?) application, it can add up.<\/p>\n<p>What&#8217;s the use of native JSON if it&#8217;s only available in Firefox? Luckily, IE8 has implemented it in RC1, which is rumored to be released in March. Hopefully other browsers will follow suit too, but for now it&#8217;s best to use a JSON parser such as the one on <a href=\"http:\/\/www.json.org\/js.html\">json.org<\/a>. It&#8217;s small, safe and will not override native JSON implementation if detected.<\/p>\n<p>Points to remember:<\/p>\n<ul>\n<li>Plain old eval is unsafe (especially if you don&#8217;t trust the source), use a JSON library to protect yourself.<\/li>\n<li>Use native JSON when available.<\/li>\n<li>Bug other <a href=\"http:\/\/webkit.org\/\">browser<\/a> <a href=\"http:\/\/www.opera.com\/\">vendors<\/a> to implement native JSON<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In case you haven&#8217;t heard, one of Firefox 3.1&#8217;s awesome new features will be native JSON support. This is totally sweet for two reasons: eval&#8217;ing JSON in the browser is unsafe. Using native JSON parsing protects you against possible code &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/\">Continue reading<\/a><\/p>\n","protected":false},"author":1438,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[510,288],"tags":[],"coauthors":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Native JSON in Firefox 3.1 - Mozilla Web Development<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"mozilla\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/\",\"url\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/\",\"name\":\"Native JSON in Firefox 3.1 - Mozilla Web Development\",\"isPartOf\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#website\"},\"datePublished\":\"2009-02-12T17:28:42+00:00\",\"dateModified\":\"2017-03-08T22:50:09+00:00\",\"author\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.mozilla.org\/webdev\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Native JSON in Firefox 3.1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#website\",\"url\":\"https:\/\/blog.mozilla.org\/webdev\/\",\"name\":\"Mozilla Web Development\",\"description\":\"For make benefit of glorious tubes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.mozilla.org\/webdev\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9\",\"name\":\"mozilla\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/image\/e77ee64829d0c3831212656324f746d1\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g\",\"caption\":\"mozilla\"},\"url\":\"https:\/\/blog.mozilla.org\/webdev\/author\/mozilla-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Native JSON in Firefox 3.1 - Mozilla Web Development","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/","twitter_misc":{"Written by":"mozilla","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/","url":"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/","name":"Native JSON in Firefox 3.1 - Mozilla Web Development","isPartOf":{"@id":"https:\/\/blog.mozilla.org\/webdev\/#website"},"datePublished":"2009-02-12T17:28:42+00:00","dateModified":"2017-03-08T22:50:09+00:00","author":{"@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9"},"breadcrumb":{"@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/02\/12\/native-json-in-firefox-31\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.mozilla.org\/webdev\/"},{"@type":"ListItem","position":2,"name":"Native JSON in Firefox 3.1"}]},{"@type":"WebSite","@id":"https:\/\/blog.mozilla.org\/webdev\/#website","url":"https:\/\/blog.mozilla.org\/webdev\/","name":"Mozilla Web Development","description":"For make benefit of glorious tubes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.mozilla.org\/webdev\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9","name":"mozilla","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/image\/e77ee64829d0c3831212656324f746d1","url":"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g","caption":"mozilla"},"url":"https:\/\/blog.mozilla.org\/webdev\/author\/mozilla-2\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts\/222"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/users\/1438"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/tags?post=222"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/coauthors?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}