Skip to content

Commit

Permalink
Make sure leading whitespace is trimmed for parseJSON. Fixes #6031.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Feb 13, 2010
1 parent 8b86004 commit 94d925c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core.js
Expand Up @@ -480,6 +480,9 @@ jQuery.extend({
if ( typeof data !== "string" || !data ) {
return null;
}

// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );

// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
Expand Down
6 changes: 4 additions & 2 deletions test/unit/core.js
Expand Up @@ -807,14 +807,16 @@ test("jQuery.proxy", function(){
});

test("jQuery.parseJSON", function(){
expect(7);
expect(8);

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." );

same( jQuery.parseJSON('\n{"test":1}'), {"test":1}, "Make sure leading whitespaces are handled." );

try {
jQuery.parseJSON("{a:1}");
Expand All @@ -829,4 +831,4 @@ test("jQuery.parseJSON", function(){
} catch( e ) {
ok( true, "Test malformed JSON string." );
}
});
});

0 comments on commit 94d925c

Please sign in to comment.