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