包详细信息

json3

bestiejs10mMIT3.3.3

A JSON polyfill for older JavaScript platforms.

json, spec, ecma, es5

自述文件

🚨 Unmaintained 🚨

JSON 3 is deprecated and no longer maintained. Please don't use it in new projects, and migrate existing projects to use the native JSON.parse and JSON.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 by Douglas Crockford, the format was standardized in the fifth 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() for serializing a JavaScript value to JSON, and parse() for producing a JavaScript value from a JSON source string. The JSON 3 parser uses recursive descent instead of eval and regular expressions, which makes it slower on older platforms compared to JSON 2. The functions behave exactly as described in the ECMAScript spec, except for the date serialization discrepancy noted below.

The project is hosted on GitHub, along with the unit tests. It is part of the BestieJS 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 define Date#toISOString() or Date#toJSON(). This preserves CommonJS compatibility and avoids polluting native prototypes. Instead, date serialization is performed internally by the stringify() implementation: if a date object does not define a custom toJSON() method, it is serialized as a simplified ISO 8601 date-time string.

Several native Date#toJSON() implementations produce date time strings that do not conform to the grammar outlined in the spec. In these environments, JSON 3 will override the native stringify() implementation. There is an issue on file to make these tests less strict.

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

Usage

Web Browsers

<script src="//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 additional JSON3 object containing the noConflict() and runInContext() functions, as well as aliases to the stringify() and parse() functions.

noConflict and runInContext

  • JSON3.noConflict() restores the original value of the global JSON object and returns a reference to the JSON3 object.
  • JSON3.runInContext([context, exports]) initializes JSON 3 using the given context object (e.g., window, global, etc.), or the global object if omitted. If an exports object is specified, the stringify(), parse(), and runInContext() functions will be attached to it instead of a new object.

Asynchronous Module Loaders

JSON 3 is defined as an anonymous module for compatibility with RequireJS, curl.js, and other asynchronous module loaders.

<script src="//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 global JSON 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 multiple define calls in one script, resulting in errors in AMD loaders. The r.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 been tested with the following web browsers, CommonJS environments, and JavaScript engines.

Web Browsers

CommonJS Environments

JavaScript Engines

  • Mozilla Rhino 1.7R3 and higher
  • WebKit JSC
  • Google V8

Known Incompatibilities

  • Attempting to serialize the arguments object may produce inconsistent results across environments due to specification version differences. As a workaround, please convert the arguments object to an array first: JSON.stringify([].slice.call(arguments, 0)).

Required Native Methods

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

  • The Number, String, Array, Object, Date, SyntaxError, and TypeError constructors.
  • String.fromCharCode
  • Object#toString
  • Object#hasOwnProperty
  • Function#call
  • Math.floor
  • Number#toString
  • Date#valueOf
  • String.prototype: indexOf, charCodeAt, charAt, slice, replace.
  • Array.prototype: push, pop, join.

更新日志

JSON 3 Releases

3.3.2

