Skip to content

Commit f1e5b9b

Browse files
committed
Updated json2.js
1 parent 51b7dc6 commit f1e5b9b

1 file changed

Lines changed: 64 additions & 56 deletions

File tree

code_comments/htdocs/json2.js

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
http://www.JSON.org/json2.js
3-
2009-09-29
2+
json2.js
3+
2014-02-04
44
55
Public Domain.
66
@@ -146,7 +146,7 @@
146146
redistribute.
147147
*/
148148

149-
/*jslint evil: true, strict: false */
149+
/*jslint evil: true, regexp: true */
150150

151151
/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
152152
call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
@@ -159,11 +159,12 @@
159159
// Create a JSON object only if one does not already exist. We create the
160160
// methods in a closure to avoid creating global variables.
161161

162-
if (!this.JSON) {
163-
this.JSON = {};
162+
if (typeof JSON !== 'object') {
163+
JSON = {};
164164
}
165165

166166
(function () {
167+
'use strict';
167168

168169
function f(n) {
169170
// Format integers to have at least two digits.
@@ -172,37 +173,30 @@ if (!this.JSON) {
172173

173174
if (typeof Date.prototype.toJSON !== 'function') {
174175

175-
Date.prototype.toJSON = function (key) {
176+
Date.prototype.toJSON = function () {
176177

177-
return isFinite(this.valueOf()) ?
178-
this.getUTCFullYear() + '-' +
179-
f(this.getUTCMonth() + 1) + '-' +
180-
f(this.getUTCDate()) + 'T' +
181-
f(this.getUTCHours()) + ':' +
182-
f(this.getUTCMinutes()) + ':' +
183-
f(this.getUTCSeconds()) + 'Z' : null;
178+
return isFinite(this.valueOf())
179+
? this.getUTCFullYear() + '-' +
180+
f(this.getUTCMonth() + 1) + '-' +
181+
f(this.getUTCDate()) + 'T' +
182+
f(this.getUTCHours()) + ':' +
183+
f(this.getUTCMinutes()) + ':' +
184+
f(this.getUTCSeconds()) + 'Z'
185+
: null;
184186
};
185187

186-
String.prototype.toJSON =
187-
Number.prototype.toJSON =
188-
Boolean.prototype.toJSON = function (key) {
189-
return this.valueOf();
190-
};
188+
String.prototype.toJSON =
189+
Number.prototype.toJSON =
190+
Boolean.prototype.toJSON = function () {
191+
return this.valueOf();
192+
};
191193
}
192194

193-
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
194-
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
195+
var cx,
196+
escapable,
195197
gap,
196198
indent,
197-
meta = { // table of character substitutions
198-
'\b': '\\b',
199-
'\t': '\\t',
200-
'\n': '\\n',
201-
'\f': '\\f',
202-
'\r': '\\r',
203-
'"' : '\\"',
204-
'\\': '\\\\'
205-
},
199+
meta,
206200
rep;
207201

208202

@@ -214,13 +208,12 @@ if (!this.JSON) {
214208
// sequences.
215209

216210
escapable.lastIndex = 0;
217-
return escapable.test(string) ?
218-
'"' + string.replace(escapable, function (a) {
219-
var c = meta[a];
220-
return typeof c === 'string' ? c :
221-
'\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
222-
}) + '"' :
223-
'"' + string + '"';
211+
return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
212+
var c = meta[a];
213+
return typeof c === 'string'
214+
? c
215+
: '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
216+
}) + '"' : '"' + string + '"';
224217
}
225218

226219

@@ -303,11 +296,11 @@ if (!this.JSON) {
303296
// Join all of the elements together, separated with commas, and wrap them in
304297
// brackets.
305298

306-
v = partial.length === 0 ? '[]' :
307-
gap ? '[\n' + gap +
308-
partial.join(',\n' + gap) + '\n' +
309-
mind + ']' :
310-
'[' + partial.join(',') + ']';
299+
v = partial.length === 0
300+
? '[]'
301+
: gap
302+
? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
303+
: '[' + partial.join(',') + ']';
311304
gap = mind;
312305
return v;
313306
}
@@ -317,8 +310,8 @@ if (!this.JSON) {
317310
if (rep && typeof rep === 'object') {
318311
length = rep.length;
319312
for (i = 0; i < length; i += 1) {
320-
k = rep[i];
321-
if (typeof k === 'string') {
313+
if (typeof rep[i] === 'string') {
314+
k = rep[i];
322315
v = str(k, value);
323316
if (v) {
324317
partial.push(quote(k) + (gap ? ': ' : ':') + v);
@@ -330,7 +323,7 @@ if (!this.JSON) {
330323
// Otherwise, iterate through all of the keys in the object.
331324

332325
for (k in value) {
333-
if (Object.hasOwnProperty.call(value, k)) {
326+
if (Object.prototype.hasOwnProperty.call(value, k)) {
334327
v = str(k, value);
335328
if (v) {
336329
partial.push(quote(k) + (gap ? ': ' : ':') + v);
@@ -342,9 +335,11 @@ if (!this.JSON) {
342335
// Join all of the member texts together, separated with commas,
343336
// and wrap them in braces.
344337

345-
v = partial.length === 0 ? '{}' :
346-
gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
347-
mind + '}' : '{' + partial.join(',') + '}';
338+
v = partial.length === 0
339+
? '{}'
340+
: gap
341+
? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
342+
: '{' + partial.join(',') + '}';
348343
gap = mind;
349344
return v;
350345
}
@@ -353,6 +348,16 @@ if (!this.JSON) {
353348
// If the JSON object does not yet have a stringify method, give it one.
354349

355350
if (typeof JSON.stringify !== 'function') {
351+
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
352+
meta = { // table of character substitutions
353+
'\b': '\\b',
354+
'\t': '\\t',
355+
'\n': '\\n',
356+
'\f': '\\f',
357+
'\r': '\\r',
358+
'"' : '\\"',
359+
'\\': '\\\\'
360+
};
356361
JSON.stringify = function (value, replacer, space) {
357362

358363
// The stringify method takes a value and an optional replacer, and an optional
@@ -385,7 +390,7 @@ if (!this.JSON) {
385390
rep = replacer;
386391
if (replacer && typeof replacer !== 'function' &&
387392
(typeof replacer !== 'object' ||
388-
typeof replacer.length !== 'number')) {
393+
typeof replacer.length !== 'number')) {
389394
throw new Error('JSON.stringify');
390395
}
391396

@@ -400,6 +405,7 @@ if (!this.JSON) {
400405
// If the JSON object does not yet have a parse method, give it one.
401406

402407
if (typeof JSON.parse !== 'function') {
408+
cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
403409
JSON.parse = function (text, reviver) {
404410

405411
// The parse method takes a text and an optional reviver function, and returns
@@ -415,7 +421,7 @@ if (!this.JSON) {
415421
var k, v, value = holder[key];
416422
if (value && typeof value === 'object') {
417423
for (k in value) {
418-
if (Object.hasOwnProperty.call(value, k)) {
424+
if (Object.prototype.hasOwnProperty.call(value, k)) {
419425
v = walk(value, k);
420426
if (v !== undefined) {
421427
value[k] = v;
@@ -433,6 +439,7 @@ if (!this.JSON) {
433439
// Unicode characters with escape sequences. JavaScript handles many characters
434440
// incorrectly, either silently deleting them, or treating them as line endings.
435441

442+
text = String(text);
436443
cx.lastIndex = 0;
437444
if (cx.test(text)) {
438445
text = text.replace(cx, function (a) {
@@ -454,10 +461,10 @@ if (!this.JSON) {
454461
// we look to see that the remaining characters are only whitespace or ']' or
455462
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
456463

457-
if (/^[\],:{}\s]*$/.
458-
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
459-
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
460-
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
464+
if (/^[\],:{}\s]*$/
465+
.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
466+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
467+
.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
461468

462469
// In the third stage we use the eval function to compile the text into a
463470
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
@@ -469,13 +476,14 @@ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
469476
// In the optional fourth stage, we recursively walk the new structure, passing
470477
// each name/value pair to a reviver function for possible transformation.
471478

472-
return typeof reviver === 'function' ?
473-
walk({'': j}, '') : j;
479+
return typeof reviver === 'function'
480+
? walk({'': j}, '')
481+
: j;
474482
}
475483

476484
// If the text is not JSON parseable, then a SyntaxError is thrown.
477485

478486
throw new SyntaxError('JSON.parse');
479487
};
480488
}
481-
}());
489+
}());

0 commit comments

Comments
 (0)