使用JSON序列化图论树的解决方法
Workaround for serializing a graph-theoretic tree with JSON?
我有一个节点数组。每个节点都有一个子节点数组和一个指向其父节点的指针。我想使用JSON.stringify对它进行序列化,但对于父指针,我显然最终得到了循环引用,JSON抛出了一个异常。我可以做些什么来处理循环引用并使用JSON进行序列化?
相关问题:Chrome发送请求错误:TypeError:将循环结构转换为JSON
您应该在具有父对象的对象中创建一个自定义的toJson函数。
从文件
如果要字符串化的对象有一个名为toJSON的属性,其值是一个函数,则toJSON方法自定义JSON字符串行为
var x = {
foo: 'foo',
toJSON: function () {
return 'bar';
}
};
var json = JSON.stringify({x: x});
所以你可以在有父引用的对象中创建这个函数,也许是这样的?
MyObj = function(){
this.xxx = 'foobar';
this.zzz = 'foooobar';
this.name = 'foo';
this.parent = ...;
toJSON = function(){
tmp = '{'
for(prop in MyObj){
if(prop == 'parent'){
tmp += 'parent: "'+ this['parent'].name +'"'; //maybe?? optional!
}else{
tmp += prop + ':' + this[prop].stringify + ','; //you will still use the browser function
}
tmp += '}
}
return tmp;
}
}
尝试一下——使用信息和演示数据在底部:
// class
var Decircularizer = function() {};
// class contents
Decircularizer.prototype = {
curId: 0x01,
idField: '{`id´}',
_getNewId: function() {
return this.curId++;
},
_getVisited: function(obj) {
return obj[''+this.idField];
},
_setVisited: function(obj) {
return (obj[''+this.idField] = this._getNewId());
},
_removeVisited: function(obj){
delete obj['' + this.idField];
},
decycle: function(obj, depth) {
var $this = this;
depth = depth || 0;
var key = this._getVisited(obj);
if (key > 0x00) return this.idField + key;
if(!jQuery.isPlainObject(obj))
return obj;
key = this._setVisited(obj);
jQuery.each(obj, function(prop, val){
if (prop == $this.idField) return;
if (jQuery.isFunction(obj[prop])) delete obj[prop];
else obj[prop] = $this.decycle(val, depth + 1);
});
return obj;
},
recycle: function(obj){
var $this = this;
var ids = {};
this._searchIds(obj, ids);
return this._recycle(obj, ids);
},
_recycle: function(obj, ids){
var $this = this;
if(!jQuery.isPlainObject(obj))
return obj;
jQuery.each(obj, function(prop, val){
var xval = ids[val];
if(xval)
obj[prop] = xval;
else
obj[prop] = $this._recycle(val, ids);
});
return obj;
},
_searchIds: function(obj, ids){
var $this = this
var ids = ids || {};
var key = this._getVisited(obj);
if(key > 0x00) {
ids[this.idField + key] = obj;
$this._removeVisited(obj);
}
if(!jQuery.isPlainObject(obj))
return ids;
jQuery.each(obj, function(prop, val){
$this._searchIds(val, ids);
});
return ids;
}
};
// EXAMPLE DATA
var a = {};
var a2 = {}
a.b = a;
a.c = "hallo";
a.e = 123;
a.f = a2;
a2.x = a;
// USAGE
// new class
var ser = new Decircularizer();
// uncycle
var cleanObjectWithOutCirculars = ser.decycle(a);
console.debug(cleanObjectWithOutCirculars);
/* object looks like
Object {c: "hallo", e: 123, b: "{`id´}1", {`id´}: 1, f: Object}
b: "{`id´}1"
c: "hallo"
e: 123
f: Object
x: "{`id´}1"
{`id´}: 2
__proto__: Object
{`id´}: 1
__proto__: Object
*/
// recycle
var aNew = ser.recycle(cleanObjectWithOutCirculars);
console.debug(aNew)
/*
Object {c: "hallo", e: 123, b: Object, f: Object}
b: Object
b: Object
c: "hallo"
e: 123
f: Object
x: Object
__proto__: Object
__proto__: Object
c: "hallo"
e: 123
f: Object
x: Object
b: Object
c: "hallo"
e: 123
f: Object
__proto__: Object
__proto__: Object
__proto__: Object
*/
// correct (Y)
相关文章:
- 使用JSON序列化图论树的解决方法
- X-Content-Type-Options=nosniff解决方法
- 供应商前缀 CSS 解决方法(引导程序)的速度和大小
- 无法使用HTML5 Web存储保存布尔值的解决方法
- HTML”;文件“/OpenFile对话框解决方法
- 多行链接中引导工具提示的解决方法
- 使用jsonp解决方法进行同步ajax调用
- YQL推特时间线超出速率限制.解决方法
- 嵌套文档.writes和第三方广告在IE中不起作用!!!!任何安全的解决方法
- 加载外部页面的跨域解决方法
- Emberjs - 基于控制器属性进行过滤的奇怪解决方法
- Z 索引绝对定位元素低于父级,具有 z 索引解决方法
- 安卓浏览器触端事件错误解决方法
- 有什么方法可以将类型指定为 self?或解决方法“属性类型不兼容”
- SVG 符号不会触发加载事件(仅在 Firefox 中).我寻找解决方法
- IE10 背景位置 x 的解决方法
- 在 IE11 中添加子项时未更新空选择器的解决方法
- 安卓浏览器上的滚动事件未触发.需要解决方法
- IE8 中 data:uri 的 32KB 限制的任何解决方法
- 溢出-x:可见;不适用于溢出-y:自动;任何解决方法