1
1
/*
2
2
json.js
3
- 2014-02-04
3
+ 2014-02-25
4
4
5
5
Public Domain
6
6
183
183
redistribute.
184
184
*/
185
185
186
- /*jslint evil: true, regexp: true, unparam: true */
186
+ /*jslint eval, for, this */
187
187
188
- /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
189
- call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
188
+ /*property
189
+ JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
190
190
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
191
191
lastIndex, length, parse, parseJSON, prototype, push, replace, slice,
192
192
stringify, test, toJSON, toJSONString, toString, valueOf
193
193
*/
194
194
195
-
196
195
// Create a JSON object only if one does not already exist. We create the
197
196
// methods in a closure to avoid creating global variables.
198
197
@@ -205,28 +204,32 @@ if (typeof JSON !== 'object') {
205
204
206
205
function f ( n ) {
207
206
// Format integers to have at least two digits.
208
- return n < 10 ? '0' + n : n ;
207
+ return n < 10
208
+ ? '0' + n
209
+ : n ;
210
+ }
211
+
212
+ function this_value ( ) {
213
+ return this . valueOf ( ) ;
209
214
}
210
215
211
216
if ( typeof Date . prototype . toJSON !== 'function' ) {
212
217
213
- Date . prototype . toJSON = function ( key ) {
218
+ Date . prototype . toJSON = function ( ignore ) {
214
219
215
220
return isFinite ( this . valueOf ( ) )
216
- ? this . getUTCFullYear ( ) + '-' +
221
+ ? this . getUTCFullYear ( ) + '-' +
217
222
f ( this . getUTCMonth ( ) + 1 ) + '-' +
218
- f ( this . getUTCDate ( ) ) + 'T' +
219
- f ( this . getUTCHours ( ) ) + ':' +
220
- f ( this . getUTCMinutes ( ) ) + ':' +
221
- f ( this . getUTCSeconds ( ) ) + 'Z'
222
- : null ;
223
+ f ( this . getUTCDate ( ) ) + 'T' +
224
+ f ( this . getUTCHours ( ) ) + ':' +
225
+ f ( this . getUTCMinutes ( ) ) + ':' +
226
+ f ( this . getUTCSeconds ( ) ) + 'Z'
227
+ : null ;
223
228
} ;
224
229
225
- String . prototype . toJSON =
226
- Number . prototype . toJSON =
227
- Boolean . prototype . toJSON = function ( key ) {
228
- return this . valueOf ( ) ;
229
- } ;
230
+ Boolean . prototype . toJSON = this_value ;
231
+ Number . prototype . toJSON = this_value ;
232
+ String . prototype . toJSON = this_value ;
230
233
}
231
234
232
235
var cx ,
@@ -245,12 +248,14 @@ if (typeof JSON !== 'object') {
245
248
// sequences.
246
249
247
250
escapable . lastIndex = 0 ;
248
- return escapable . test ( string ) ? '"' + string . replace ( escapable , function ( a ) {
251
+ return escapable . test ( string )
252
+ ? '"' + string . replace ( escapable , function ( a ) {
249
253
var c = meta [ a ] ;
250
254
return typeof c === 'string'
251
- ? c
252
- : '\\u' + ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
253
- } ) + '"' : '"' + string + '"' ;
255
+ ? c
256
+ : '\\u' + ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
257
+ } ) + '"'
258
+ : '"' + string + '"' ;
254
259
}
255
260
256
261
@@ -290,7 +295,9 @@ if (typeof JSON !== 'object') {
290
295
291
296
// JSON numbers must be finite. Encode non-finite numbers as null.
292
297
293
- return isFinite ( value ) ? String ( value ) : 'null' ;
298
+ return isFinite ( value )
299
+ ? String ( value )
300
+ : 'null' ;
294
301
295
302
case 'boolean' :
296
303
case 'null' :
@@ -334,10 +341,10 @@ if (typeof JSON !== 'object') {
334
341
// brackets.
335
342
336
343
v = partial . length === 0
337
- ? '[]'
338
- : gap
339
- ? '[\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + ']'
340
- : '[' + partial . join ( ',' ) + ']' ;
344
+ ? '[]'
345
+ : gap
346
+ ? '[\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + ']'
347
+ : '[' + partial . join ( ',' ) + ']' ;
341
348
gap = mind ;
342
349
return v ;
343
350
}
@@ -351,7 +358,9 @@ if (typeof JSON !== 'object') {
351
358
if ( typeof k === 'string' ) {
352
359
v = str ( k , value ) ;
353
360
if ( v ) {
354
- partial . push ( quote ( k ) + ( gap ? ': ' : ':' ) + v ) ;
361
+ partial . push ( quote ( k ) + ( gap
362
+ ? ': '
363
+ : ':' ) + v ) ;
355
364
}
356
365
}
357
366
}
@@ -363,7 +372,9 @@ if (typeof JSON !== 'object') {
363
372
if ( Object . prototype . hasOwnProperty . call ( value , k ) ) {
364
373
v = str ( k , value ) ;
365
374
if ( v ) {
366
- partial . push ( quote ( k ) + ( gap ? ': ' : ':' ) + v ) ;
375
+ partial . push ( quote ( k ) + ( gap
376
+ ? ': '
377
+ : ':' ) + v ) ;
367
378
}
368
379
}
369
380
}
@@ -372,10 +383,11 @@ if (typeof JSON !== 'object') {
372
383
// Join all of the member texts together, separated with commas,
373
384
// and wrap them in braces.
374
385
375
- v = partial . length === 0 ? '{}'
376
- : gap
377
- ? '{\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + '}'
378
- : '{' + partial . join ( ',' ) + '}' ;
386
+ v = partial . length === 0
387
+ ? '{}'
388
+ : gap
389
+ ? '{\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + '}'
390
+ : '{' + partial . join ( ',' ) + '}' ;
379
391
gap = mind ;
380
392
return v ;
381
393
}
@@ -384,14 +396,14 @@ if (typeof JSON !== 'object') {
384
396
// If the JSON object does not yet have a stringify method, give it one.
385
397
386
398
if ( typeof JSON . stringify !== 'function' ) {
387
- escapable = / [ \\ \" \x00 - \x1f \x7f - \x9f \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g;
399
+ escapable = / [ \\ \" \u0000 - \u001f \u007f - \u009f \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g;
388
400
meta = { // table of character substitutions
389
401
'\b' : '\\b' ,
390
402
'\t' : '\\t' ,
391
403
'\n' : '\\n' ,
392
404
'\f' : '\\f' ,
393
405
'\r' : '\\r' ,
394
- '"' : '\\"' ,
406
+ '"' : '\\"' ,
395
407
'\\' : '\\\\'
396
408
} ;
397
409
JSON . stringify = function ( value , replacer , space ) {
@@ -480,7 +492,7 @@ if (typeof JSON !== 'object') {
480
492
if ( cx . test ( text ) ) {
481
493
text = text . replace ( cx , function ( a ) {
482
494
return '\\u' +
483
- ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
495
+ ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
484
496
} ) ;
485
497
}
486
498
@@ -497,10 +509,11 @@ if (typeof JSON !== 'object') {
497
509
// we look to see that the remaining characters are only whitespace or ']' or
498
510
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
499
511
500
- if ( / ^ [ \] , : { } \s ] * $ /
501
- . test ( text . replace ( / \\ (?: [ " \\ \/ b f n r t ] | u [ 0 - 9 a - f A - F ] { 4 } ) / g, '@' )
502
- . replace ( / " [ ^ " \\ \n \r ] * " | t r u e | f a l s e | n u l l | - ? \d + (?: \. \d * ) ? (?: [ e E ] [ + \- ] ? \d + ) ? / g, ']' )
503
- . replace ( / (?: ^ | : | , ) (?: \s * \[ ) + / g, '' ) ) ) {
512
+ if ( / ^ [ \] , : { } \s ] * $ / . test (
513
+ text . replace ( / \\ (?: [ " \\ \/ b f n r t ] | u [ 0 - 9 a - f A - F ] { 4 } ) / g, '@' )
514
+ . replace ( / " [ ^ " \\ \n \r ] * " | t r u e | f a l s e | n u l l | - ? \d + (?: \. \d * ) ? (?: [ e E ] [ + \- ] ? \d + ) ? / g, ']' )
515
+ . replace ( / (?: ^ | : | , ) (?: \s * \[ ) + / g, '' )
516
+ ) ) {
504
517
505
518
// In the third stage we use the eval function to compile the text into a
506
519
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
@@ -513,8 +526,8 @@ if (typeof JSON !== 'object') {
513
526
// each name/value pair to a reviver function for possible transformation.
514
527
515
528
return typeof reviver === 'function'
516
- ? walk ( { '' : j } , '' )
517
- : j ;
529
+ ? walk ( { '' : j } , '' )
530
+ : j ;
518
531
}
519
532
520
533
// If the text is not JSON parseable, then a SyntaxError is thrown.
0 commit comments