Skip to content
Permalink
Browse files
Try to use the native JSON parser in all cases and fallback to the ol…
…d technique otherwise. This allows us to also handle cases where the JSON parser is unable to parse JSON-like strings correctly (e.g. {foo:bar}) which is something that worked before but would stop working with the switch to the new parser.
  • Loading branch information
jeresig committed Jan 5, 2010
1 parent 3f648c4 commit ff3645ee05ca5cb416b7d3500a45a4410ce0470a
Showing 1 changed file with 4 additions and 2 deletions.
@@ -579,9 +579,11 @@ jQuery.extend({

// Get the JavaScript object, if JSON is used.
if ( type === "json" ) {
if ( typeof JSON === "object" && JSON.parse ) {
// Try to use the native JSON parser first
try {
data = JSON.parse( data );
} else {

} catch(e) {
data = (new Function("return " + data))();
}
}

7 comments on commit ff3645e

@leeoniya
Copy link

@leeoniya leeoniya commented on ff3645e Jan 5, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've run into a known IE8 issue with its native parser's inability to serialize empty values of DOM elements. details here:
http://blogs.msdn.com/jscript/archive/2009/06/23/serializing-the-value-of-empty-dom-elements-using-native-json-in-ie8.aspx

just a heads up,
Leon

@m64253
Copy link

@m64253 m64253 commented on ff3645e Jan 6, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity; is double quotes required (") for keys or will single quotes suffice?

@scottgonzalez
Copy link
Member

@scottgonzalez scottgonzalez commented on ff3645e Jan 6, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON strings only use double quotes. See json.org for the full (very tiny) spec.

@leeoniya
Copy link

@leeoniya leeoniya commented on ff3645e Jan 6, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dunno why my first comment didnt show up...reposting w/tinyurl...

i've run into a known IE8 issue with its native parser's inability to serialize empty values of DOM elements.
details here: http://tinyurl.com/m2j294

just a heads up,
Leon

@m64253
Copy link

@m64253 m64253 commented on ff3645e Jan 6, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I know, was just wondering what would "work"

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on ff3645e Jan 7, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IE8: JSON.parse("{ 'a':1 }") ---> Syntax Error
CHROME: JSON.parse("{ 'a':1 }") ---> OK
FF: JSON.parse("{ 'a':1 }") ---> Syntax Error
OPERA 10.10: JSON.parse("{ 'a':1 }") ---> Undefined variable JSON
SAFARI 4.0.4: JSON.parse("{ 'a':1 }") ---> Syntax Error

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on ff3645e Jan 7, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.