]> git.proxmox.com Git - extjs.git/blame - extjs/classic/classic/.sencha/test/json2.js
add extjs 6.0.1 sources
[extjs.git] / extjs / classic / classic / .sencha / test / json2.js
CommitLineData
6527f429
DM
1/*\r
2 json2.js\r
3 2012-10-08\r
4\r
5 Public Domain.\r
6\r
7 NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.\r
8\r
9 See http://www.JSON.org/js.html\r
10\r
11\r
12 This code should be minified before deployment.\r
13 See http://javascript.crockford.com/jsmin.html\r
14\r
15 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO\r
16 NOT CONTROL.\r
17\r
18\r
19 This file creates a global JSON object containing two methods: stringify\r
20 and parse.\r
21\r
22 JSON.stringify(value, replacer, space)\r
23 value any JavaScript value, usually an object or array.\r
24\r
25 replacer an optional parameter that determines how object\r
26 values are stringified for objects. It can be a\r
27 function or an array of strings.\r
28\r
29 space an optional parameter that specifies the indentation\r
30 of nested structures. If it is omitted, the text will\r
31 be packed without extra whitespace. If it is a number,\r
32 it will specify the number of spaces to indent at each\r
33 level. If it is a string (such as '\t' or ' '),\r
34 it contains the characters used to indent at each level.\r
35\r
36 This method produces a JSON text from a JavaScript value.\r
37\r
38 When an object value is found, if the object contains a toJSON\r
39 method, its toJSON method will be called and the result will be\r
40 stringified. A toJSON method does not serialize: it returns the\r
41 value represented by the name/value pair that should be serialized,\r
42 or undefined if nothing should be serialized. The toJSON method\r
43 will be passed the key associated with the value, and this will be\r
44 bound to the value\r
45\r
46 For example, this would serialize Dates as ISO strings.\r
47\r
48 Date.prototype.toJSON = function (key) {\r
49 function f(n) {\r
50 // Format integers to have at least two digits.\r
51 return n < 10 ? '0' + n : n;\r
52 }\r
53\r
54 return this.getUTCFullYear() + '-' +\r
55 f(this.getUTCMonth() + 1) + '-' +\r
56 f(this.getUTCDate()) + 'T' +\r
57 f(this.getUTCHours()) + ':' +\r
58 f(this.getUTCMinutes()) + ':' +\r
59 f(this.getUTCSeconds()) + 'Z';\r
60 };\r
61\r
62 You can provide an optional replacer method. It will be passed the\r
63 key and value of each member, with this bound to the containing\r
64 object. The value that is returned from your method will be\r
65 serialized. If your method returns undefined, then the member will\r
66 be excluded from the serialization.\r
67\r
68 If the replacer parameter is an array of strings, then it will be\r
69 used to select the members to be serialized. It filters the results\r
70 such that only members with keys listed in the replacer array are\r
71 stringified.\r
72\r
73 Values that do not have JSON representations, such as undefined or\r
74 functions, will not be serialized. Such values in objects will be\r
75 dropped; in arrays they will be replaced with null. You can use\r
76 a replacer function to replace those with JSON values.\r
77 JSON.stringify(undefined) returns undefined.\r
78\r
79 The optional space parameter produces a stringification of the\r
80 value that is filled with line breaks and indentation to make it\r
81 easier to read.\r
82\r
83 If the space parameter is a non-empty string, then that string will\r
84 be used for indentation. If the space parameter is a number, then\r
85 the indentation will be that many spaces.\r
86\r
87 Example:\r
88\r
89 text = JSON.stringify(['e', {pluribus: 'unum'}]);\r
90 // text is '["e",{"pluribus":"unum"}]'\r
91\r
92\r
93 text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');\r
94 // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'\r
95\r
96 text = JSON.stringify([new Date()], function (key, value) {\r
97 return this[key] instanceof Date ?\r
98 'Date(' + this[key] + ')' : value;\r
99 });\r
100 // text is '["Date(---current time---)"]'\r
101\r
102\r
103 JSON.parse(text, reviver)\r
104 This method parses a JSON text to produce an object or array.\r
105 It can throw a SyntaxError exception.\r
106\r
107 The optional reviver parameter is a function that can filter and\r
108 transform the results. It receives each of the keys and values,\r
109 and its return value is used instead of the original value.\r
110 If it returns what it received, then the structure is not modified.\r
111 If it returns undefined then the member is deleted.\r
112\r
113 Example:\r
114\r
115 // Parse the text. Values that look like ISO date strings will\r
116 // be converted to Date objects.\r
117\r
118 myData = JSON.parse(text, function (key, value) {\r
119 var a;\r
120 if (typeof value === 'string') {\r
121 a =\r
122/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);\r
123 if (a) {\r
124 return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],\r
125 +a[5], +a[6]));\r
126 }\r
127 }\r
128 return value;\r
129 });\r
130\r
131 myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {\r
132 var d;\r
133 if (typeof value === 'string' &&\r
134 value.slice(0, 5) === 'Date(' &&\r
135 value.slice(-1) === ')') {\r
136 d = new Date(value.slice(5, -1));\r
137 if (d) {\r
138 return d;\r
139 }\r
140 }\r
141 return value;\r
142 });\r
143\r
144\r
145 This is a reference implementation. You are free to copy, modify, or\r
146 redistribute.\r
147*/\r
148\r
149/*jslint evil: true, regexp: true */\r
150\r
151/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,\r
152 call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,\r
153 getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,\r
154 lastIndex, length, parse, prototype, push, replace, slice, stringify,\r
155 test, toJSON, toString, valueOf\r
156*/\r
157\r
158\r
159// Create a JSON object only if one does not already exist. We create the\r
160// methods in a closure to avoid creating global variables.\r
161\r
162if (typeof JSON !== 'object') {\r
163 JSON = {};\r
164}\r
165\r
166(function () {\r
167 'use strict';\r
168\r
169 function f(n) {\r
170 // Format integers to have at least two digits.\r
171 return n < 10 ? '0' + n : n;\r
172 }\r
173\r
174 if (typeof Date.prototype.toJSON !== 'function') {\r
175\r
176 Date.prototype.toJSON = function (key) {\r
177\r
178 return isFinite(this.valueOf())\r
179 ? this.getUTCFullYear() + '-' +\r
180 f(this.getUTCMonth() + 1) + '-' +\r
181 f(this.getUTCDate()) + 'T' +\r
182 f(this.getUTCHours()) + ':' +\r
183 f(this.getUTCMinutes()) + ':' +\r
184 f(this.getUTCSeconds()) + 'Z'\r
185 : null;\r
186 };\r
187\r
188 String.prototype.toJSON =\r
189 Number.prototype.toJSON =\r
190 Boolean.prototype.toJSON = function (key) {\r
191 return this.valueOf();\r
192 };\r
193 }\r
194\r
195 var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,\r
196 escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,\r
197 gap,\r
198 indent,\r
199 meta = { // table of character substitutions\r
200 '\b': '\\b',\r
201 '\t': '\\t',\r
202 '\n': '\\n',\r
203 '\f': '\\f',\r
204 '\r': '\\r',\r
205 '"' : '\\"',\r
206 '\\': '\\\\'\r
207 },\r
208 rep;\r
209\r
210\r
211 function quote(string) {\r
212\r
213// If the string contains no control characters, no quote characters, and no\r
214// backslash characters, then we can safely slap some quotes around it.\r
215// Otherwise we must also replace the offending characters with safe escape\r
216// sequences.\r
217\r
218 escapable.lastIndex = 0;\r
219 return escapable.test(string) ? '"' + string.replace(escapable, function (a) {\r
220 var c = meta[a];\r
221 return typeof c === 'string'\r
222 ? c\r
223 : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);\r
224 }) + '"' : '"' + string + '"';\r
225 }\r
226\r
227\r
228 function str(key, holder) {\r
229\r
230// Produce a string from holder[key].\r
231\r
232 var i, // The loop counter.\r
233 k, // The member key.\r
234 v, // The member value.\r
235 length,\r
236 mind = gap,\r
237 partial,\r
238 value = holder[key];\r
239\r
240// If the value has a toJSON method, call it to obtain a replacement value.\r
241\r
242 if (value && typeof value === 'object' &&\r
243 typeof value.toJSON === 'function') {\r
244 value = value.toJSON(key);\r
245 }\r
246\r
247// If we were called with a replacer function, then call the replacer to\r
248// obtain a replacement value.\r
249\r
250 if (typeof rep === 'function') {\r
251 value = rep.call(holder, key, value);\r
252 }\r
253\r
254// What happens next depends on the value's type.\r
255\r
256 switch (typeof value) {\r
257 case 'string':\r
258 return quote(value);\r
259\r
260 case 'number':\r
261\r
262// JSON numbers must be finite. Encode non-finite numbers as null.\r
263\r
264 return isFinite(value) ? String(value) : 'null';\r
265\r
266 case 'boolean':\r
267 case 'null':\r
268\r
269// If the value is a boolean or null, convert it to a string. Note:\r
270// typeof null does not produce 'null'. The case is included here in\r
271// the remote chance that this gets fixed someday.\r
272\r
273 return String(value);\r
274\r
275// If the type is 'object', we might be dealing with an object or an array or\r
276// null.\r
277\r
278 case 'object':\r
279\r
280// Due to a specification blunder in ECMAScript, typeof null is 'object',\r
281// so watch out for that case.\r
282\r
283 if (!value) {\r
284 return 'null';\r
285 }\r
286\r
287// Make an array to hold the partial results of stringifying this object value.\r
288\r
289 gap += indent;\r
290 partial = [];\r
291\r
292// Is the value an array?\r
293\r
294 if (Object.prototype.toString.apply(value) === '[object Array]') {\r
295\r
296// The value is an array. Stringify every element. Use null as a placeholder\r
297// for non-JSON values.\r
298\r
299 length = value.length;\r
300 for (i = 0; i < length; i += 1) {\r
301 partial[i] = str(i, value) || 'null';\r
302 }\r
303\r
304// Join all of the elements together, separated with commas, and wrap them in\r
305// brackets.\r
306\r
307 v = partial.length === 0\r
308 ? '[]'\r
309 : gap\r
310 ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'\r
311 : '[' + partial.join(',') + ']';\r
312 gap = mind;\r
313 return v;\r
314 }\r
315\r
316// If the replacer is an array, use it to select the members to be stringified.\r
317\r
318 if (rep && typeof rep === 'object') {\r
319 length = rep.length;\r
320 for (i = 0; i < length; i += 1) {\r
321 if (typeof rep[i] === 'string') {\r
322 k = rep[i];\r
323 v = str(k, value);\r
324 if (v) {\r
325 partial.push(quote(k) + (gap ? ': ' : ':') + v);\r
326 }\r
327 }\r
328 }\r
329 } else {\r
330\r
331// Otherwise, iterate through all of the keys in the object.\r
332\r
333 for (k in value) {\r
334 if (Object.prototype.hasOwnProperty.call(value, k)) {\r
335 v = str(k, value);\r
336 if (v) {\r
337 partial.push(quote(k) + (gap ? ': ' : ':') + v);\r
338 }\r
339 }\r
340 }\r
341 }\r
342\r
343// Join all of the member texts together, separated with commas,\r
344// and wrap them in braces.\r
345\r
346 v = partial.length === 0\r
347 ? '{}'\r
348 : gap\r
349 ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'\r
350 : '{' + partial.join(',') + '}';\r
351 gap = mind;\r
352 return v;\r
353 }\r
354 }\r
355\r
356// If the JSON object does not yet have a stringify method, give it one.\r
357\r
358 if (typeof JSON.stringify !== 'function') {\r
359 JSON.stringify = function (value, replacer, space) {\r
360\r
361// The stringify method takes a value and an optional replacer, and an optional\r
362// space parameter, and returns a JSON text. The replacer can be a function\r
363// that can replace values, or an array of strings that will select the keys.\r
364// A default replacer method can be provided. Use of the space parameter can\r
365// produce text that is more easily readable.\r
366\r
367 var i;\r
368 gap = '';\r
369 indent = '';\r
370\r
371// If the space parameter is a number, make an indent string containing that\r
372// many spaces.\r
373\r
374 if (typeof space === 'number') {\r
375 for (i = 0; i < space; i += 1) {\r
376 indent += ' ';\r
377 }\r
378\r
379// If the space parameter is a string, it will be used as the indent string.\r
380\r
381 } else if (typeof space === 'string') {\r
382 indent = space;\r
383 }\r
384\r
385// If there is a replacer, it must be a function or an array.\r
386// Otherwise, throw an error.\r
387\r
388 rep = replacer;\r
389 if (replacer && typeof replacer !== 'function' &&\r
390 (typeof replacer !== 'object' ||\r
391 typeof replacer.length !== 'number')) {\r
392 throw new Error('JSON.stringify');\r
393 }\r
394\r
395// Make a fake root object containing our value under the key of ''.\r
396// Return the result of stringifying the value.\r
397\r
398 return str('', {'': value});\r
399 };\r
400 }\r
401\r
402\r
403// If the JSON object does not yet have a parse method, give it one.\r
404\r
405 if (typeof JSON.parse !== 'function') {\r
406 JSON.parse = function (text, reviver) {\r
407\r
408// The parse method takes a text and an optional reviver function, and returns\r
409// a JavaScript value if the text is a valid JSON text.\r
410\r
411 var j;\r
412\r
413 function walk(holder, key) {\r
414\r
415// The walk method is used to recursively walk the resulting structure so\r
416// that modifications can be made.\r
417\r
418 var k, v, value = holder[key];\r
419 if (value && typeof value === 'object') {\r
420 for (k in value) {\r
421 if (Object.prototype.hasOwnProperty.call(value, k)) {\r
422 v = walk(value, k);\r
423 if (v !== undefined) {\r
424 value[k] = v;\r
425 } else {\r
426 delete value[k];\r
427 }\r
428 }\r
429 }\r
430 }\r
431 return reviver.call(holder, key, value);\r
432 }\r
433\r
434\r
435// Parsing happens in four stages. In the first stage, we replace certain\r
436// Unicode characters with escape sequences. JavaScript handles many characters\r
437// incorrectly, either silently deleting them, or treating them as line endings.\r
438\r
439 text = String(text);\r
440 cx.lastIndex = 0;\r
441 if (cx.test(text)) {\r
442 text = text.replace(cx, function (a) {\r
443 return '\\u' +\r
444 ('0000' + a.charCodeAt(0).toString(16)).slice(-4);\r
445 });\r
446 }\r
447\r
448// In the second stage, we run the text against regular expressions that look\r
449// for non-JSON patterns. We are especially concerned with '()' and 'new'\r
450// because they can cause invocation, and '=' because it can cause mutation.\r
451// But just to be safe, we want to reject all unexpected forms.\r
452\r
453// We split the second stage into 4 regexp operations in order to work around\r
454// crippling inefficiencies in IE's and Safari's regexp engines. First we\r
455// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we\r
456// replace all simple value tokens with ']' characters. Third, we delete all\r
457// open brackets that follow a colon or comma or that begin the text. Finally,\r
458// we look to see that the remaining characters are only whitespace or ']' or\r
459// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.\r
460\r
461 if (/^[\],:{}\s]*$/\r
462 .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')\r
463 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')\r
464 .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {\r
465\r
466// In the third stage we use the eval function to compile the text into a\r
467// JavaScript structure. The '{' operator is subject to a syntactic ambiguity\r
468// in JavaScript: it can begin a block or an object literal. We wrap the text\r
469// in parens to eliminate the ambiguity.\r
470\r
471 j = eval('(' + text + ')');\r
472\r
473// In the optional fourth stage, we recursively walk the new structure, passing\r
474// each name/value pair to a reviver function for possible transformation.\r
475\r
476 return typeof reviver === 'function'\r
477 ? walk({'': j}, '')\r
478 : j;\r
479 }\r
480\r
481// If the text is not JSON parseable, then a SyntaxError is thrown.\r
482\r
483 throw new SyntaxError('JSON.parse');\r
484 };\r
485 }\r
486}());\r