1 parseJSON: function( data ) {
2 if ( typeof data !== "string" || !data ) {
3 return null;
4 }
5
6 // Make sure leading/trailing whitespace is removed (IE can't handle it)
7 data = jQuery.trim( data );
8
9 // Make sure the incoming data is actual JSON
10 // Logic borrowed from http://json.org/json2.js
11 if ( rvalidchars.test(data.replace(rvalidescape, "@")
12 .replace(rvalidtokens, "]")
13 .replace(rvalidbraces, "")) ) {
14
15 // Try to use the native JSON parser first
16 return window.JSON && window.JSON.parse ?
17 window.JSON.parse( data ) :
18 (new Function("return " + data))();
19
20 } else {
21 jQuery.error( "Invalid JSON: " + data );
22 }
23 }