X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=efdc4570ffb2e13cdc9bdd5febe648fbee746042;hb=4f9aa62a858f28b8ed310ac73f9d2cd788bf1eed;hp=4102e5c8ccce75952b608704db6da175b5380eea;hpb=09ef5b7598bcdd9e6c254b24b55a54e6bfb2b4ca;p=jquery.git diff --git a/src/core.js b/src/core.js index 4102e5c..efdc457 100644 --- a/src/core.js +++ b/src/core.js @@ -466,6 +466,31 @@ jQuery.extend({ } return true; }, + + 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() {},