PDA

View Full Version : Why Ext.USE_NATIVE_JSON is false by default?



LesJ
9 Jul 2012, 12:00 PM
Why Ext.USE_NATIVE_JSON is false by default?

Native JSON parsing is faster and more secure because it will not compile scripts.

scottmartin
9 Jul 2012, 1:56 PM
Here is a previous thread that discusses this a little:
http://www.sencha.com/forum/showthread.php?73110

Scott.

LesJ
9 Jul 2012, 2:19 PM
Thanks Scott, but this is a very old thread.

You should try to parse using the native JSON parser first.

Even jQuery (http://code.jquery.com/jquery-1.7.2.js) is doing that ;)

scottmartin
9 Jul 2012, 2:39 PM
I will shoot this over to one of the senior developers so we can both find out ;)

Scott.

evant
9 Jul 2012, 6:29 PM
Legacy, mostly. There's a whole lot of code that works fine with:



{
data: [{
name: 'foo'
}]
}


Then suddenly it all explodes. Most certainly it's better to use the native JSON, but it's off by default for the purposes of backwards compatibility.

extjs@kingsquare.nl
19 Nov 2014, 12:22 AM
There are more issues with the current default (eval-based) approach. E.g. \u2028 and \u2029 characters can not be decoded using eval since they are not valid in a string, but work fine with JSON.parse.

Try in your console for example:

var a = {"test": "?this"};

You will see JavaScript throws an error, since the word "this" contains such a character. However, when this would be received through AJAX (and not as a string literal), it could perfectly fine be decoded through JSON.parse.

LesJ
21 Mar 2017, 5:59 AM
Legacy, mostly. There's a whole lot of code that works fine with:



{
data: [{
name: 'foo'
}]
}


Then suddenly it all explodes. Most certainly it's better to use the native JSON, but it's off by default for the purposes of backwards compatibility.

This is very interesting. I always thought that JSON.parse is the fastest way to parse JSON on all browsers.

I just tried this test, and eval is significantly faster on Chrome 56 than JSON.parse (both on macOS and Windows).

https://jsperf.com/json-parse-vs-eval/6

The results are reversed on IE11 and Firefox.