/* 2007-07-19 版本 Summer 整理和修正于 2007-7-23 10:54 这里出现后将移到Jcore.js里,以后章节将不再出现 */ // 这里防止多次引入重复定义 if (!Object.prototype.toJSONString) { // 数组的串形化 Array.prototype.toJSONString = function () { var a = [], // 各子元素串形化缓存 i, // 循环计数 l = this.length, v, // String化的value _this = this; // 这里修改为高性能循环方式 var n = l % 8, nOldN = n, i = 0; while(0 < n--) { v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function [^\(]*\(/m, "function(")); } }; n = (l - nOldN) / 8; while(0 < n--) { // 1 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\() *\(/m, "function(")); } // 2 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\()*\ (/m, "function(")); } // 3 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\()*\ (/m, "function(")); } // 4 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\() *\(/m, "function(")); } // 5 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\()*\ (/m, "function(")); } // 6 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\()*\ (/m, "function(")); } // 7 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\()*\ (/m, "function(")); } // 8 v = _this[i++]; switch (typeof v) { case 'object': if (v) { if (typeof v.toJSONString === 'function') { a.push(v.toJSONString()); } } else { a.push('null'); } break;
case 'string': case 'number': case 'boolean': a.push(v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function':a.push(v.toString().replace(/function[^\()*\ (/m, "function(")); } }; return '[' + a.join(',') + ']'; }; Boolean.prototype.toJSONString = function () { return String(this); }; // 这里没有支持format的格式,确实有些遗憾 Date.prototype.toJSONString = function () { function f(n) { return n < 10 ? '0' + n : n; } // 如果你要其他格式,可以修改下面的代码 return ['"', this.getUTCFullYear(),'-', f(this.getUTCMonth() + 1) , '-', f(this.getUTCDate()), 'T', f(this.getUTCHours()), ':', f(this.getUTCMinutes()), ':', f(this.getUTCSeconds()), 'Z"'].join(""); };
Number.prototype.toJSONString = function () { return isFinite(this) ? String(this) : 'null'; };
Object.prototype.toJSONString = function () { var a = [], // 各子元素串形化缓存 k, // 当前key v; // 当前元素的值 for (k in this) { if(typeof k === 'string' && this.hasOwnProperty(k)) { v = this[k]; switch (typeof v) { case 'object': if(v) { if (typeof v.toJSONString === 'function') { a.push(k.toJSONString() + ':' + v.toJSONString()); } } else { a.push(k.toJSONString() + ':null'); } break;
case 'string': case 'number': case 'boolean': a.push(k.toJSONString() + ':' + v.toJSONString()); // 原有版本对"function"的处理是有问题的,下面的代码是我加的 break; case 'function': a.push(k.toJSONString() + ':' + v.toString().replace (/function[^\(]*\(/m, "function(")); } } } return '{' + a.join(',') + '}'; };
// 这里使用匿名函数对String对象进行格式化下面的符号 (function (s) { var m = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\' };
s.parseJSON = function (filter) { var j; function walk(k, v) { var i; if (v && typeof v === 'object') { for (i in v) { if (v.hasOwnProperty(i)) { v[i] = walk(i, v[i]); } } } return filter(k, v); } if (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$/.test(this. replace(/\\./g, '@'). replace(/"[^"\\\n\r]*"/g, ''))) { j = eval('(' + this + ')'); return typeof filter === 'function' ? walk('', j) : j; } throw new SyntaxError('parseJSON'); }; s.toJSONString = function () { if (/["\\\x00-\x1f]/.test(this)) { return '"' + this.replace(/([\x00-\x1f\\"])/g, function (a, b) { var c = m[b]; if (c) { return c; } c = b.charCodeAt(); return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); }) + '"'; } return '"' + this + '"'; }; })(String.prototype); }
|