diff --git a/json2.js b/json2.js index e66f817..f6fada6 100644 --- a/json2.js +++ b/json2.js @@ -1,10 +1,7 @@ // json2.js -// 2016-10-28 +// 2017-06-12 // Public Domain. // NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. -// See http://www.JSON.org/js.html -// This code should be minified before deployment. -// See http://javascript.crockford.com/jsmin.html // USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO // NOT CONTROL. @@ -113,25 +110,31 @@ // a = // /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); // if (a) { -// return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], -// +a[5], +a[6])); +// return new Date(Date.UTC( +// +a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6] +// )); // } +// return value; // } -// return value; // }); -// myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) { -// var d; -// if (typeof value === "string" && -// value.slice(0, 5) === "Date(" && -// value.slice(-1) === ")") { -// d = new Date(value.slice(5, -1)); -// if (d) { -// return d; +// myData = JSON.parse( +// "[\"Date(09/09/2001)\"]", +// function (key, value) { +// var d; +// if ( +// typeof value === "string" +// && value.slice(0, 5) === "Date(" +// && value.slice(-1) === ")" +// ) { +// d = new Date(value.slice(5, -1)); +// if (d) { +// return d; +// } // } +// return value; // } -// return value; -// }); +// ); // This is a reference implementation. You are free to copy, modify, or // redistribute. @@ -167,7 +170,7 @@ if (typeof JSON !== "object") { function f(n) { // Format integers to have at least two digits. - return n < 10 + return (n < 10) ? "0" + n : n; } @@ -181,12 +184,20 @@ if (typeof JSON !== "object") { Date.prototype.toJSON = function () { return isFinite(this.valueOf()) - ? this.getUTCFullYear() + "-" + - f(this.getUTCMonth() + 1) + "-" + - f(this.getUTCDate()) + "T" + - f(this.getUTCHours()) + ":" + - f(this.getUTCMinutes()) + ":" + - f(this.getUTCSeconds()) + "Z" + ? ( + this.getUTCFullYear() + + "-" + + f(this.getUTCMonth() + 1) + + "-" + + f(this.getUTCDate()) + + "T" + + f(this.getUTCHours()) + + ":" + + f(this.getUTCMinutes()) + + ":" + + f(this.getUTCSeconds()) + + "Z" + ) : null; }; @@ -234,8 +245,11 @@ if (typeof JSON !== "object") { // If the value has a toJSON method, call it to obtain a replacement value. - if (value && typeof value === "object" && - typeof value.toJSON === "function") { + if ( + value + && typeof value === "object" + && typeof value.toJSON === "function" + ) { value = value.toJSON(key); } @@ -256,7 +270,7 @@ if (typeof JSON !== "object") { // JSON numbers must be finite. Encode non-finite numbers as null. - return isFinite(value) + return (isFinite(value)) ? String(value) : "null"; @@ -304,7 +318,14 @@ if (typeof JSON !== "object") { v = partial.length === 0 ? "[]" : gap - ? "[\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "]" + ? ( + "[\n" + + gap + + partial.join(",\n" + gap) + + "\n" + + mind + + "]" + ) : "[" + partial.join(",") + "]"; gap = mind; return v; @@ -320,7 +341,7 @@ if (typeof JSON !== "object") { v = str(k, value); if (v) { partial.push(quote(k) + ( - gap + (gap) ? ": " : ":" ) + v); @@ -336,7 +357,7 @@ if (typeof JSON !== "object") { v = str(k, value); if (v) { partial.push(quote(k) + ( - gap + (gap) ? ": " : ":" ) + v); @@ -400,9 +421,10 @@ if (typeof JSON !== "object") { // Otherwise, throw an error. rep = replacer; - if (replacer && typeof replacer !== "function" && - (typeof replacer !== "object" || - typeof replacer.length !== "number")) { + if (replacer && typeof replacer !== "function" && ( + typeof replacer !== "object" + || typeof replacer.length !== "number" + )) { throw new Error("JSON.stringify"); } @@ -456,8 +478,10 @@ if (typeof JSON !== "object") { rx_dangerous.lastIndex = 0; if (rx_dangerous.test(text)) { text = text.replace(rx_dangerous, function (a) { - return "\\u" + - ("0000" + a.charCodeAt(0).toString(16)).slice(-4); + return ( + "\\u" + + ("0000" + a.charCodeAt(0).toString(16)).slice(-4) + ); }); }