From 633fe5ae4bd69985254a68ae111505ec3d3e74ab Mon Sep 17 00:00:00 2001 From: Douglas Crockford Date: Thu, 20 Jan 2011 10:47:02 -0800 Subject: [PATCH] Hygiene --- cycle.js | 6 ++-- json.js | 82 +++++++++++++++++++++------------------------ json2.js | 63 +++++++++++++++++----------------- json_parse.js | 2 +- json_parse_state.js | 2 +- 5 files changed, 74 insertions(+), 81 deletions(-) diff --git a/cycle.js b/cycle.js index a6e0fa5..c5ef2a8 100755 --- a/cycle.js +++ b/cycle.js @@ -1,5 +1,5 @@ // cycle.js -// 2010-11-18 +// 2011-01-18 /*jslint forin: true, evil: true */ @@ -76,7 +76,7 @@ if (typeof JSON.decycle !== 'function') { for (name in value) { if (Object.hasOwnProperty.call(value, name)) { nu[name] = derez(value[name], - path + '[' + JSON.stringify(name) + ']'); + path + '[' + JSON.stringify(name) + ']'); } } } @@ -114,7 +114,7 @@ if (typeof JSON.retrocycle !== 'function') { // produces an array containing a single element which is the array itself. var px = -/^\$(?:\[(?:\d?|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/; + /^\$(?:\[(?:\d?|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/; (function rez(value) { diff --git a/json.js b/json.js index ea2377f..4018ed0 100755 --- a/json.js +++ b/json.js @@ -1,6 +1,6 @@ /* json.js - 2010-12-08 + 2011-01-18 Public Domain @@ -196,8 +196,9 @@ // Create a JSON object only if one does not already exist. We create the // methods in a closure to avoid creating global variables. -if (!this.JSON) { - this.JSON = {}; +var JSON; +if (!JSON) { + JSON = {}; } (function () { @@ -213,19 +214,19 @@ if (!this.JSON) { Date.prototype.toJSON = function (key) { return isFinite(this.valueOf()) ? - this.getUTCFullYear() + '-' + - f(this.getUTCMonth() + 1) + '-' + - f(this.getUTCDate()) + 'T' + - f(this.getUTCHours()) + ':' + - f(this.getUTCMinutes()) + ':' + - f(this.getUTCSeconds()) + 'Z' : null; + this.getUTCFullYear() + '-' + + f(this.getUTCMonth() + 1) + '-' + + f(this.getUTCDate()) + 'T' + + f(this.getUTCHours()) + ':' + + f(this.getUTCMinutes()) + ':' + + f(this.getUTCSeconds()) + 'Z' : null; }; - String.prototype.toJSON = - Number.prototype.toJSON = - Boolean.prototype.toJSON = function (key) { - return this.valueOf(); - }; + String.prototype.toJSON = + Number.prototype.toJSON = + Boolean.prototype.toJSON = function (key) { + return this.valueOf(); + }; } var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, @@ -252,13 +253,11 @@ if (!this.JSON) { // sequences. escapable.lastIndex = 0; - return escapable.test(string) ? - '"' + string.replace(escapable, function (a) { - var c = meta[a]; - return typeof c === 'string' ? c : - '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); - }) + '"' : - '"' + string + '"'; + return escapable.test(string) ? '"' + string.replace(escapable, function (a) { + var c = meta[a]; + return typeof c === 'string' ? c : + '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); + }) + '"' : '"' + string + '"'; } @@ -341,11 +340,9 @@ if (!this.JSON) { // Join all of the elements together, separated with commas, and wrap them in // brackets. - v = partial.length === 0 ? '[]' : - gap ? '[\n' + gap + - partial.join(',\n' + gap) + '\n' + - mind + ']' : - '[' + partial.join(',') + ']'; + v = partial.length === 0 ? '[]' : gap ? + '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : + '[' + partial.join(',') + ']'; gap = mind; return v; } @@ -380,9 +377,9 @@ if (!this.JSON) { // Join all of the member texts together, separated with commas, // and wrap them in braces. - v = partial.length === 0 ? '{}' : - gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + - mind + '}' : '{' + partial.join(',') + '}'; + v = partial.length === 0 ? '{}' : gap ? + '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : + '{' + partial.join(',') + '}'; gap = mind; return v; } @@ -423,7 +420,7 @@ if (!this.JSON) { rep = replacer; if (replacer && typeof replacer !== 'function' && (typeof replacer !== 'object' || - typeof replacer.length !== 'number')) { + typeof replacer.length !== 'number')) { throw new Error('JSON.stringify'); } @@ -494,9 +491,9 @@ if (!this.JSON) { // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. if (/^[\],:{}\s]*$/ -.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') -.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') -.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { + .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') + .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { // In the third stage we use the eval function to compile the text into a // JavaScript structure. The '{' operator is subject to a syntactic ambiguity @@ -517,18 +514,17 @@ if (!this.JSON) { throw new SyntaxError('JSON.parse'); }; } -}()); - // 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); - }; -} + if (!Object.prototype.toJSONString) { + Object.prototype.toJSONString = function (filter) { + return JSON.stringify(this, filter); + }; + Object.prototype.parseJSON = function (filter) { + return JSON.parse(this, filter); + }; + } +}()); diff --git a/json2.js b/json2.js index 22b44d9..36d3dc3 100755 --- a/json2.js +++ b/json2.js @@ -1,6 +1,6 @@ /* http://www.JSON.org/json2.js - 2010-11-17 + 2011-01-18 Public Domain. @@ -159,8 +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. -if (!this.JSON) { - this.JSON = {}; +var JSON; +if (!JSON) { + JSON = {}; } (function () { @@ -176,19 +177,19 @@ if (!this.JSON) { Date.prototype.toJSON = function (key) { return isFinite(this.valueOf()) ? - this.getUTCFullYear() + '-' + - f(this.getUTCMonth() + 1) + '-' + - f(this.getUTCDate()) + 'T' + - f(this.getUTCHours()) + ':' + - f(this.getUTCMinutes()) + ':' + - f(this.getUTCSeconds()) + 'Z' : null; + this.getUTCFullYear() + '-' + + f(this.getUTCMonth() + 1) + '-' + + f(this.getUTCDate()) + 'T' + + f(this.getUTCHours()) + ':' + + f(this.getUTCMinutes()) + ':' + + f(this.getUTCSeconds()) + 'Z' : null; }; - String.prototype.toJSON = - Number.prototype.toJSON = - Boolean.prototype.toJSON = function (key) { - return this.valueOf(); - }; + String.prototype.toJSON = + Number.prototype.toJSON = + Boolean.prototype.toJSON = function (key) { + return this.valueOf(); + }; } var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, @@ -215,13 +216,11 @@ if (!this.JSON) { // sequences. escapable.lastIndex = 0; - return escapable.test(string) ? - '"' + string.replace(escapable, function (a) { - var c = meta[a]; - return typeof c === 'string' ? c : - '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); - }) + '"' : - '"' + string + '"'; + return escapable.test(string) ? '"' + string.replace(escapable, function (a) { + var c = meta[a]; + return typeof c === 'string' ? c : + '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); + }) + '"' : '"' + string + '"'; } @@ -304,11 +303,9 @@ if (!this.JSON) { // Join all of the elements together, separated with commas, and wrap them in // brackets. - v = partial.length === 0 ? '[]' : - gap ? '[\n' + gap + - partial.join(',\n' + gap) + '\n' + - mind + ']' : - '[' + partial.join(',') + ']'; + v = partial.length === 0 ? '[]' : gap ? + '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : + '[' + partial.join(',') + ']'; gap = mind; return v; } @@ -343,9 +340,9 @@ if (!this.JSON) { // Join all of the member texts together, separated with commas, // and wrap them in braces. - v = partial.length === 0 ? '{}' : - gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + - mind + '}' : '{' + partial.join(',') + '}'; + v = partial.length === 0 ? '{}' : gap ? + '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : + '{' + partial.join(',') + '}'; gap = mind; return v; } @@ -386,7 +383,7 @@ if (!this.JSON) { rep = replacer; if (replacer && typeof replacer !== 'function' && (typeof replacer !== 'object' || - typeof replacer.length !== 'number')) { + typeof replacer.length !== 'number')) { throw new Error('JSON.stringify'); } @@ -457,9 +454,9 @@ if (!this.JSON) { // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. if (/^[\],:{}\s]*$/ -.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') -.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') -.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { + .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') + .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') + .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { // In the third stage we use the eval function to compile the text into a // JavaScript structure. The '{' operator is subject to a syntactic ambiguity diff --git a/json_parse.js b/json_parse.js index ca120c5..c712d37 100755 --- a/json_parse.js +++ b/json_parse.js @@ -1,6 +1,6 @@ /* http://www.JSON.org/json_parse.js - 2009-05-31 + 2011-01-18 Public Domain. diff --git a/json_parse_state.js b/json_parse_state.js index a2a9ea7..f474805 100755 --- a/json_parse_state.js +++ b/json_parse_state.js @@ -1,6 +1,6 @@ /* http://www.JSON.org/json_parse_state.js - 2009-05-31 + 2011-01-18 Public Domain.