PageSourceSearch

https://www.amlogic.com/js/json2.js

js amlogic.com collected 2026-10-02 04:09:38 UTC 17,449 bytes, 454 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.