90

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.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
  • 2
    You can find the new file here https://github.com/douglascrockford/JSON-js – Daniel Little May 21 '11 at 03:43
  • 1
    For anybody who came to this question wondering about what these files are, know that there is no reason to use them in modern browsers. From the [GitHub repo](https://github.com/douglascrockford/JSON-js): "On current browsers, [json2.js] does nothing, preferring the built-in JSON object. There is no reason to use this file unless fate compels you to support IE8, which is something that no one should ever have to do again." – Thunderforge Aug 30 '17 at 16:12

3 Answers3

60

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);
    };
Chris
  • 11,780
  • 13
  • 48
  • 70
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
  • 1
    Thanks, so it appears that parseJSON has been replaced by JSON.parse? Also, what about toJSONString? Our existing code uses a lot of these methods: boolean.toJSONString() date.toJSONString() number.toJSONString() object.toJSONString() string.toJSONString() –  Feb 16 '09 at 03:38
  • 1
    Then also add the 1st piece of code, all the values you specified are Objects, therefore they will all be converted to use JSON.stringify automatically. – Luca Matteis Feb 16 '09 at 03:41
  • Thanks! I will give this a try. So, can I add these functions to the json.js file? –  Feb 16 '09 at 04:33
  • "absolete" - absolute or obsolete? – Eric Nov 20 '10 at 20:54
  • 84
    "absolete" - when it's definitely obsolete. – davidtbernal Jan 04 '11 at 17:24
32

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."

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
26

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"].
XP1
  • 6,910
  • 8
  • 54
  • 61
Vimil Saju
  • 345
  • 3
  • 7