11/*
22 json2.js
3- 2015-02-25
3+ 2015-05-03
44
55 Public Domain.
66
1717
1818
1919 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.
2123
2224 JSON.stringify(value, replacer, space)
2325 value any JavaScript value, usually an object or array.
4951 function f(n) {
5052 // Format integers to have at least two digits.
5153 return n < 10
52- ? '0' + n
53- : n;
54+ ? '0' + n
55+ : n;
5456 }
5557
5658 return this.getUTCFullYear() + '-' +
9698 // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
9799
98100 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;
101104 });
102105 // text is '["Date(---current time---)"]'
103106
@@ -169,12 +172,19 @@ if (typeof JSON !== 'object') {
169172
170173( function ( ) {
171174 '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;
172182
173183 function f ( n ) {
174184 // Format integers to have at least two digits.
175185 return n < 10
176- ? '0' + n
177- : n ;
186+ ? '0' + n
187+ : n ;
178188 }
179189
180190 function this_value ( ) {
@@ -186,23 +196,21 @@ if (typeof JSON !== 'object') {
186196 Date . prototype . toJSON = function ( ) {
187197
188198 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 ;
196206 } ;
197207
198208 Boolean . prototype . toJSON = this_value ;
199209 Number . prototype . toJSON = this_value ;
200210 String . prototype . toJSON = this_value ;
201211 }
202212
203- var cx ,
204- escapable ,
205- gap ,
213+ var gap ,
206214 indent ,
207215 meta ,
208216 rep ;
@@ -215,15 +223,15 @@ if (typeof JSON !== 'object') {
215223// Otherwise we must also replace the offending characters with safe escape
216224// sequences.
217225
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 + '"' ;
227235 }
228236
229237
@@ -264,8 +272,8 @@ if (typeof JSON !== 'object') {
264272// JSON numbers must be finite. Encode non-finite numbers as null.
265273
266274 return isFinite ( value )
267- ? String ( value )
268- : 'null' ;
275+ ? String ( value )
276+ : 'null' ;
269277
270278 case 'boolean' :
271279 case 'null' :
@@ -309,10 +317,10 @@ if (typeof JSON !== 'object') {
309317// brackets.
310318
311319 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 ( ',' ) + ']' ;
316324 gap = mind ;
317325 return v ;
318326 }
@@ -328,8 +336,8 @@ if (typeof JSON !== 'object') {
328336 if ( v ) {
329337 partial . push ( quote ( k ) + (
330338 gap
331- ? ': '
332- : ':'
339+ ? ': '
340+ : ':'
333341 ) + v ) ;
334342 }
335343 }
@@ -344,8 +352,8 @@ if (typeof JSON !== 'object') {
344352 if ( v ) {
345353 partial . push ( quote ( k ) + (
346354 gap
347- ? ': '
348- : ':'
355+ ? ': '
356+ : ':'
349357 ) + v ) ;
350358 }
351359 }
@@ -356,10 +364,10 @@ if (typeof JSON !== 'object') {
356364// and wrap them in braces.
357365
358366 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 ( ',' ) + '}' ;
363371 gap = mind ;
364372 return v ;
365373 }
@@ -368,7 +376,6 @@ if (typeof JSON !== 'object') {
368376// If the JSON object does not yet have a stringify method, give it one.
369377
370378 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;
372379 meta = { // table of character substitutions
373380 '\b' : '\\b' ,
374381 '\t' : '\\t' ,
@@ -425,7 +432,6 @@ if (typeof JSON !== 'object') {
425432// If the JSON object does not yet have a parse method, give it one.
426433
427434 if ( typeof JSON . parse !== 'function' ) {
428- cx = / [ \u0000 \u00ad \u0600 - \u0604 \u070f \u17b4 \u17b5 \u200c - \u200f \u2028 - \u202f \u2060 - \u206f \ufeff \ufff0 - \uffff ] / g;
429435 JSON . parse = function ( text , reviver ) {
430436
431437// The parse method takes a text and an optional reviver function, and returns
@@ -460,9 +466,9 @@ if (typeof JSON !== 'object') {
460466// incorrectly, either silently deleting them, or treating them as line endings.
461467
462468 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 ) {
466472 return '\\u' +
467473 ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
468474 } ) ;
@@ -482,10 +488,11 @@ if (typeof JSON !== 'object') {
482488// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
483489
484490 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 , '' )
489496 )
490497 ) {
491498
@@ -500,8 +507,8 @@ if (typeof JSON !== 'object') {
500507// each name/value pair to a reviver function for possible transformation.
501508
502509 return typeof reviver === 'function'
503- ? walk ( { '' : j } , '' )
504- : j ;
510+ ? walk ( { '' : j } , '' )
511+ : j ;
505512 }
506513
507514// If the text is not JSON parseable, then a SyntaxError is thrown.
0 commit comments