Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/client/lib/json2.js
1154 views
1
// json2.js
2
// 2016-10-28
3
// Public Domain.
4
// NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
5
// See http://www.JSON.org/js.html
6
// This code should be minified before deployment.
7
// See http://javascript.crockford.com/jsmin.html
8
9
// USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
10
// NOT CONTROL.
11
12
// This file creates a global JSON object containing two methods: stringify
13
// and parse. This file provides the ES5 JSON capability to ES3 systems.
14
// If a project might run on IE8 or earlier, then this file should be included.
15
// This file does nothing on ES5 systems.
16
17
// Create a JSON object only if one does not already exist. We create the
18
// methods in a closure to avoid creating global variables.
19
20
if (typeof JSON !== "object") {
21
JSON = {};
22
}
23
24
(function () {
25
"use strict";
26
27
var rx_one = /^[\],:{}\s]*$/;
28
var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
29
var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
30
var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
31
var rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
32
var rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
33
34
function f(n) {
35
// Format integers to have at least two digits.
36
return n < 10
37
? "0" + n
38
: n;
39
}
40
41
function this_value() {
42
return this.valueOf();
43
}
44
45
if (typeof Date.prototype.toJSON !== "function") {
46
47
Date.prototype.toJSON = function () {
48
49
return isFinite(this.valueOf())
50
? this.getUTCFullYear() + "-" +
51
f(this.getUTCMonth() + 1) + "-" +
52
f(this.getUTCDate()) + "T" +
53
f(this.getUTCHours()) + ":" +
54
f(this.getUTCMinutes()) + ":" +
55
f(this.getUTCSeconds()) + "Z"
56
: null;
57
};
58
59
Boolean.prototype.toJSON = this_value;
60
Number.prototype.toJSON = this_value;
61
String.prototype.toJSON = this_value;
62
}
63
64
var gap;
65
var indent;
66
var meta;
67
var rep;
68
69
70
function quote(string) {
71
72
// If the string contains no control characters, no quote characters, and no
73
// backslash characters, then we can safely slap some quotes around it.
74
// Otherwise we must also replace the offending characters with safe escape
75
// sequences.
76
77
rx_escapable.lastIndex = 0;
78
return rx_escapable.test(string)
79
? "\"" + string.replace(rx_escapable, function (a) {
80
var c = meta[a];
81
return typeof c === "string"
82
? c
83
: "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
84
}) + "\""
85
: "\"" + string + "\"";
86
}
87
88
89
function str(key, holder) {
90
91
// Produce a string from holder[key].
92
93
var i; // The loop counter.
94
var k; // The member key.
95
var v; // The member value.
96
var length;
97
var mind = gap;
98
var partial;
99
var value = holder[key];
100
101
// If the value has a toJSON method, call it to obtain a replacement value.
102
103
if (value && typeof value === "object" &&
104
typeof value.toJSON === "function") {
105
value = value.toJSON(key);
106
}
107
108
// If we were called with a replacer function, then call the replacer to
109
// obtain a replacement value.
110
111
if (typeof rep === "function") {
112
value = rep.call(holder, key, value);
113
}
114
115
// What happens next depends on the value's type.
116
117
switch (typeof value) {
118
case "string":
119
return quote(value);
120
121
case "number":
122
123
// JSON numbers must be finite. Encode non-finite numbers as null.
124
125
return isFinite(value)
126
? String(value)
127
: "null";
128
129
case "boolean":
130
case "null":
131
132
// If the value is a boolean or null, convert it to a string. Note:
133
// typeof null does not produce "null". The case is included here in
134
// the remote chance that this gets fixed someday.
135
136
return String(value);
137
138
// If the type is "object", we might be dealing with an object or an array or
139
// null.
140
141
case "object":
142
143
// Due to a specification blunder in ECMAScript, typeof null is "object",
144
// so watch out for that case.
145
146
if (!value) {
147
return "null";
148
}
149
150
// Make an array to hold the partial results of stringifying this object value.
151
152
gap += indent;
153
partial = [];
154
155
// Is the value an array?
156
157
if (Object.prototype.toString.apply(value) === "[object Array]") {
158
159
// The value is an array. Stringify every element. Use null as a placeholder
160
// for non-JSON values.
161
162
length = value.length;
163
for (i = 0; i < length; i += 1) {
164
partial[i] = str(i, value) || "null";
165
}
166
167
// Join all of the elements together, separated with commas, and wrap them in
168
// brackets.
169
170
v = partial.length === 0
171
? "[]"
172
: gap
173
? "[\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "]"
174
: "[" + partial.join(",") + "]";
175
gap = mind;
176
return v;
177
}
178
179
// If the replacer is an array, use it to select the members to be stringified.
180
181
if (rep && typeof rep === "object") {
182
length = rep.length;
183
for (i = 0; i < length; i += 1) {
184
if (typeof rep[i] === "string") {
185
k = rep[i];
186
v = str(k, value);
187
if (v) {
188
partial.push(quote(k) + (
189
gap
190
? ": "
191
: ":"
192
) + v);
193
}
194
}
195
}
196
} else {
197
198
// Otherwise, iterate through all of the keys in the object.
199
200
for (k in value) {
201
if (Object.prototype.hasOwnProperty.call(value, k)) {
202
v = str(k, value);
203
if (v) {
204
partial.push(quote(k) + (
205
gap
206
? ": "
207
: ":"
208
) + v);
209
}
210
}
211
}
212
}
213
214
// Join all of the member texts together, separated with commas,
215
// and wrap them in braces.
216
217
v = partial.length === 0
218
? "{}"
219
: gap
220
? "{\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "}"
221
: "{" + partial.join(",") + "}";
222
gap = mind;
223
return v;
224
}
225
}
226
227
// If the JSON object does not yet have a stringify method, give it one.
228
229
if (typeof JSON.stringify !== "function") {
230
meta = { // table of character substitutions
231
"\b": "\\b",
232
"\t": "\\t",
233
"\n": "\\n",
234
"\f": "\\f",
235
"\r": "\\r",
236
"\"": "\\\"",
237
"\\": "\\\\"
238
};
239
JSON.stringify = function (value, replacer, space) {
240
241
// The stringify method takes a value and an optional replacer, and an optional
242
// space parameter, and returns a JSON text. The replacer can be a function
243
// that can replace values, or an array of strings that will select the keys.
244
// A default replacer method can be provided. Use of the space parameter can
245
// produce text that is more easily readable.
246
247
var i;
248
gap = "";
249
indent = "";
250
251
// If the space parameter is a number, make an indent string containing that
252
// many spaces.
253
254
if (typeof space === "number") {
255
for (i = 0; i < space; i += 1) {
256
indent += " ";
257
}
258
259
// If the space parameter is a string, it will be used as the indent string.
260
261
} else if (typeof space === "string") {
262
indent = space;
263
}
264
265
// If there is a replacer, it must be a function or an array.
266
// Otherwise, throw an error.
267
268
rep = replacer;
269
if (replacer && typeof replacer !== "function" &&
270
(typeof replacer !== "object" ||
271
typeof replacer.length !== "number")) {
272
throw new Error("JSON.stringify");
273
}
274
275
// Make a fake root object containing our value under the key of "".
276
// Return the result of stringifying the value.
277
278
return str("", {"": value});
279
};
280
}
281
282
283
// If the JSON object does not yet have a parse method, give it one.
284
285
if (typeof JSON.parse !== "function") {
286
JSON.parse = function (text, reviver) {
287
288
// The parse method takes a text and an optional reviver function, and returns
289
// a JavaScript value if the text is a valid JSON text.
290
291
var j;
292
293
function walk(holder, key) {
294
295
// The walk method is used to recursively walk the resulting structure so
296
// that modifications can be made.
297
298
var k;
299
var v;
300
var value = holder[key];
301
if (value && typeof value === "object") {
302
for (k in value) {
303
if (Object.prototype.hasOwnProperty.call(value, k)) {
304
v = walk(value, k);
305
if (v !== undefined) {
306
value[k] = v;
307
} else {
308
delete value[k];
309
}
310
}
311
}
312
}
313
return reviver.call(holder, key, value);
314
}
315
316
317
// Parsing happens in four stages. In the first stage, we replace certain
318
// Unicode characters with escape sequences. JavaScript handles many characters
319
// incorrectly, either silently deleting them, or treating them as line endings.
320
321
text = String(text);
322
rx_dangerous.lastIndex = 0;
323
if (rx_dangerous.test(text)) {
324
text = text.replace(rx_dangerous, function (a) {
325
return "\\u" +
326
("0000" + a.charCodeAt(0).toString(16)).slice(-4);
327
});
328
}
329
330
// In the second stage, we run the text against regular expressions that look
331
// for non-JSON patterns. We are especially concerned with "()" and "new"
332
// because they can cause invocation, and "=" because it can cause mutation.
333
// But just to be safe, we want to reject all unexpected forms.
334
335
// We split the second stage into 4 regexp operations in order to work around
336
// crippling inefficiencies in IE's and Safari's regexp engines. First we
337
// replace the JSON backslash pairs with "@" (a non-JSON character). Second, we
338
// replace all simple value tokens with "]" characters. Third, we delete all
339
// open brackets that follow a colon or comma or that begin the text. Finally,
340
// we look to see that the remaining characters are only whitespace or "]" or
341
// "," or ":" or "{" or "}". If that is so, then the text is safe for eval.
342
343
if (
344
rx_one.test(
345
text
346
.replace(rx_two, "@")
347
.replace(rx_three, "]")
348
.replace(rx_four, "")
349
)
350
) {
351
352
// In the third stage we use the eval function to compile the text into a
353
// JavaScript structure. The "{" operator is subject to a syntactic ambiguity
354
// in JavaScript: it can begin a block or an object literal. We wrap the text
355
// in parens to eliminate the ambiguity.
356
357
j = eval("(" + text + ")");
358
359
// In the optional fourth stage, we recursively walk the new structure, passing
360
// each name/value pair to a reviver function for possible transformation.
361
362
return (typeof reviver === "function")
363
? walk({"": j}, "")
364
: j;
365
}
366
367
// If the text is not JSON parseable, then a SyntaxError is thrown.
368
369
throw new SyntaxError("JSON.parse");
370
};
371
}
372
}());
373
374