Quick question. Is there any need to include the Json2 library anymore? Did I read somewhere that JavaScript supports JSON as standard these days so there's no need to parse it anymore?
Any advantages or disadvantages to Json2 vs standard JavaScript support?
Quick question. Is there any need to include the Json2 library anymore? Did I read somewhere that JavaScript supports JSON as standard these days so there's no need to parse it anymore?
Any advantages or disadvantages to Json2 vs standard JavaScript support?
Share Improve this question edited Dec 17, 2013 at 12:48 Liam asked Mar 16, 2012 at 11:06 LiamLiam 29.7k28 gold badges137 silver badges200 bronze badges 2 |2 Answers
Reset to default 22JSON
was standardised in ECMAScript 5, so browsers that conform to that specification include JSON
, but others browsers don't (such as those that were released before ECMAScript 5/ IE). If you need to support these browsers, you still need to include it.
Specifically, JSON
was introduced in:
- IE8,
- FireFox 3.5 -
- Opera 10.5.
- Chrome (1) (AFAIK)
json2.js
checks whether JSON.parse
is already implemented, and only provides it's implementation if it doesn't; so think of it as a sort of polyfiller for older browsers.
Sources: http://kangax.github.com/es5-compat-table/, http://caniuse.com/#feat=json
Update: Now everyone on IE8 should have the patch (or alternatively a virus!), so if you are supporting IE8+ in document mode 8 or greater, then I suggest remove the Json2 library and use the native JSON.stringify() instead.
There are a variety of situations where you need to use the json2 library:
IE10, IE9, IE8: If the document uses IE7 mode or compatibility mode then you must use json2. If your document can be iframe'd or framed, you might need to test what happens if the host frame is in IE7 mode (weird shit happens with document modes and frames!).
IE8 - Using json2 is the most reliable way to avoid bugs in unpatched IE8. The bugs in JSON.stringify() and JSON.parse() have been patched for a few years but some consumers will be missing the patches, and your customers will get undebuggable errors which is very undesirable (See below for possible workaround).
IE7 - you need to use json2.
Various other old browsers need json2 e.g. iPhone 1. For a detailed breakdown of native support, tick the "Show all browsers" option here: http://caniuse.com/#search=JSON
WORKAROUND for unpatched IE8
Background: Older versions of IE8 using jscript.dll (e.g. 5.8.6001.18702) have a variety of bugs and non-standard behaviour in the JSON implementation. http://support.microsoft.com/kb/976662 (jscript.dll 5.8.6001.22960) patches those bugs. The bugs are also fixed in a later jscript.dll version 5.8.6001.23141 (I don't know which KB is related to that). Some of the issues are discussed here: http://blogs.msdn.com/b/jscript/archive/2009/06/23/serializing-the-value-of-empty-dom-elements-using-native-json-in-ie8.aspx and here: http://blogs.msdn.com/b/jscript/archive/2009/06/23/native-json-support-in-ie8-and-tracking-the-ecmascript-fifth-edition-draft-specification.aspx
Buggy versions can be detected using:
var kb976662bug = (document.documentMode === 8) && (JSON.stringify(document.createElement('input').value) === '"null"';
The only corner case that affected us was due to an empty string generating "null"
instead of ""
. So we removed the JSON2 library and instead we use a workaround that has code like:
if (kb976662bug) {
return JSON.stringify(o, function(k, v) {
return (v === '') ? '' : v;
});
} else {
return JSON.stringify(o);
}
which has worked for a long time in our production code.
Show older browsers
. – Felix Kling Commented Mar 16, 2012 at 11:09