4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
9 * Based on JSON2 (http://www.JSON.org/js.html).
12 (function (exports
, nativeJSON
) {
15 // use native JSON if it's available
16 if (nativeJSON
&& nativeJSON
.parse
){
17 return exports
.JSON
= {
18 parse
: nativeJSON
.parse
19 , stringify
: nativeJSON
.stringify
23 var JSON
= exports
.JSON
= {};
26 // Format integers to have at least two digits.
27 return n
< 10 ? '0' + n
: n
;
30 function date(d
, key
) {
31 return isFinite(d
.valueOf()) ?
32 d
.getUTCFullYear() + '-' +
33 f(d
.getUTCMonth() + 1) + '-' +
34 f(d
.getUTCDate()) + 'T' +
35 f(d
.getUTCHours()) + ':' +
36 f(d
.getUTCMinutes()) + ':' +
37 f(d
.getUTCSeconds()) + 'Z' : null;
40 var cx
= /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
41 escapable
= /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
44 meta
= { // table of character substitutions
56 function quote(string
) {
58 // If the string contains no control characters, no quote characters, and no
59 // backslash characters, then we can safely slap some quotes around it.
60 // Otherwise we must also replace the offending characters with safe escape
63 escapable
.lastIndex
= 0;
64 return escapable
.test(string
) ? '"' + string
.replace(escapable
, function (a
) {
66 return typeof c
=== 'string' ? c
:
67 '\\u' + ('0000' + a
.charCodeAt(0).toString(16)).slice(-4);
68 }) + '"' : '"' + string
+ '"';
72 function str(key
, holder
) {
74 // Produce a string from holder[key].
76 var i
, // The loop counter.
78 v
, // The member value.
84 // If the value has a toJSON method, call it to obtain a replacement value.
86 if (value
instanceof Date
) {
90 // If we were called with a replacer function, then call the replacer to
91 // obtain a replacement value.
93 if (typeof rep
=== 'function') {
94 value
= rep
.call(holder
, key
, value
);
97 // What happens next depends on the value's type.
99 switch (typeof value
) {
105 // JSON numbers must be finite. Encode non-finite numbers as null.
107 return isFinite(value
) ? String(value
) : 'null';
112 // If the value is a boolean or null, convert it to a string. Note:
113 // typeof null does not produce 'null'. The case is included here in
114 // the remote chance that this gets fixed someday.
116 return String(value
);
118 // If the type is 'object', we might be dealing with an object or an array or
123 // Due to a specification blunder in ECMAScript, typeof null is 'object',
124 // so watch out for that case.
130 // Make an array to hold the partial results of stringifying this object value.
135 // Is the value an array?
137 if (Object
.prototype.toString
.apply(value
) === '[object Array]') {
139 // The value is an array. Stringify every element. Use null as a placeholder
140 // for non-JSON values.
142 length
= value
.length
;
143 for (i
= 0; i
< length
; i
+= 1) {
144 partial
[i
] = str(i
, value
) || 'null';
147 // Join all of the elements together, separated with commas, and wrap them in
150 v
= partial
.length
=== 0 ? '[]' : gap
?
151 '[\n' + gap
+ partial
.join(',\n' + gap
) + '\n' + mind
+ ']' :
152 '[' + partial
.join(',') + ']';
157 // If the replacer is an array, use it to select the members to be stringified.
159 if (rep
&& typeof rep
=== 'object') {
161 for (i
= 0; i
< length
; i
+= 1) {
162 if (typeof rep
[i
] === 'string') {
166 partial
.push(quote(k
) + (gap
? ': ' : ':') + v
);
172 // Otherwise, iterate through all of the keys in the object.
175 if (Object
.prototype.hasOwnProperty
.call(value
, k
)) {
178 partial
.push(quote(k
) + (gap
? ': ' : ':') + v
);
184 // Join all of the member texts together, separated with commas,
185 // and wrap them in braces.
187 v
= partial
.length
=== 0 ? '{}' : gap
?
188 '{\n' + gap
+ partial
.join(',\n' + gap
) + '\n' + mind
+ '}' :
189 '{' + partial
.join(',') + '}';
195 // If the JSON object does not yet have a stringify method, give it one.
197 JSON
.stringify = function (value
, replacer
, space
) {
199 // The stringify method takes a value and an optional replacer, and an optional
200 // space parameter, and returns a JSON text. The replacer can be a function
201 // that can replace values, or an array of strings that will select the keys.
202 // A default replacer method can be provided. Use of the space parameter can
203 // produce text that is more easily readable.
209 // If the space parameter is a number, make an indent string containing that
212 if (typeof space
=== 'number') {
213 for (i
= 0; i
< space
; i
+= 1) {
217 // If the space parameter is a string, it will be used as the indent string.
219 } else if (typeof space
=== 'string') {
223 // If there is a replacer, it must be a function or an array.
224 // Otherwise, throw an error.
227 if (replacer
&& typeof replacer
!== 'function' &&
228 (typeof replacer
!== 'object' ||
229 typeof replacer
.length
!== 'number')) {
230 throw new Error('JSON.stringify');
233 // Make a fake root object containing our value under the key of ''.
234 // Return the result of stringifying the value.
236 return str('', {'': value
});
239 // If the JSON object does not yet have a parse method, give it one.
241 JSON
.parse = function (text
, reviver
) {
242 // The parse method takes a text and an optional reviver function, and returns
243 // a JavaScript value if the text is a valid JSON text.
247 function walk(holder
, key
) {
249 // The walk method is used to recursively walk the resulting structure so
250 // that modifications can be made.
252 var k
, v
, value
= holder
[key
];
253 if (value
&& typeof value
=== 'object') {
255 if (Object
.prototype.hasOwnProperty
.call(value
, k
)) {
257 if (v
!== undefined) {
265 return reviver
.call(holder
, key
, value
);
269 // Parsing happens in four stages. In the first stage, we replace certain
270 // Unicode characters with escape sequences. JavaScript handles many characters
271 // incorrectly, either silently deleting them, or treating them as line endings.
276 text
= text
.replace(cx
, function (a
) {
278 ('0000' + a
.charCodeAt(0).toString(16)).slice(-4);
282 // In the second stage, we run the text against regular expressions that look
283 // for non-JSON patterns. We are especially concerned with '()' and 'new'
284 // because they can cause invocation, and '=' because it can cause mutation.
285 // But just to be safe, we want to reject all unexpected forms.
287 // We split the second stage into 4 regexp operations in order to work around
288 // crippling inefficiencies in IE's and Safari's regexp engines. First we
289 // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
290 // replace all simple value tokens with ']' characters. Third, we delete all
291 // open brackets that follow a colon or comma or that begin the text. Finally,
292 // we look to see that the remaining characters are only whitespace or ']' or
293 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
296 .test(text
.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
297 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
298 .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
300 // In the third stage we use the eval function to compile the text into a
301 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
302 // in JavaScript: it can begin a block or an object literal. We wrap the text
303 // in parens to eliminate the ambiguity.
305 j
= eval('(' + text
+ ')');
307 // In the optional fourth stage, we recursively walk the new structure, passing
308 // each name/value pair to a reviver function for possible transformation.
310 return typeof reviver
=== 'function' ?
311 walk({'': j
}, '') : j
;
314 // If the text is not JSON parseable, then a SyntaxError is thrown.
316 throw new SyntaxError('JSON.parse');
320 'undefined' != typeof io
? io
: module
.exports
321 , typeof JSON
!== 'undefined' ? JSON
: undefined