2014-06-22

  • Test the minified version on Travis [Issue #35].
  • Add a change log and contribution guidelines [Issue #55].
  • Include the minified version in the npm package [Issue #59].
  • Simplify bower.json.

3.3.1

2014-04-08

  • Reduce the Bower package size by lazily downloading the Closure Compiler [Issue #54].
  • Make JSON3.noConflict() idempotent [Issue #56].
  • Improve AMD define pragma detection before minifying.
  • Support node-webkit and web workers.

3.3.0

2014-01-20

  • Export a JSON3 global in browsers and JavaScript engines.
  • Add JSON3.noConflict() and JSON3.runInContext() [Issue #50].
  • Add a post-minification step to remove multiple IIFE wrappers.
  • Optimize quote.

3.2.6

2013-10-25

  • Add Travis CI integration.
  • Support Bower, Component, Jam, and Volo.
  • Test with Node, PhantomJS, RingoJS, Rhino, and Narwhal on Travis.
  • Simplify exports.
  • stringify() optimizations.
  • Add a ?minified query parameter to the browser harness for testing the minified version [Issue #35].
  • Detect trailing comma and trailing decimal extensions in Rhino 1.7R3-R4 [Issue #46].

3.2.5

2013-06-14

  • Use object.hasOwnProperty(prop) instead of Object#hasOwnProperty.call(object, prop) when iterating over host objects [Issue #18].
  • Minification improvements; avoid munging the AMD define pragma [Issue #22; PR #25].
  • Use character codes instead of strings in lex(). Optimize for valid source strings [Issue #23; PR #27].
  • Support Adobe ExtendScript [Issue #29].
  • Handle serializing ExtendScript host objects that throw exceptions [Issue #30; PR #31].
  • Support Browserify and RequireJS by exporting for CommonJS and AMD [PR #33].
  • Use square bracket character access in parse. Add a charIndexBuggy flag.
  • Add a benchmark suite.

3.2.4

2012-10-11

  • Change the export order to prefer module.exports, exports, and then define [PR #14].
  • Avoid conflating duplicate properties and circular references [Issue #15].
  • Export parse and stringify globally even if an AMD loader is present [PR #17].
  • Isolate the feature tests into a has() function for has.js compatibility [Issue #19].

3.2.3

2012-07-13

  • Prototype <= 1.6.1 compatibility [Issue #8].
  • stringify(): Iterate over whitelisted properties in order [Issue #12].
  • Correctly detect trailing commas in array literals.

3.2.2

2012-05-05

  • Correctly detect native parse() implementations in AMD loaders and CommonJS environments [Issue #9].
  • parse(): Use delete instead of Array#splice() when removing elements from traversed arrays [Issue #10].
  • Detect parse() number grammar extensions in IE 9 [Issue #11].

3.2.1

2012-04-26

  • Reduce the file size by removing parse error strings [Issue #5].
  • Fall back to the native stringify() and parse() implementations in AMD loaders and CommonJS environments [Issue #6].
  • Use the correct global object when exporting for browsers and JavaScript engines.
  • Support building on Windows by using zlib instead of shelling out to gzip.
  • Switch to the Closure Compiler for generating the minified version.
  • r.js compatibility.
  • Safari < 2.0.2 and Opera >= 10.53 support.

3.2.0

2012-04-15

  • Override native stringify() implementations to work around date serialization bugs.
  • Ensure the date serialization tests pass in all time zones [Issue #3].
  • Add a workaround for buggy Date#getUTC{FullYear, Month, Date} implementations in Opera > 9.64 [Issue #4].
  • Ensure Firefox <= 11.0 serializes negative years as six-digit extended years.
  • Ensure Safari <= 5.1.5 serializes milliseconds correctly.
  • Add a Node-based build script.
  • Vendor all dependencies.
  • Opera 7.54u2 support.

3.1.0

2012-03-22

  • Switched to bestiejs organisation
  • Added support for a list of properties as the filter argument for JSON.stringify
  • Fixed Firefox 4 and 4.0.1 allowing non-standard extensions to JSON.parse

3.0.0

2012-03-20

  • Renamed JSON3 to JSON
  • Removed JSON3.Version
  • Added minified version of library
  • Created a GitHub Project Page
  • Preserved alphanumeric order when iterating over shadowed properties on objects

0.8.5

2012-03-16

  • Avoided relying on native functions Math.abs, and isFinite, and native constructors String, Number, Object, and Array
  • Fixed AMD export logic

0.8.0

2012-03-15

  • Renamed Prim to JSON3
  • Added JSON3.Version
  • Added support for AMD lodaers as the "json" module
  • Added feature tests for native JSON implementations
  • Added string coercion for the source argument in JSON3.parse
  • Fixed the date serialization routine in JSON3.stringify

0.5.0

2012-02-18

  • Fixed Prim.stringify’s handling of the width argument
  • Added Microsoft’s ES5 Conformance Tests to the test suite

0.2.0

2012-02-17

  • Added Prim.stringify for serializing values
  • Renamed Prim.Escapes to Prim.Unescapes
  • Disallowed unescaped tab characters in strings passed to Prim.parse

0.1.0

2012-02-16

  • Initial release of Prim