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 with 4 additions and 2 deletions.
  1. +4 −2 src/ajax.js
@@ -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

This comment has been minimized.

Copy link

leeoniya replied Jan 5, 2010

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

This comment has been minimized.

Copy link

m64253 replied Jan 6, 2010

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

@scottgonzalez

This comment has been minimized.

Copy link
Member

scottgonzalez replied Jan 6, 2010

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

@leeoniya

This comment has been minimized.

Copy link

leeoniya replied Jan 6, 2010

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

This comment has been minimized.

Copy link

m64253 replied Jan 6, 2010

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

@DBJDBJ

This comment has been minimized.

Copy link

DBJDBJ replied Jan 7, 2010

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

Please sign in to comment.
You can’t perform that action at this time.