2 * Copyright 2016-2017, CMCC Technologies Co., Ltd.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 if (typeof JSON !== 'object') {
\r
25 // Format integers to have at least two digits.
\r
26 return n < 10 ? '0' + n : n;
\r
29 if (typeof Date.prototype.toJSON !== 'function') {
\r
31 Date.prototype.toJSON = function (key) {
\r
33 return isFinite(this.valueOf())
\r
34 ? this.getUTCFullYear() + '-' +
\r
35 f(this.getUTCMonth() + 1) + '-' +
\r
36 f(this.getUTCDate()) + 'T' +
\r
37 f(this.getUTCHours()) + ':' +
\r
38 f(this.getUTCMinutes()) + ':' +
\r
39 f(this.getUTCSeconds()) + 'Z'
\r
43 String.prototype.toJSON =
\r
44 Number.prototype.toJSON =
\r
45 Boolean.prototype.toJSON = function (key) {
\r
46 return this.valueOf();
\r
50 var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
\r
51 escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
\r
54 meta = { // table of character substitutions
\r
66 function quote(string) {
\r
68 // If the string contains no control characters, no quote characters, and no
\r
69 // backslash characters, then we can safely slap some quotes around it.
\r
70 // Otherwise we must also replace the offending characters with safe escape
\r
73 escapable.lastIndex = 0;
\r
74 return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
\r
76 return typeof c === 'string'
\r
78 : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
\r
79 }) + '"' : '"' + string + '"';
\r
83 function str(key, holder) {
\r
85 // Produce a string from holder[key].
\r
87 var i, // The loop counter.
\r
88 k, // The member key.
\r
89 v, // The member value.
\r
93 value = holder[key];
\r
95 // If the value has a toJSON method, call it to obtain a replacement value.
\r
97 if (value && typeof value === 'object' &&
\r
98 typeof value.toJSON === 'function') {
\r
99 value = value.toJSON(key);
\r
102 // If we were called with a replacer function, then call the replacer to
\r
103 // obtain a replacement value.
\r
105 if (typeof rep === 'function') {
\r
106 value = rep.call(holder, key, value);
\r
109 // What happens next depends on the value's type.
\r
111 switch (typeof value) {
\r
113 return quote(value);
\r
117 // JSON numbers must be finite. Encode non-finite numbers as null.
\r
119 return isFinite(value) ? String(value) : 'null';
\r
124 // If the value is a boolean or null, convert it to a string. Note:
\r
125 // typeof null does not produce 'null'. The case is included here in
\r
126 // the remote chance that this gets fixed someday.
\r
128 return String(value);
\r
130 // If the type is 'object', we might be dealing with an object or an array or
\r
135 // Due to a specification blunder in ECMAScript, typeof null is 'object',
\r
136 // so watch out for that case.
\r
142 // Make an array to hold the partial results of stringifying this object value.
\r
147 // Is the value an array?
\r
149 if (Object.prototype.toString.apply(value) === '[object Array]') {
\r
151 // The value is an array. Stringify every element. Use null as a placeholder
\r
152 // for non-JSON values.
\r
154 length = value.length;
\r
155 for (i = 0; i < length; i += 1) {
\r
156 partial[i] = str(i, value) || 'null';
\r
159 // Join all of the elements together, separated with commas, and wrap them in
\r
162 v = partial.length === 0
\r
165 ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']'
\r
166 : '[' + partial.join(',') + ']';
\r
171 // If the replacer is an array, use it to select the members to be stringified.
\r
173 if (rep && typeof rep === 'object') {
\r
174 length = rep.length;
\r
175 for (i = 0; i < length; i += 1) {
\r
176 if (typeof rep[i] === 'string') {
\r
180 partial.push(quote(k) + (gap ? ': ' : ':') + v);
\r
186 // Otherwise, iterate through all of the keys in the object.
\r
189 if (Object.prototype.hasOwnProperty.call(value, k)) {
\r
192 partial.push(quote(k) + (gap ? ': ' : ':') + v);
\r
198 // Join all of the member texts together, separated with commas,
\r
199 // and wrap them in braces.
\r
201 v = partial.length === 0
\r
204 ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}'
\r
205 : '{' + partial.join(',') + '}';
\r
211 // If the JSON object does not yet have a stringify method, give it one.
\r
213 if (typeof JSON.stringify !== 'function') {
\r
214 JSON.stringify = function (value, replacer, space) {
\r
216 // The stringify method takes a value and an optional replacer, and an optional
\r
217 // space parameter, and returns a JSON text. The replacer can be a function
\r
218 // that can replace values, or an array of strings that will select the keys.
\r
219 // A default replacer method can be provided. Use of the space parameter can
\r
220 // produce text that is more easily readable.
\r
226 // If the space parameter is a number, make an indent string containing that
\r
229 if (typeof space === 'number') {
\r
230 for (i = 0; i < space; i += 1) {
\r
234 // If the space parameter is a string, it will be used as the indent string.
\r
236 } else if (typeof space === 'string') {
\r
240 // If there is a replacer, it must be a function or an array.
\r
241 // Otherwise, throw an error.
\r
244 if (replacer && typeof replacer !== 'function' &&
\r
245 (typeof replacer !== 'object' ||
\r
246 typeof replacer.length !== 'number')) {
\r
247 throw new Error('JSON.stringify');
\r
250 // Make a fake root object containing our value under the key of ''.
\r
251 // Return the result of stringifying the value.
\r
253 return str('', {'': value});
\r
258 // If the JSON object does not yet have a parse method, give it one.
\r
260 if (typeof JSON.parse !== 'function') {
\r
261 JSON.parse = function (text, reviver) {
\r
263 // The parse method takes a text and an optional reviver function, and returns
\r
264 // a JavaScript value if the text is a valid JSON text.
\r
268 function walk(holder, key) {
\r
270 // The walk method is used to recursively walk the resulting structure so
\r
271 // that modifications can be made.
\r
273 var k, v, value = holder[key];
\r
274 if (value && typeof value === 'object') {
\r
276 if (Object.prototype.hasOwnProperty.call(value, k)) {
\r
277 v = walk(value, k);
\r
278 if (v !== undefined) {
\r
286 return reviver.call(holder, key, value);
\r
290 // Parsing happens in four stages. In the first stage, we replace certain
\r
291 // Unicode characters with escape sequences. JavaScript handles many characters
\r
292 // incorrectly, either silently deleting them, or treating them as line endings.
\r
294 text = String(text);
\r
296 if (cx.test(text)) {
\r
297 text = text.replace(cx, function (a) {
\r
299 ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
\r
303 // In the second stage, we run the text against regular expressions that look
\r
304 // for non-JSON patterns. We are especially concerned with '()' and 'new'
\r
305 // because they can cause invocation, and '=' because it can cause mutation.
\r
306 // But just to be safe, we want to reject all unexpected forms.
\r
308 // We split the second stage into 4 regexp operations in order to work around
\r
309 // crippling inefficiencies in IE's and Safari's regexp engines. First we
\r
310 // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
\r
311 // replace all simple value tokens with ']' characters. Third, we delete all
\r
312 // open brackets that follow a colon or comma or that begin the text. Finally,
\r
313 // we look to see that the remaining characters are only whitespace or ']' or
\r
314 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
\r
316 if (/^[\],:{}\s]*$/
\r
317 .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
\r
318 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
\r
319 .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
\r
321 // In the third stage we use the eval function to compile the text into a
\r
322 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
\r
323 // in JavaScript: it can begin a block or an object literal. We wrap the text
\r
324 // in parens to eliminate the ambiguity.
\r
326 j = eval('(' + text + ')');
\r
328 // In the optional fourth stage, we recursively walk the new structure, passing
\r
329 // each name/value pair to a reviver function for possible transformation.
\r
331 return typeof reviver === 'function'
\r
332 ? walk({'': j}, '')
\r
336 // If the text is not JSON parseable, then a SyntaxError is thrown.
\r
338 throw new SyntaxError('JSON.parse');
\r