JSON in JavaScript library updated

...why we now need a JSON library... _in_ Javascript?!
Source: http://www.json.org/js.html
This commit is contained in:
Mikael Nordfeldth 2015-03-07 00:57:21 +01:00
parent a6c03e3127
commit 395c2cc075

View File

@ -1,6 +1,6 @@
/* /*
json2.js json2.js
2013-05-26 2015-02-25
Public Domain. Public Domain.
@ -48,7 +48,9 @@
Date.prototype.toJSON = function (key) { Date.prototype.toJSON = function (key) {
function f(n) { function f(n) {
// Format integers to have at least two digits. // Format integers to have at least two digits.
return n < 10 ? '0' + n : n; return n < 10
? '0' + n
: n;
} }
return this.getUTCFullYear() + '-' + return this.getUTCFullYear() + '-' +
@ -146,10 +148,12 @@
redistribute. redistribute.
*/ */
/*jslint evil: true, regexp: true */ /*jslint
eval, for, this
*/
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, /*property
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
lastIndex, length, parse, prototype, push, replace, slice, stringify, lastIndex, length, parse, prototype, push, replace, slice, stringify,
test, toJSON, toString, valueOf test, toJSON, toString, valueOf
@ -168,7 +172,13 @@ if (typeof JSON !== 'object') {
function f(n) { function f(n) {
// Format integers to have at least two digits. // Format integers to have at least two digits.
return n < 10 ? '0' + n : n; return n < 10
? '0' + n
: n;
}
function this_value() {
return this.valueOf();
} }
if (typeof Date.prototype.toJSON !== 'function') { if (typeof Date.prototype.toJSON !== 'function') {
@ -176,35 +186,25 @@ if (typeof JSON !== 'object') {
Date.prototype.toJSON = function () { Date.prototype.toJSON = function () {
return isFinite(this.valueOf()) return isFinite(this.valueOf())
? this.getUTCFullYear() + '-' + ? this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' + f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' + f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' + f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' + f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z' f(this.getUTCSeconds()) + 'Z'
: null; : null;
}; };
String.prototype.toJSON = Boolean.prototype.toJSON = this_value;
Number.prototype.toJSON = Number.prototype.toJSON = this_value;
Boolean.prototype.toJSON = function () { String.prototype.toJSON = this_value;
return this.valueOf();
};
} }
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, var cx,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, escapable,
gap, gap,
indent, indent,
meta = { // table of character substitutions meta,
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
rep; rep;
@ -216,12 +216,14 @@ if (typeof JSON !== 'object') {
// sequences. // sequences.
escapable.lastIndex = 0; escapable.lastIndex = 0;
return escapable.test(string) ? '"' + string.replace(escapable, function (a) { return escapable.test(string)
? '"' + string.replace(escapable, function (a) {
var c = meta[a]; var c = meta[a];
return typeof c === 'string' return typeof c === 'string'
? c ? c
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}) + '"' : '"' + string + '"'; }) + '"'
: '"' + string + '"';
} }
@ -261,7 +263,9 @@ if (typeof JSON !== 'object') {
// JSON numbers must be finite. Encode non-finite numbers as null. // JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(value) ? String(value) : 'null'; return isFinite(value)
? String(value)
: 'null';
case 'boolean': case 'boolean':
case 'null': case 'null':
@ -305,10 +309,10 @@ if (typeof JSON !== 'object') {
// brackets. // brackets.
v = partial.length === 0 v = partial.length === 0
? '[]' ? '[]'
: gap : gap
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
: '[' + partial.join(',') + ']'; : '[' + partial.join(',') + ']';
gap = mind; gap = mind;
return v; return v;
} }
@ -322,7 +326,11 @@ if (typeof JSON !== 'object') {
k = rep[i]; k = rep[i];
v = str(k, value); v = str(k, value);
if (v) { if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v); partial.push(quote(k) + (
gap
? ': '
: ':'
) + v);
} }
} }
} }
@ -334,7 +342,11 @@ if (typeof JSON !== 'object') {
if (Object.prototype.hasOwnProperty.call(value, k)) { if (Object.prototype.hasOwnProperty.call(value, k)) {
v = str(k, value); v = str(k, value);
if (v) { if (v) {
partial.push(quote(k) + (gap ? ': ' : ':') + v); partial.push(quote(k) + (
gap
? ': '
: ':'
) + v);
} }
} }
} }
@ -344,10 +356,10 @@ if (typeof JSON !== 'object') {
// and wrap them in braces. // and wrap them in braces.
v = partial.length === 0 v = partial.length === 0
? '{}' ? '{}'
: gap : gap
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
: '{' + partial.join(',') + '}'; : '{' + partial.join(',') + '}';
gap = mind; gap = mind;
return v; return v;
} }
@ -356,6 +368,16 @@ if (typeof JSON !== 'object') {
// If the JSON object does not yet have a stringify method, give it one. // If the JSON object does not yet have a stringify method, give it one.
if (typeof JSON.stringify !== 'function') { if (typeof JSON.stringify !== 'function') {
escapable = /[\\\"\u0000-\u001f\u007f-\u009f\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) { JSON.stringify = function (value, replacer, space) {
// The stringify method takes a value and an optional replacer, and an optional // The stringify method takes a value and an optional replacer, and an optional
@ -403,6 +425,7 @@ if (typeof JSON !== 'object') {
// If the JSON object does not yet have a parse method, give it one. // If the JSON object does not yet have a parse method, give it one.
if (typeof JSON.parse !== 'function') { 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) { JSON.parse = function (text, reviver) {
// The parse method takes a text and an optional reviver function, and returns // The parse method takes a text and an optional reviver function, and returns
@ -441,7 +464,7 @@ if (typeof JSON !== 'object') {
if (cx.test(text)) { if (cx.test(text)) {
text = text.replace(cx, function (a) { text = text.replace(cx, function (a) {
return '\\u' + return '\\u' +
('0000' + a.charCodeAt(0).toString(16)).slice(-4); ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
}); });
} }
@ -458,10 +481,13 @@ if (typeof JSON !== 'object') {
// we look to see that the remaining characters are only whitespace or ']' or // we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/ if (
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') /^[\],:{}\s]*$/.test(
text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { .replace(/(?:^|:|,)(?:\s*\[)+/g, '')
)
) {
// In the third stage we use the eval function to compile the text into a // 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 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
@ -474,8 +500,8 @@ if (typeof JSON !== 'object') {
// each name/value pair to a reviver function for possible transformation. // each name/value pair to a reviver function for possible transformation.
return typeof reviver === 'function' return typeof reviver === 'function'
? walk({'': j}, '') ? walk({'': j}, '')
: j; : j;
} }
// If the text is not JSON parseable, then a SyntaxError is thrown. // If the text is not JSON parseable, then a SyntaxError is thrown.