mirror of
https://github.com/jquery/jquery.git
synced 2024-11-23 02:54:22 +00:00
Fix #12751. Ensure parseJson throws in the same situations as JSON.parse. Close gh-993.
This commit is contained in:
parent
c31539c8a2
commit
ee9687d441
@ -133,4 +133,5 @@ Daniel Chatfield <chatfielddaniel@gmail.com>
|
||||
Nikita Govorov <nikita.govorov@gmail.com>
|
||||
Michael Pennisi <mike@mikepennisi.com>
|
||||
Markus Staab <markus.staab@redaxo.de>
|
||||
Daniel Gálvez <dgalvez@editablething.com>
|
||||
Daniel Gálvez <dgalvez@editablething.com>
|
||||
James Huston <james@jameshuston.net>
|
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 );
|
||||
},
|
||||
|
||||
|
@ -2759,4 +2759,18 @@ if ( jQuery.ajax && ( !isLocal || hasPHP ) ) {
|
||||
start();
|
||||
});
|
||||
});
|
||||
|
||||
test( "jQuery.ajax - empty json gets to error callback instead of success callback.", function() {
|
||||
expect( 1 );
|
||||
|
||||
stop();
|
||||
|
||||
jQuery.ajax( url("data/echoData.php"), {
|
||||
error: function( _, __, error ) {
|
||||
equal( typeof error === "object", true, "Didn't get back error object for empty json response" );
|
||||
start();
|
||||
},
|
||||
dataType: "json"
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -1150,9 +1150,13 @@ test("jQuery.parseHTML", function() {
|
||||
test("jQuery.parseJSON", function(){
|
||||
expect(8);
|
||||
|
||||
equal( jQuery.parseJSON(), null, "Nothing in, null out." );
|
||||
equal( jQuery.parseJSON( null ), null, "Nothing in, null out." );
|
||||
equal( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
|
||||
raises(function() {
|
||||
jQuery.parseJSON();
|
||||
}, null, "parseJson now matches JSON.parse for empty input." );
|
||||
equal(jQuery.parseJSON( null ), null, "parseJson now matches JSON.parse on null input." );
|
||||
raises( function() {
|
||||
jQuery.parseJSON( "" );
|
||||
}, null, "parseJson now matches JSON.parse for empty strings." );
|
||||
|
||||
deepEqual( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
|
||||
deepEqual( jQuery.parseJSON("{\"test\":1}"), {"test":1}, "Plain object parsing." );
|
||||
@ -1346,3 +1350,50 @@ test("jQuery.camelCase()", function() {
|
||||
equal( jQuery.camelCase( key ), val, "Converts: " + key + " => " + val );
|
||||
});
|
||||
});
|
||||
|
||||
test( "JQuery.parseJSON() test internal parseJson (using fallback) to make sure that it throws like JSON.parse", function() {
|
||||
expect( 10 );
|
||||
|
||||
var jsonParse = window.JSON;
|
||||
window.JSON = null;
|
||||
|
||||
raises(function() {
|
||||
jsonParse.parse("''");
|
||||
});
|
||||
|
||||
raises(function() {
|
||||
jQuery.parseJSON("''");
|
||||
});
|
||||
|
||||
raises(function() {
|
||||
jsonParse.parse("");
|
||||
});
|
||||
|
||||
raises(function() {
|
||||
jQuery.parseJSON("");
|
||||
});
|
||||
|
||||
raises(function() {
|
||||
jsonParse.parse({});
|
||||
});
|
||||
|
||||
raises(function() {
|
||||
jQuery.parseJSON({});
|
||||
});
|
||||
|
||||
var parsedValue = jsonParse.parse(null);
|
||||
equal( parsedValue, null );
|
||||
|
||||
parsedValue = jQuery.parseJSON(null);
|
||||
equal( parsedValue, null );
|
||||
|
||||
parsedValue = jsonParse.parse("{}");
|
||||
equal( (typeof parsedValue === "object"), true );
|
||||
|
||||
parsedValue = jQuery.parseJSON("{}");
|
||||
equal( (typeof parsedValue === "object"), true );
|
||||
|
||||
|
||||
window.JSON = jsonParse;
|
||||
} );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user