[英]Serializing a complex, nested, user-defined object into a string in javascript
I am working with the following complicated object: 我正在使用以下复杂的对象:
ObjectA {
somearr: [
ObjectB {
somevar: ObjectD {...},
somecircularvar: pointer
},
ObjectC {...},
...
],
someobj: ObjectE {
someothercircularvar: pointer,
somevar: ObjectF {...}
},
someMethod: ...
}
The above object has the following: 上述目标如下:
Main Question : How do I turn this object
into a string
for storage, and how do I parse the string
back into the object
with all of the methods and variables as is? 主要问题 :如何将此
object
转换为string
以进行存储,以及如何使用所有方法和变量将string
解析回object
?
Things I have Tried : 我试过的事情 :
After trying these I saw that all of the "solutions" deal with circular and nested objects, not user-defined objects . 在尝试这些之后,我看到所有“解决方案”都处理循环和嵌套对象, 而不是用户定义的对象 。
I do remember trying a couple other libraries but none could deal with circular, nested, and user-defined objects all at the same time. 我确实记得尝试过其他几个库,但没有一个可以同时处理循环,嵌套和用户定义的对象。
The closest I have gotten to is the following parse: 我得到的最接近的是以下解析:
{
somearr: [
{
somevar: {...},
somecircularvar: pointer
},
{...},
...
],
someobj: {
someothercircularvar: pointer, // Circular pointer conserved
somevar: {...}
}
}
Notice how my object names have disappeared, and each __proto__
is now the default object (not my object as defined by my local classes) with none of my methods conserved. 注意我的对象名称是如何消失的,每个
__proto__
现在都是默认对象(不是我的本地类定义的对象),我的方法都没有被保存。
Huge thanks in advance to the person that can solve this problem. 非常感谢能够解决这个问题的人。
Right, so it's kind of like you're serializing object data, but not class data. 是的,所以有点像你在序列化对象数据,而不是类数据。 The variables persist, but the meta-stuff about the class (namely methods ) aren't being preserved.
变量仍然存在,但关于类的元数据(即方法 )没有被保留。
One solution might be serialijse . 一种解决方案可能是serialijse 。
serialijse is an simple persistence framework for JavaScript that overcomes two main limitation of JSON persistence:
serialijse是一个简单的JavaScript持久性框架,它克服了JSON持久性的两个主要限制:
- serialijse deals well with cyclic objects.
serialijse处理循环对象。
- serialijse preserve object class upon deserialization.
serialijse在反序列化时保留对象类。
Their third examples demonstrates your case: 他们的第三个例子说明你的情况
// serializing an object with cyclic dependencies
function testing_javascript_serialization_objects_with_cyclic_dependencies() {
var Mary = { name: "Mary", friends: [] };
var Bob = { name: "Bob", friends: [] };
Mary.friends.push(Bob);
Bob.friends.push(Mary);
var group = [ Mary, Bob];
console.log(group);
// testing serialization using JSON.stringify/JSON.parse
try {
var jstr = JSON.stringify(group);
var jo = JSON.parse(jstr);
console.log(jo);
} catch (err) {
console.log(" JSON has failed to manage object with cyclic deps");
console.log(" and has generated the following error message", err.message);
}
// now testing serialization using serialijse serialize/deserialize
var str = s.serialize(group);
var so = s.deserialize(str);
console.log(" However Serialijse knows to manage object with cyclic deps !");
console.log(so);
assert(so[0].friends[0] == so[1]); // Mary's friend is Bob
}
testing_javascript_serialization_objects_with_cyclic_dependencies();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.