Make empty strings (and other non-string values) simply return null from parseJSON. Also added some parseJSON tests. Fixes #5859.
This commit is contained in:
parent
ea9e0ed841
commit
781fe8b80d
19
src/core.js
19
src/core.js
|
@ -472,25 +472,24 @@ jQuery.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
parseJSON: function( data ) {
|
parseJSON: function( data ) {
|
||||||
|
if ( typeof data !== "string" || !data ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure the incoming data is actual JSON
|
// Make sure the incoming data is actual JSON
|
||||||
// Logic borrowed from http://json.org/json2.js
|
// 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(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
|
||||||
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
|
.replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
|
||||||
|
|
||||||
// Try to use the native JSON parser first
|
// Try to use the native JSON parser first
|
||||||
if ( window.JSON && window.JSON.parse ) {
|
return window.JSON && window.JSON.parse ?
|
||||||
data = window.JSON.parse( data );
|
window.JSON.parse( data ) :
|
||||||
|
(new Function("return " + data))();
|
||||||
} else {
|
|
||||||
data = (new Function("return " + data))();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
jQuery.error( "Invalid JSON: " + data );
|
jQuery.error( "Invalid JSON: " + data );
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
noop: function() {},
|
noop: function() {},
|
||||||
|
|
|
@ -805,3 +805,28 @@ test("jQuery.proxy", function(){
|
||||||
// Use the string shortcut
|
// Use the string shortcut
|
||||||
jQuery.proxy( thisObject, "method" )();
|
jQuery.proxy( thisObject, "method" )();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("jQuery.parseJSON", function(){
|
||||||
|
expect(7);
|
||||||
|
|
||||||
|
equals( jQuery.parseJSON(), null, "Nothing in, null out." );
|
||||||
|
equals( jQuery.parseJSON( null ), null, "Nothing in, null out." );
|
||||||
|
equals( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
|
||||||
|
|
||||||
|
same( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
|
||||||
|
same( jQuery.parseJSON('{"test":1}'), {"test":1}, "Plain object parsing." );
|
||||||
|
|
||||||
|
try {
|
||||||
|
jQuery.parseJSON("{a:1}");
|
||||||
|
ok( false, "Test malformed JSON string." );
|
||||||
|
} catch( e ) {
|
||||||
|
ok( true, "Test malformed JSON string." );
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
jQuery.parseJSON("{'a':1}");
|
||||||
|
ok( false, "Test malformed JSON string." );
|
||||||
|
} catch( e ) {
|
||||||
|
ok( true, "Test malformed JSON string." );
|
||||||
|
}
|
||||||
|
});
|
Loading…
Reference in a new issue