Movatterモバイル変換


[0]ホーム

URL:


🚨 Unmaintained 🚨

JSON 3 isdeprecated andno longer maintained. Please don’t use it in new projects, and migrate existing projects to use the nativeJSON.parse andJSON.stringify instead.

Thanks to everyone who contributed patches or found it useful! ❤️

JSON 3

No Maintenance Intended

JSON 3 was a JSON polyfill for older JavaScript platforms.

About

JSON is a language-independent data interchange format based on a loose subset of the JavaScript grammar. Originally popularized byDouglas Crockford, the format was standardized in thefifth edition of the ECMAScript specification. The 5.1 edition, ratified in June 2011, incorporates several modifications to the grammar pertaining to the serialization of dates.

JSON 3 exposes two functions:stringify() forserializing a JavaScript value to JSON, andparse() forproducing a JavaScript value from a JSON source string. The JSON 3 parser uses recursive descent instead ofeval and regular expressions, which makes it slower on older platforms compared toJSON 2. The functions behave exactly as described in the ECMAScript spec,except for the date serialization discrepancy noted below.

The project ishosted on GitHub, along with theunit tests. It is part of theBestieJS family, a collection of best-in-class JavaScript libraries that promote cross-platform support, specification precedents, unit testing, and plenty of documentation.

Date Serialization

JSON 3 deviates from the specification in one important way: it does not defineDate#toISOString() orDate#toJSON(). This preserves CommonJS compatibility and avoids polluting native prototypes. Instead, date serialization is performed internally by thestringify() implementation: if a date object does not define a customtoJSON() method, it is serialized as asimplified ISO 8601 date-time string.

Several nativeDate#toJSON() implementations produce date time strings that donot conform to the grammar outlined in the spec. In these environments, JSON 3 will override the nativestringify() implementation. There is anissue on file to make these tests less strict.

Portions of the date serialization code are adapted from thedate-shim project.

Usage

Web Browsers

<scriptsrc="//cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script><script>JSON.stringify({"Hello":123});// => '{"Hello":123}'JSON.parse("[[1, 2, 3], 1, 2, 3, 4]",function(key, value){if (typeof value =="number") {      value = value %2 ?"Odd" :"Even";    }return value;  });// => [["Odd", "Even", "Odd"], "Odd", "Even", "Odd", "Even"]</script>

When used in a web browser, JSON 3 exposes an additionalJSON3 object containing thenoConflict() andrunInContext() functions, as well as aliases to thestringify() andparse() functions.

noConflict andrunInContext

Asynchronous Module Loaders

JSON 3 is defined as ananonymous module for compatibility withRequireJS,curl.js, and other asynchronous module loaders.

<scriptsrc="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.js"></script><script>require({"paths": {"json3":"./path/to/json3"    }  }, ["json3"],function(JSON){JSON.parse("[1, 2, 3]");// => [1, 2, 3]  });</script>

To avoid issues with third-party scripts,JSON 3 is exported to the global scope even when used with a module loader. If this behavior is undesired,JSON3.noConflict() can be used to restore the globalJSON object to its original value.

Note: If you intend to use JSON3 alongside another module,please do not simply concatenate these modules together, as that would cause multipledefine calls in one script, resulting in errors in AMD loaders. Ther.js build optimizer can be used instead if you need a single compressed file for production.

CommonJS Environments

var JSON3 = require("./path/to/json3");JSON3.parse("[1, 2, 3]");// => [1, 2, 3]

JavaScript Engines

load("path/to/json3.js");JSON.stringify({"Hello":123,"Good-bye":456}, ["Hello"],"\t");// =>'{\n\t"Hello": 123\n}'

Compatibility

JSON 3 has beentested with the following web browsers, CommonJS environments, and JavaScript engines.

Web Browsers

CommonJS Environments

JavaScript Engines

Known Incompatibilities

Required Native Methods

JSON 3 assumes that the following methods exist and function as described in the ECMAScript specification:

© 2012-2015Kit Cambridge,Benjamin Tan.


[8]ページ先頭

©2009-2025 Movatter.jp