Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between json.js and json2.js

Tags:

json

People also ask

What is the difference between JSON and JS?

JavaScript Objects VS JSON JSON cannot contain functions. JavaScript objects can contain functions. JSON can be created and used by other programming languages. JavaScript objects can only be used in JavaScript.

What is json2?

json2. js: This file creates a JSON property in the global object, if there isn't already one, setting its value to an object containing a stringify method and a parse method.

What is the main difference between a JavaScript object and a JSON object?

JSON is a data interchange format, which just happens to look like a subset of YAML or JavaScript code you can execute and get an object back. A JavaScript object is just an object in JavaScript. With JSON being a data interchange format you can exchange structured data in a textual form with it.

Are all JavaScript objects JSON?

Modern JavaScript engines found in browsers have a native object, also named JSON. This JSON object is able to: Decode a string built using JSON standard, using JSON. parse(string).


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);
    };

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


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