jQuery uses JSON.parse() for a cross-browser solution when converting JSON-strings to objects using jQuery.parseJSON():
parseJSON: function( data ) {
if ( !data || typeof data !== "string") {
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 );
}
But why isn’t there a similar solution for converting objects to JSON using JSON.stringify? JSON.stringify doesn’t work in IE7 for example, unless you include json2.js. Is it something that’s on jQuerys roadmap?
There isn’t a similar solution because jQuery internally does not need it. The current stance on it is If the core doesn’t need it, and it can be implemented using generally accepted methods (including json2.js or using the one built into all modern browsers) it doesn’t need to be in the core.
For reference, https://forum.jquery.com/topic/jquery-encodejson