JSON polyfill: JSON 2 or JSON 3?

拜拜、爱过 提交于 2019-12-29 05:05:27

问题


I need a JSON polyfill to support IE 7 and some IE 8 and IE 9 modes. Should I pick JSON 2 or JSON 3? Obviously JSON 3 is a more modern implementation, but for example a site like html5please doesn't even mention it and recommends JSON 2.


回答1:


HTML5 Please has updated their JSON page to include JSON 3 as the main polyfill for use, with JSON 2 taking a backseat as the older replacement.




回答2:


I was wondering the same thing. JSON3 says:

Changes from JSON 2

JSON 3...

  • Correctly serializes primitive wrapper objects (Issue #28).
  • Throws a TypeError when serializing cyclic structures (JSON 2 recurses until the call stack overflows).
  • Utilizes feature tests to detect broken or incomplete native JSON implementations (JSON 2 only checks for the presence of the native functions). The tests are only executed once at runtime, so there is no additional performance cost when parsing or serializing values.

In contrast to JSON 2, JSON 3 does not...

  • Add toJSON() methods to the Boolean, Number, and String prototypes. These are not part of any standard, and are made redundant by the design of the stringify() implementation.
  • Add toJSON() or toISOString() methods to Date.prototype. See the note about date serialization below.

bestiejs.github.com/json3/

HTML5 Please says:

JSON 3 is a polyfill compatible with nearly all JavaScript platforms. It is a drop-in replacement for JSON 2 that uses feature tests to detect broken and incomplete native JSON implementations, supports asynchronous module loaders and has-aware optimizers, and is built to specification standards.

JSON 2 is an older, alternative polyfill by Douglas Crockford.




回答3:


Use json2.js if you want to improve the performance of your page in IE7 (and possibly 8). I was happily using json3 for months and then I saw this question and did some research. All of the jsperf's performed 1-2x faster with json2.js in my IE7 VM.

One example:

http://jsperf.com/json-vs-jquery-json/2



来源:https://stackoverflow.com/questions/10963723/json-polyfill-json-2-or-json-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!