mirror of
https://github.com/jquery/jquery.git
synced 2026-02-02 12:55:02 -05:00
Move parsing methods to their own files (separates manipulation dependency from core)
Conflicts: src/core.js
This commit is contained in:
41
src/ajax/parseJSON.js
Normal file
41
src/ajax/parseJSON.js
Normal file
@@ -0,0 +1,41 @@
|
||||
define([
|
||||
"../core"
|
||||
], function( jQuery ) {
|
||||
|
||||
var rvalidchars = /^[\],:{}\s]*$/,
|
||||
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
|
||||
rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,
|
||||
rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g;
|
||||
|
||||
jQuery.parseJSON = function( data ) {
|
||||
// Attempt to parse using the native JSON parser first
|
||||
if ( window.JSON && window.JSON.parse ) {
|
||||
return window.JSON.parse( data );
|
||||
}
|
||||
|
||||
if ( data === null ) {
|
||||
return data;
|
||||
}
|
||||
|
||||
if ( typeof data === "string" ) {
|
||||
|
||||
// Make sure leading/trailing whitespace is removed (IE can't handle it)
|
||||
data = jQuery.trim( data );
|
||||
|
||||
if ( 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 );
|
||||
};
|
||||
|
||||
return jQuery.parseJSON;
|
||||
});
|
||||
Reference in New Issue
Block a user