1
1
/*
2
2
json2.js
3
- 2015-02-25
3
+ 2015-05-03
4
4
5
5
Public Domain.
6
6
17
17
18
18
19
19
This file creates a global JSON object containing two methods: stringify
20
- and parse.
20
+ and parse. This file is provides the ES5 JSON capability to ES3 systems.
21
+ If a project might run on IE8 or earlier, then this file should be included.
22
+ This file does nothing on ES5 systems.
21
23
22
24
JSON.stringify(value, replacer, space)
23
25
value any JavaScript value, usually an object or array.
49
51
function f(n) {
50
52
// Format integers to have at least two digits.
51
53
return n < 10
52
- ? '0' + n
53
- : n;
54
+ ? '0' + n
55
+ : n;
54
56
}
55
57
56
58
return this.getUTCFullYear() + '-' +
96
98
// text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
97
99
98
100
text = JSON.stringify([new Date()], function (key, value) {
99
- return this[key] instanceof Date ?
100
- 'Date(' + this[key] + ')' : value;
101
+ return this[key] instanceof Date
102
+ ? 'Date(' + this[key] + ')'
103
+ : value;
101
104
});
102
105
// text is '["Date(---current time---)"]'
103
106
@@ -169,12 +172,19 @@ if (typeof JSON !== 'object') {
169
172
170
173
( function ( ) {
171
174
'use strict' ;
175
+
176
+ var rx_one = / ^ [ \] , : { } \s ] * $ / ,
177
+ rx_two = / \\ (?: [ " \\ \/ b f n r t ] | u [ 0 - 9 a - f A - F ] { 4 } ) / g,
178
+ rx_three = / " [ ^ " \\ \n \r ] * " | t r u e | f a l s e | n u l l | - ? \d + (?: \. \d * ) ? (?: [ e E ] [ + \- ] ? \d + ) ? / g,
179
+ rx_four = / (?: ^ | : | , ) (?: \s * \[ ) + / g,
180
+ rx_escapable = / [ \\ \" \u0000 - \u001f \u007f - \u009f \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g,
181
+ rx_dangerous = / [ \u0000 \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g;
172
182
173
183
function f ( n ) {
174
184
// Format integers to have at least two digits.
175
185
return n < 10
176
- ? '0' + n
177
- : n ;
186
+ ? '0' + n
187
+ : n ;
178
188
}
179
189
180
190
function this_value ( ) {
@@ -186,23 +196,21 @@ if (typeof JSON !== 'object') {
186
196
Date . prototype . toJSON = function ( ) {
187
197
188
198
return isFinite ( this . valueOf ( ) )
189
- ? this . getUTCFullYear ( ) + '-' +
190
- f ( this . getUTCMonth ( ) + 1 ) + '-' +
191
- f ( this . getUTCDate ( ) ) + 'T' +
192
- f ( this . getUTCHours ( ) ) + ':' +
193
- f ( this . getUTCMinutes ( ) ) + ':' +
194
- f ( this . getUTCSeconds ( ) ) + 'Z'
195
- : null ;
199
+ ? this . getUTCFullYear ( ) + '-' +
200
+ f ( this . getUTCMonth ( ) + 1 ) + '-' +
201
+ f ( this . getUTCDate ( ) ) + 'T' +
202
+ f ( this . getUTCHours ( ) ) + ':' +
203
+ f ( this . getUTCMinutes ( ) ) + ':' +
204
+ f ( this . getUTCSeconds ( ) ) + 'Z'
205
+ : null ;
196
206
} ;
197
207
198
208
Boolean . prototype . toJSON = this_value ;
199
209
Number . prototype . toJSON = this_value ;
200
210
String . prototype . toJSON = this_value ;
201
211
}
202
212
203
- var cx ,
204
- escapable ,
205
- gap ,
213
+ var gap ,
206
214
indent ,
207
215
meta ,
208
216
rep ;
@@ -215,15 +223,15 @@ if (typeof JSON !== 'object') {
215
223
// Otherwise we must also replace the offending characters with safe escape
216
224
// sequences.
217
225
218
- escapable . lastIndex = 0 ;
219
- return escapable . test ( string )
220
- ? '"' + string . replace ( escapable , function ( a ) {
221
- var c = meta [ a ] ;
222
- return typeof c === 'string'
223
- ? c
224
- : '\\u' + ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
225
- } ) + '"'
226
- : '"' + string + '"' ;
226
+ rx_escapable . lastIndex = 0 ;
227
+ return rx_escapable . test ( string )
228
+ ? '"' + string . replace ( rx_escapable , function ( a ) {
229
+ var c = meta [ a ] ;
230
+ return typeof c === 'string'
231
+ ? c
232
+ : '\\u' + ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
233
+ } ) + '"'
234
+ : '"' + string + '"' ;
227
235
}
228
236
229
237
@@ -264,8 +272,8 @@ if (typeof JSON !== 'object') {
264
272
// JSON numbers must be finite. Encode non-finite numbers as null.
265
273
266
274
return isFinite ( value )
267
- ? String ( value )
268
- : 'null' ;
275
+ ? String ( value )
276
+ : 'null' ;
269
277
270
278
case 'boolean' :
271
279
case 'null' :
@@ -309,10 +317,10 @@ if (typeof JSON !== 'object') {
309
317
// brackets.
310
318
311
319
v = partial . length === 0
312
- ? '[]'
313
- : gap
314
- ? '[\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + ']'
315
- : '[' + partial . join ( ',' ) + ']' ;
320
+ ? '[]'
321
+ : gap
322
+ ? '[\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + ']'
323
+ : '[' + partial . join ( ',' ) + ']' ;
316
324
gap = mind ;
317
325
return v ;
318
326
}
@@ -328,8 +336,8 @@ if (typeof JSON !== 'object') {
328
336
if ( v ) {
329
337
partial . push ( quote ( k ) + (
330
338
gap
331
- ? ': '
332
- : ':'
339
+ ? ': '
340
+ : ':'
333
341
) + v ) ;
334
342
}
335
343
}
@@ -344,8 +352,8 @@ if (typeof JSON !== 'object') {
344
352
if ( v ) {
345
353
partial . push ( quote ( k ) + (
346
354
gap
347
- ? ': '
348
- : ':'
355
+ ? ': '
356
+ : ':'
349
357
) + v ) ;
350
358
}
351
359
}
@@ -356,10 +364,10 @@ if (typeof JSON !== 'object') {
356
364
// and wrap them in braces.
357
365
358
366
v = partial . length === 0
359
- ? '{}'
360
- : gap
361
- ? '{\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + '}'
362
- : '{' + partial . join ( ',' ) + '}' ;
367
+ ? '{}'
368
+ : gap
369
+ ? '{\n' + gap + partial . join ( ',\n' + gap ) + '\n' + mind + '}'
370
+ : '{' + partial . join ( ',' ) + '}' ;
363
371
gap = mind ;
364
372
return v ;
365
373
}
@@ -368,7 +376,6 @@ if (typeof JSON !== 'object') {
368
376
// If the JSON object does not yet have a stringify method, give it one.
369
377
370
378
if ( typeof JSON . stringify !== 'function' ) {
371
- escapable = / [ \\ \" \u0000 - \u001f \u007f - \u009f \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g;
372
379
meta = { // table of character substitutions
373
380
'\b' : '\\b' ,
374
381
'\t' : '\\t' ,
@@ -425,7 +432,6 @@ if (typeof JSON !== 'object') {
425
432
// If the JSON object does not yet have a parse method, give it one.
426
433
427
434
if ( typeof JSON . parse !== 'function' ) {
428
- cx = / [ \u0000 \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g;
429
435
JSON . parse = function ( text , reviver ) {
430
436
431
437
// The parse method takes a text and an optional reviver function, and returns
@@ -460,9 +466,9 @@ if (typeof JSON !== 'object') {
460
466
// incorrectly, either silently deleting them, or treating them as line endings.
461
467
462
468
text = String ( text ) ;
463
- cx . lastIndex = 0 ;
464
- if ( cx . test ( text ) ) {
465
- text = text . replace ( cx , function ( a ) {
469
+ rx_dangerous . lastIndex = 0 ;
470
+ if ( rx_dangerous . test ( text ) ) {
471
+ text = text . replace ( rx_dangerous , function ( a ) {
466
472
return '\\u' +
467
473
( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
468
474
} ) ;
@@ -482,10 +488,11 @@ if (typeof JSON !== 'object') {
482
488
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
483
489
484
490
if (
485
- / ^ [ \] , : { } \s ] * $ / . test (
486
- text . replace ( / \\ (?: [ " \\ \/ b f n r t ] | u [ 0 - 9 a - f A - F ] { 4 } ) / g, '@' )
487
- . replace ( / " [ ^ " \\ \n \r ] * " | t r u e | f a l s e | n u l l | - ? \d + (?: \. \d * ) ? (?: [ e E ] [ + \- ] ? \d + ) ? / g, ']' )
488
- . replace ( / (?: ^ | : | , ) (?: \s * \[ ) + / g, '' )
491
+ rx_one . test (
492
+ text
493
+ . replace ( rx_two , '@' )
494
+ . replace ( rx_three , ']' )
495
+ . replace ( rx_four , '' )
489
496
)
490
497
) {
491
498
@@ -500,8 +507,8 @@ if (typeof JSON !== 'object') {
500
507
// each name/value pair to a reviver function for possible transformation.
501
508
502
509
return typeof reviver === 'function'
503
- ? walk ( { '' : j } , '' )
504
- : j ;
510
+ ? walk ( { '' : j } , '' )
511
+ : j ;
505
512
}
506
513
507
514
// If the text is not JSON parseable, then a SyntaxError is thrown.
0 commit comments