mirror of
https://github.com/jquery/jquery.git
synced 2026-02-02 22:55:08 -05:00
Fix #12751. Ensure parseJson throws in the same situations as JSON.parse. Close gh-993.
This commit is contained in:
committed by
Dave Methvin
parent
c31539c8a2
commit
ee9687d441
35
src/core.js
35
src/core.js
@@ -488,27 +488,32 @@ jQuery.extend({
|
||||
},
|
||||
|
||||
parseJSON: function( data ) {
|
||||
if ( !data || typeof data !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Make sure leading/trailing whitespace is removed (IE can't handle it)
|
||||
data = jQuery.trim( data );
|
||||
|
||||
// Attempt to parse using the native JSON parser first
|
||||
if ( window.JSON && window.JSON.parse ) {
|
||||
return window.JSON.parse( 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 ) )();
|
||||
|
||||
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 );
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user