我做了我最初担心的事情:我采用了Crockford的代码,并根据需要对其进行了修改。现在,它可以构建JSON但可以处理
- 周期
- 太深的物体
- 数组太长
- 异常(无法合法访问的访问器)
如果有人需要,我在GitHub上建立了一个GitHub存储库:JSON.prune
这是代码:
// JSON.pruned : a function to stringify any object without overflow// example : var json = JSON.pruned({a:'e', c:[1,2,{d:{e:42, f:'deep'}}]})// two additional optional parameters :// - the maximal depth (default : 6)// - the maximal length of arrays (default : 50)// GitHub : https://github.com/Canop/JSON.prune// This is based on Douglas Crockford's pre ( https://github.com/douglascrockford/JSON-js/blob/master/json2.js )(function () { 'use strict'; var DEFAULT_MAX_DEPTH = 6; var DEFAULT_ARRAY_MAX_LENGTH = 50; var seen; // Same variable used for all stringifications Date.prototype.toPrunedJSON = Date.prototype.toJSON; String.prototype.toPrunedJSON = String.prototype.toJSON; var cx = /[u0000u00adu0600-u0604u070fu17b4u17b5u200c-u200fu2028-u202fu2060-u206fufeffufff0-uffff]/g, escapable = /[\"x00-x1fx7f-x9fu00adu0600-u0604u070fu17b4u17b5u200c-u200fu2028-u202fu2060-u206fufeffufff0-uffff]/g, meta = { // table of character substitutions 'b': '\b', 't': '\t', 'n': '\n', 'f': '\f', 'r': '\r', '"' : '\"', '\': '\' }; function quote(string) { escapable.lastIndex = 0; return escapable.test(string) ? '"' + string.replace(escapable, function (a) { var c = meta[a]; return typeof c === 'string' ? c : '\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }) + '"' : '"' + string + '"'; } function str(key, holder, depthDecr, arrayMaxLength) { var i, // The loop counter. k, // The member key. v, // The member value. length, partial, value = holder[key]; if (value && typeof value === 'object' && typeof value.toPrunedJSON === 'function') { value = value.toPrunedJSON(key); } switch (typeof value) { case 'string': return quote(value); case 'number': return isFinite(value) ? String(value) : 'null'; case 'boolean': case 'null': return String(value); case 'object': if (!value) { return 'null'; } if (depthDecr<=0 || seen.indexOf(value)!==-1) { return '"-pruned-"'; } seen.push(value); partial = []; if (Object.prototype.toString.apply(value) === '[object Array]') { length = Math.min(value.length, arrayMaxLength); for (i = 0; i < length; i += 1) { partial[i] = str(i, value, depthDecr-1, arrayMaxLength) || 'null'; } v = partial.length === 0 ? '[]' : '[' + partial.join(',') + ']'; return v; } for (k in value) { if (Object.prototype.hasOwnProperty.call(value, k)) { try { v = str(k, value, depthDecr-1, arrayMaxLength); if (v) partial.push(quote(k) + ':' + v); } catch (e) { // this try/catch due to some "Accessing selectionEnd on an input element that cannot have a selection." on Chrome } } } v = partial.length === 0 ? '{}' : '{' + partial.join(',') + '}'; return v; } } JSON.pruned = function (value, depthDecr, arrayMaxLength) { seen = []; depthDecr = depthDecr || DEFAULT_MAX_DEPTH; arrayMaxLength = arrayMaxLength || DEFAULT_ARRAY_MAX_LENGTH; return str('', {'': value}, depthDecr, arrayMaxLength); };}());
一个可以做什么的例子:
var json = JSON.pruned(window);
注意:
与该答案中的代码相反,GitHub存储库在需要时进行更新(文档,兼容性,在commonjs或节点中用作模块,特定的序列化等)。如果需要此修剪功能,则从存储库开始是个好主意。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)