X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fcore.js;h=efdc4570ffb2e13cdc9bdd5febe648fbee746042;hb=781fe8b80d08b287e4c6e4ca408f773c6a1f3b2d;hp=5c990688803ee7f10bed1f015b3c543d8e044333;hpb=ea9e0ed841f0f2851162e01d5199052872ba7483;p=jquery.git diff --git a/src/core.js b/src/core.js index 5c99068..efdc457 100644 --- a/src/core.js +++ b/src/core.js @@ -472,25 +472,24 @@ jQuery.extend({ }, 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, "@") + 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, ""))) { + .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) { // Try to use the native JSON parser first - if ( window.JSON && window.JSON.parse ) { - data = window.JSON.parse( data ); - - } else { - data = (new Function("return " + data))(); - } + return window.JSON && window.JSON.parse ? + window.JSON.parse( data ) : + (new Function("return " + data))(); } else { jQuery.error( "Invalid JSON: " + data ); } - - return data; }, noop: function() {},