diff --git a/index.js b/index.js index 5a47ca6..8859887 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ /* json2.js - 2011-10-19 + 2014-02-04 Public Domain. @@ -159,7 +159,9 @@ // Create a JSON object only if one does not already exist. We create the // methods in a closure to avoid creating global variables. -var JSON = {}; +if (typeof JSON !== 'object') { + JSON = {}; +} (function () { 'use strict'; @@ -171,7 +173,7 @@ var JSON = {}; if (typeof Date.prototype.toJSON !== 'function') { - Date.prototype.toJSON = function (key) { + Date.prototype.toJSON = function () { return isFinite(this.valueOf()) ? this.getUTCFullYear() + '-' + @@ -185,24 +187,16 @@ var JSON = {}; String.prototype.toJSON = Number.prototype.toJSON = - Boolean.prototype.toJSON = function (key) { + Boolean.prototype.toJSON = function () { return this.valueOf(); }; } - var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, - escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, + var cx, + escapable, gap, indent, - meta = { // table of character substitutions - '\b': '\\b', - '\t': '\\t', - '\n': '\\n', - '\f': '\\f', - '\r': '\\r', - '"' : '\\"', - '\\': '\\\\' - }, + meta, rep; @@ -354,6 +348,16 @@ var JSON = {}; // If the JSON object does not yet have a stringify method, give it one. if (typeof JSON.stringify !== 'function') { + escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g; + meta = { // table of character substitutions + '\b': '\\b', + '\t': '\\t', + '\n': '\\n', + '\f': '\\f', + '\r': '\\r', + '"' : '\\"', + '\\': '\\\\' + }; JSON.stringify = function (value, replacer, space) { // The stringify method takes a value and an optional replacer, and an optional @@ -401,6 +405,7 @@ var JSON = {}; // If the JSON object does not yet have a parse method, give it one. if (typeof JSON.parse !== 'function') { + cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g; JSON.parse = function (text, reviver) { // The parse method takes a text and an optional reviver function, and returns