Difference between json.js and json2.js

Json

Json Problem Overview


Can someone tell me what the difference is between the 2 JSON parsers?

https://github.com/douglascrockford/JSON-js/blob/master/json.js
https://github.com/douglascrockford/JSON-js/blob/master/json2.js

I have a JSON file from 2007-04-13 (It has methods such as parseJSON). I don't see these methods in any of the new versions.

Json Solutions


Solution 1 - Json

From their code:

// Augment the basic prototypes if they have not already been augmented.
// These forms are obsolete. It is recommended that JSON.stringify and
// JSON.parse be used instead.

if (!Object.prototype.toJSONString) {
    Object.prototype.toJSONString = function (filter) {
        return JSON.stringify(this, filter);
    };
    Object.prototype.parseJSON = function (filter) {
        return JSON.parse(this, filter);
    };
}

I guess parseJSON is obsolete, therefore the new version (json2) doesn't even use it anymore. However if your code uses parseJSON a lot you could just add this piece of code somewhere to make it work again:

    Object.prototype.parseJSON = function (filter) {
        return JSON.parse(this, filter);
    };

Solution 2 - Json

Quoting here:

"JSON2.js - Late last year Crockford quietly released a new version of his JSON API that replaced his existing API. The important difference was that it used a single base object."

Solution 3 - Json

I also noticed that json2 stringified arrays differently than json2007.

In json2007:

var array = [];
array[1] = "apple";
array[2] = "orange";
alert(array.toJSONString()); // Output: ["apple", "orange"].

In json2:

var array = [];
array[1] = "apple";
array[2] = "orange";
alert(JSON.stringify(array)); // Output: [null, "apple", "orange"].

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBob SmithView Question on Stackoverflow
Solution 1 - JsonLuca MatteisView Answer on Stackoverflow
Solution 2 - JsonpaxdiabloView Answer on Stackoverflow
Solution 3 - JsonVimil SajuView Answer on Stackoverflow