Ticket #16669: json2.diff
File json2.diff, 7.6 KB (added by , 14 years ago) |
---|
-
wp-includes/js/json2.dev.js
1 1 /* 2 2 http://www.JSON.org/json2.js 3 20 09-08-173 2011-02-23 4 4 5 5 Public Domain. 6 6 … … 8 8 9 9 See http://www.JSON.org/js.html 10 10 11 12 This code should be minified before deployment. 13 See http://javascript.crockford.com/jsmin.html 14 15 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO 16 NOT CONTROL. 17 18 11 19 This file creates a global JSON object containing two methods: stringify 12 20 and parse. 13 21 … … 136 144 137 145 This is a reference implementation. You are free to copy, modify, or 138 146 redistribute. 139 140 This code should be minified before deployment.141 See http://javascript.crockford.com/jsmin.html142 143 USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO144 NOT CONTROL.145 147 */ 146 148 147 /*jslint evil: true */149 /*jslint evil: true, strict: false, regexp: false */ 148 150 149 151 /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, 150 152 call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, … … 153 155 test, toJSON, toString, valueOf 154 156 */ 155 157 156 "use strict";157 158 158 159 // Create a JSON object only if one does not already exist. We create the 159 160 // methods in a closure to avoid creating global variables. 160 161 161 if (!this.JSON) { 162 this.JSON = {}; 162 var JSON; 163 if (!JSON) { 164 JSON = {}; 163 165 } 164 166 165 167 (function () { 168 "use strict"; 166 169 167 170 function f(n) { 168 171 // Format integers to have at least two digits. … … 174 177 Date.prototype.toJSON = function (key) { 175 178 176 179 return isFinite(this.valueOf()) ? 177 this.getUTCFullYear()+ '-' +178 179 180 181 182 180 this.getUTCFullYear() + '-' + 181 f(this.getUTCMonth() + 1) + '-' + 182 f(this.getUTCDate()) + 'T' + 183 f(this.getUTCHours()) + ':' + 184 f(this.getUTCMinutes()) + ':' + 185 f(this.getUTCSeconds()) + 'Z' : null; 183 186 }; 184 187 185 String.prototype.toJSON =186 Number.prototype.toJSON=187 Boolean.prototype.toJSON = function (key) {188 return this.valueOf();189 };188 String.prototype.toJSON = 189 Number.prototype.toJSON = 190 Boolean.prototype.toJSON = function (key) { 191 return this.valueOf(); 192 }; 190 193 } 191 194 192 195 var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, … … 213 216 // sequences. 214 217 215 218 escapable.lastIndex = 0; 216 return escapable.test(string) ? 217 '"' + string.replace(escapable, function (a) { 218 var c = meta[a]; 219 return typeof c === 'string' ? c : 220 '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); 221 }) + '"' : 222 '"' + string + '"'; 219 return escapable.test(string) ? '"' + string.replace(escapable, function (a) { 220 var c = meta[a]; 221 return typeof c === 'string' ? c : 222 '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); 223 }) + '"' : '"' + string + '"'; 223 224 } 224 225 225 226 … … 302 303 // Join all of the elements together, separated with commas, and wrap them in 303 304 // brackets. 304 305 305 v = partial.length === 0 ? '[]' : 306 gap ? '[\n' + gap + 307 partial.join(',\n' + gap) + '\n' + 308 mind + ']' : 309 '[' + partial.join(',') + ']'; 306 v = partial.length === 0 ? '[]' : gap ? 307 '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : 308 '[' + partial.join(',') + ']'; 310 309 gap = mind; 311 310 return v; 312 311 } … … 316 315 if (rep && typeof rep === 'object') { 317 316 length = rep.length; 318 317 for (i = 0; i < length; i += 1) { 319 k = rep[i];320 if (typeof k === 'string') {318 if (typeof rep[i] === 'string') { 319 k = rep[i]; 321 320 v = str(k, value); 322 321 if (v) { 323 322 partial.push(quote(k) + (gap ? ': ' : ':') + v); … … 329 328 // Otherwise, iterate through all of the keys in the object. 330 329 331 330 for (k in value) { 332 if (Object. hasOwnProperty.call(value, k)) {331 if (Object.prototype.hasOwnProperty.call(value, k)) { 333 332 v = str(k, value); 334 333 if (v) { 335 334 partial.push(quote(k) + (gap ? ': ' : ':') + v); … … 341 340 // Join all of the member texts together, separated with commas, 342 341 // and wrap them in braces. 343 342 344 v = partial.length === 0 ? '{}' : 345 gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +346 mind + '}' :'{' + partial.join(',') + '}';343 v = partial.length === 0 ? '{}' : gap ? 344 '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : 345 '{' + partial.join(',') + '}'; 347 346 gap = mind; 348 347 return v; 349 348 } … … 384 383 rep = replacer; 385 384 if (replacer && typeof replacer !== 'function' && 386 385 (typeof replacer !== 'object' || 387 386 typeof replacer.length !== 'number')) { 388 387 throw new Error('JSON.stringify'); 389 388 } 390 389 … … 414 413 var k, v, value = holder[key]; 415 414 if (value && typeof value === 'object') { 416 415 for (k in value) { 417 if (Object. hasOwnProperty.call(value, k)) {416 if (Object.prototype.hasOwnProperty.call(value, k)) { 418 417 v = walk(value, k); 419 418 if (v !== undefined) { 420 419 value[k] = v; … … 432 431 // Unicode characters with escape sequences. JavaScript handles many characters 433 432 // incorrectly, either silently deleting them, or treating them as line endings. 434 433 434 text = String(text); 435 435 cx.lastIndex = 0; 436 436 if (cx.test(text)) { 437 437 text = text.replace(cx, function (a) { … … 453 453 // we look to see that the remaining characters are only whitespace or ']' or 454 454 // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. 455 455 456 if (/^[\],:{}\s]*$/ .457 test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@'). 458 replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). 459 replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {456 if (/^[\],:{}\s]*$/ 457 .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') 458 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') 459 .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { 460 460 461 461 // In the third stage we use the eval function to compile the text into a 462 462 // JavaScript structure. The '{' operator is subject to a syntactic ambiguity … … 478 478 }; 479 479 } 480 480 }()); 481