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