X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=efdc4570ffb2e13cdc9bdd5febe648fbee746042;hb=76236a15062aa82a4841748e9071eb3c9535ef62;hp=3ff95e0205f5118598a1e92e5ded9296692095ab;hpb=a6ef036bb6a3610431471eebc2623bf8ad06bdd6;p=jquery.git diff --git a/src/core.js b/src/core.js index 3ff95e0..efdc457 100644 --- a/src/core.js +++ b/src/core.js @@ -470,6 +470,27 @@ jQuery.extend({ error: function( msg ) { throw msg; }, + + parseJSON: function( data ) { + if ( typeof data !== "string" || !data ) { + return null; + } + + // Make sure the incoming data is actual JSON + // Logic borrowed from http://json.org/json2.js + if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@") + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]") + .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) { + + // Try to use the native JSON parser first + return window.JSON && window.JSON.parse ? + window.JSON.parse( data ) : + (new Function("return " + data))(); + + } else { + jQuery.error( "Invalid JSON: " + data ); + } + }, noop: function() {},