Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser-native JSON support (window.JSON)

I have seen references to some browsers natively supporting JSON parsing/serialization of objects safely and efficiently via the window.JSON Object, but details are hard to come by. Can anyone point in the right direction? What are the methods this Object exposes? What browsers is it supported under?

like image 804
levik Avatar asked May 21 '09 03:05

levik


People also ask

Is JSON supported by all browsers?

All modern browsers support native JSON encoding/decoding (Internet Explorer 8+, Firefox 3.1+, Safari 4+, and Chrome 3+). Basically, JSON. parse(str) will parse the JSON string in str and return an object, and JSON.

Can JavaScript parse JSON natively?

JSON is a JavaScript-based object/value encoding format that looks very close to raw JavaScript and can be very easily parsed by JavaScript code because JavaScript can effectively evaluate a JSON string and re-materialize an object from it.

What is native JSON?

native-json is a header package for building c++ native addons for node. js that is meant to complement nan by adding an interface to the v8::JSON object's Parse and Stringify methods that can be relied upon regardless of the version of node. js that the addon is being built against.

What is difference between JSON and JavaScript?

JavaScript Objects VS JSONJSON cannot contain functions. JavaScript objects can contain functions. JSON can be created and used by other programming languages. JavaScript objects can only be used in JavaScript.


2 Answers

All modern browsers support native JSON encoding/decoding (Internet Explorer 8+, Firefox 3.1+, Safari 4+, and Chrome 3+). Basically, JSON.parse(str) will parse the JSON string in str and return an object, and JSON.stringify(obj) will return the JSON representation of the object obj.

More details on the MDN article.

like image 176
Sasha Chedygov Avatar answered Oct 15 '22 00:10

Sasha Chedygov


jQuery-1.7.1.js - 555 line...

parseJSON: function( data ) {     if ( typeof data !== "string" || !data ) {         return null;     }      // Make sure leading/trailing whitespace is removed (IE can't handle it)     data = jQuery.trim( data );      // Attempt to parse using the native JSON parser first     if ( window.JSON && window.JSON.parse ) {         return window.JSON.parse( data );     }      // Make sure the incoming data is actual JSON     // Logic borrowed from http://json.org/json2.js     if ( rvalidchars.test( data.replace( rvalidescape, "@" )         .replace( rvalidtokens, "]" )         .replace( rvalidbraces, "")) ) {          return ( new Function( "return " + data ) )();      }     jQuery.error( "Invalid JSON: " + data ); }      rvalidchars = /^[\],:{}\s]*$/,  rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,  rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,  rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g, 
like image 29
lks Avatar answered Oct 15 '22 01:10

lks