annotate webapp/src/main/webapp/jquery/json2.js @ 1120:8bd10cd04169

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