1
1
/*
2
2
json2.js
3
- 2014 -02-04
3
+ 2015 -02-25
4
4
5
5
Public Domain.
6
6
48
48
Date.prototype.toJSON = function (key) {
49
49
function f(n) {
50
50
// Format integers to have at least two digits.
51
- return n < 10 ? '0' + n : n;
51
+ return n < 10
52
+ ? '0' + n
53
+ : n;
52
54
}
53
55
54
56
return this.getUTCFullYear() + '-' +
146
148
redistribute.
147
149
*/
148
150
149
- /*jslint evil: true, regexp: true */
151
+ /*jslint
152
+ eval, for, this
153
+ */
150
154
151
- /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
152
- call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
155
+ /*property
156
+ JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
153
157
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
154
158
lastIndex, length, parse, prototype, push, replace, slice, stringify,
155
159
test, toJSON, toString, valueOf
@@ -168,28 +172,32 @@ if (typeof JSON !== 'object') {
168
172
169
173
function f ( n ) {
170
174
// Format integers to have at least two digits.
171
- return n < 10 ? '0' + n : n ;
175
+ return n < 10
176
+ ? '0' + n
177
+ : n ;
178
+ }
179
+
180
+ function this_value ( ) {
181
+ return this . valueOf ( ) ;
172
182
}
173
183
174
184
if ( typeof Date . prototype . toJSON !== 'function' ) {
175
185
176
186
Date . prototype . toJSON = function ( ) {
177
187
178
188
return isFinite ( this . valueOf ( ) )
179
- ? this . getUTCFullYear ( ) + '-' +
189
+ ? this . getUTCFullYear ( ) + '-' +
180
190
f ( this . getUTCMonth ( ) + 1 ) + '-' +
181
- f ( this . getUTCDate ( ) ) + 'T' +
182
- f ( this . getUTCHours ( ) ) + ':' +
183
- f ( this . getUTCMinutes ( ) ) + ':' +
184
- f ( this . getUTCSeconds ( ) ) + 'Z'
185
- : null ;
191
+ f ( this . getUTCDate ( ) ) + 'T' +
192
+ f ( this . getUTCHours ( ) ) + ':' +
193
+ f ( this . getUTCMinutes ( ) ) + ':' +
194
+ f ( this . getUTCSeconds ( ) ) + 'Z'
195
+ : null ;
186
196
} ;
187
197
188
- String . prototype . toJSON =
189
- Number . prototype . toJSON =
190
- Boolean . prototype . toJSON = function ( ) {
191
- return this . valueOf ( ) ;
192
- } ;
198
+ Boolean . prototype . toJSON = this_value ;
199
+ Number . prototype . toJSON = this_value ;
200
+ String . prototype . toJSON = this_value ;
193
201
}
194
202
195
203
var cx ,
@@ -208,12 +216,14 @@ if (typeof JSON !== 'object') {
208
216
// sequences.
209
217
210
218
escapable . lastIndex = 0 ;
211
- return escapable . test ( string ) ? '"' + string . replace ( escapable , function ( a ) {
219
+ return escapable . test ( string )
220
+ ? '"' + string . replace ( escapable , function ( a ) {
212
221
var c = meta [ a ] ;
213
222
return typeof c === 'string'
214
- ? c
215
- : '\\u' + ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
216
- } ) + '"' : '"' + string + '"' ;
223
+ ? c
224
+ : '\\u' + ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
225
+ } ) + '"'
226
+ : '"' + string + '"' ;
217
227
}
218
228
219
229
@@ -253,7 +263,9 @@ if (typeof JSON !== 'object') {
253
263
254
264
// JSON numbers must be finite. Encode non-finite numbers as null.
255
265
256
- return isFinite ( value ) ? String ( value ) : 'null' ;
266
+ return isFinite ( value )
267
+ ? String ( value )
268
+ : 'null' ;
257
269
258
270
case 'boolean' :
259
271
case 'null' :
@@ -297,10 +309,10 @@ if (typeof JSON !== 'object') {
297
309
// brackets.
298
310
299
311
v = partial . length === 0
300
- ? '[]'
301
- : gap
302
- ? '[\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + ']'
303
- : '[' + partial . join ( ',' ) + ']' ;
312
+ ? '[]'
313
+ : gap
314
+ ? '[\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + ']'
315
+ : '[' + partial . join ( ',' ) + ']' ;
304
316
gap = mind ;
305
317
return v ;
306
318
}
@@ -314,7 +326,11 @@ if (typeof JSON !== 'object') {
314
326
k = rep [ i ] ;
315
327
v = str ( k , value ) ;
316
328
if ( v ) {
317
- partial . push ( quote ( k ) + ( gap ? ': ' : ':' ) + v ) ;
329
+ partial . push ( quote ( k ) + (
330
+ gap
331
+ ? ': '
332
+ : ':'
333
+ ) + v ) ;
318
334
}
319
335
}
320
336
}
@@ -326,7 +342,11 @@ if (typeof JSON !== 'object') {
326
342
if ( Object . prototype . hasOwnProperty . call ( value , k ) ) {
327
343
v = str ( k , value ) ;
328
344
if ( v ) {
329
- partial . push ( quote ( k ) + ( gap ? ': ' : ':' ) + v ) ;
345
+ partial . push ( quote ( k ) + (
346
+ gap
347
+ ? ': '
348
+ : ':'
349
+ ) + v ) ;
330
350
}
331
351
}
332
352
}
@@ -336,10 +356,10 @@ if (typeof JSON !== 'object') {
336
356
// and wrap them in braces.
337
357
338
358
v = partial . length === 0
339
- ? '{}'
340
- : gap
341
- ? '{\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + '}'
342
- : '{' + partial . join ( ',' ) + '}' ;
359
+ ? '{}'
360
+ : gap
361
+ ? '{\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + '}'
362
+ : '{' + partial . join ( ',' ) + '}' ;
343
363
gap = mind ;
344
364
return v ;
345
365
}
@@ -348,14 +368,14 @@ if (typeof JSON !== 'object') {
348
368
// If the JSON object does not yet have a stringify method, give it one.
349
369
350
370
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;
371
+ escapable = / [ \\ \" \u0000 - \u001f \u007f - \u009f \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g;
352
372
meta = { // table of character substitutions
353
373
'\b' : '\\b' ,
354
374
'\t' : '\\t' ,
355
375
'\n' : '\\n' ,
356
376
'\f' : '\\f' ,
357
377
'\r' : '\\r' ,
358
- '"' : '\\"' ,
378
+ '"' : '\\"' ,
359
379
'\\' : '\\\\'
360
380
} ;
361
381
JSON . stringify = function ( value , replacer , space ) {
@@ -444,7 +464,7 @@ if (typeof JSON !== 'object') {
444
464
if ( cx . test ( text ) ) {
445
465
text = text . replace ( cx , function ( a ) {
446
466
return '\\u' +
447
- ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
467
+ ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
448
468
} ) ;
449
469
}
450
470
@@ -461,10 +481,13 @@ if (typeof JSON !== 'object') {
461
481
// we look to see that the remaining characters are only whitespace or ']' or
462
482
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
463
483
464
- if ( / ^ [ \] , : { } \s ] * $ /
465
- . test ( text . replace ( / \\ (?: [ " \\ \/ b f n r t ] | u [ 0 - 9 a - f A - F ] { 4 } ) / g, '@' )
484
+ if (
485
+ / ^ [ \] , : { } \s ] * $ / . test (
486
+ text . replace ( / \\ (?: [ " \\ \/ b f n r t ] | u [ 0 - 9 a - f A - F ] { 4 } ) / g, '@' )
466
487
. replace ( / " [ ^ " \\ \n \r ] * " | t r u e | f a l s e | n u l l | - ? \d + (?: \. \d * ) ? (?: [ e E ] [ + \- ] ? \d + ) ? / g, ']' )
467
- . replace ( / (?: ^ | : | , ) (?: \s * \[ ) + / g, '' ) ) ) {
488
+ . replace ( / (?: ^ | : | , ) (?: \s * \[ ) + / g, '' )
489
+ )
490
+ ) {
468
491
469
492
// In the third stage we use the eval function to compile the text into a
470
493
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
@@ -477,8 +500,8 @@ if (typeof JSON !== 'object') {
477
500
// each name/value pair to a reviver function for possible transformation.
478
501
479
502
return typeof reviver === 'function'
480
- ? walk ( { '' : j } , '' )
481
- : j ;
503
+ ? walk ( { '' : j } , '' )
504
+ : j ;
482
505
}
483
506
484
507
// If the text is not JSON parseable, then a SyntaxError is thrown.
0 commit comments