11/*
22 json2.js
3- 2014 -02-04
3+ 2015 -02-25
44
55 Public Domain.
66
4848 Date.prototype.toJSON = function (key) {
4949 function f(n) {
5050 // Format integers to have at least two digits.
51- return n < 10 ? '0' + n : n;
51+ return n < 10
52+ ? '0' + n
53+ : n;
5254 }
5355
5456 return this.getUTCFullYear() + '-' +
146148 redistribute.
147149*/
148150
149- /*jslint evil: true, regexp: true */
151+ /*jslint
152+ eval, for, this
153+ */
150154
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,
153157 getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
154158 lastIndex, length, parse, prototype, push, replace, slice, stringify,
155159 test, toJSON, toString, valueOf
@@ -168,28 +172,32 @@ if (typeof JSON !== 'object') {
168172
169173 function f ( n ) {
170174 // 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 ( ) ;
172182 }
173183
174184 if ( typeof Date . prototype . toJSON !== 'function' ) {
175185
176186 Date . prototype . toJSON = function ( ) {
177187
178188 return isFinite ( this . valueOf ( ) )
179- ? this . getUTCFullYear ( ) + '-' +
189+ ? this . getUTCFullYear ( ) + '-' +
180190 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 ;
186196 } ;
187197
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 ;
193201 }
194202
195203 var cx ,
@@ -208,12 +216,14 @@ if (typeof JSON !== 'object') {
208216// sequences.
209217
210218 escapable . lastIndex = 0 ;
211- return escapable . test ( string ) ? '"' + string . replace ( escapable , function ( a ) {
219+ return escapable . test ( string )
220+ ? '"' + string . replace ( escapable , function ( a ) {
212221 var c = meta [ a ] ;
213222 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 + '"' ;
217227 }
218228
219229
@@ -253,7 +263,9 @@ if (typeof JSON !== 'object') {
253263
254264// JSON numbers must be finite. Encode non-finite numbers as null.
255265
256- return isFinite ( value ) ? String ( value ) : 'null' ;
266+ return isFinite ( value )
267+ ? String ( value )
268+ : 'null' ;
257269
258270 case 'boolean' :
259271 case 'null' :
@@ -297,10 +309,10 @@ if (typeof JSON !== 'object') {
297309// brackets.
298310
299311 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 ( ',' ) + ']' ;
304316 gap = mind ;
305317 return v ;
306318 }
@@ -314,7 +326,11 @@ if (typeof JSON !== 'object') {
314326 k = rep [ i ] ;
315327 v = str ( k , value ) ;
316328 if ( v ) {
317- partial . push ( quote ( k ) + ( gap ? ': ' : ':' ) + v ) ;
329+ partial . push ( quote ( k ) + (
330+ gap
331+ ? ': '
332+ : ':'
333+ ) + v ) ;
318334 }
319335 }
320336 }
@@ -326,7 +342,11 @@ if (typeof JSON !== 'object') {
326342 if ( Object . prototype . hasOwnProperty . call ( value , k ) ) {
327343 v = str ( k , value ) ;
328344 if ( v ) {
329- partial . push ( quote ( k ) + ( gap ? ': ' : ':' ) + v ) ;
345+ partial . push ( quote ( k ) + (
346+ gap
347+ ? ': '
348+ : ':'
349+ ) + v ) ;
330350 }
331351 }
332352 }
@@ -336,10 +356,10 @@ if (typeof JSON !== 'object') {
336356// and wrap them in braces.
337357
338358 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 ( ',' ) + '}' ;
343363 gap = mind ;
344364 return v ;
345365 }
@@ -348,14 +368,14 @@ if (typeof JSON !== 'object') {
348368// If the JSON object does not yet have a stringify method, give it one.
349369
350370 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;
352372 meta = { // table of character substitutions
353373 '\b' : '\\b' ,
354374 '\t' : '\\t' ,
355375 '\n' : '\\n' ,
356376 '\f' : '\\f' ,
357377 '\r' : '\\r' ,
358- '"' : '\\"' ,
378+ '"' : '\\"' ,
359379 '\\' : '\\\\'
360380 } ;
361381 JSON . stringify = function ( value , replacer , space ) {
@@ -444,7 +464,7 @@ if (typeof JSON !== 'object') {
444464 if ( cx . test ( text ) ) {
445465 text = text . replace ( cx , function ( a ) {
446466 return '\\u' +
447- ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
467+ ( '0000' + a . charCodeAt ( 0 ) . toString ( 16 ) ) . slice ( - 4 ) ;
448468 } ) ;
449469 }
450470
@@ -461,10 +481,13 @@ if (typeof JSON !== 'object') {
461481// we look to see that the remaining characters are only whitespace or ']' or
462482// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
463483
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, '@' )
466487 . 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+ ) {
468491
469492// In the third stage we use the eval function to compile the text into a
470493// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
@@ -477,8 +500,8 @@ if (typeof JSON !== 'object') {
477500// each name/value pair to a reviver function for possible transformation.
478501
479502 return typeof reviver === 'function'
480- ? walk ( { '' : j } , '' )
481- : j ;
503+ ? walk ( { '' : j } , '' )
504+ : j ;
482505 }
483506
484507// If the text is not JSON parseable, then a SyntaxError is thrown.
0 commit comments