简体   繁体   English

在javascript中将复杂的,嵌套的,用户定义的对象序列化为字符串

[英]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: 上述目标如下:

  • Nested objects 嵌套对象
  • Circular references to many locations within the object (not just the main reference) 对对象内许多位置的循环引用(不仅仅是主要参考)
  • User-defined objects 用户定义的对象

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 : 我试过的事情

  • Libraries 图书馆
    • Flatted.js Flatted.js
    • Cyro.js Cyro.js
    • JSONfn.js JSONfn.js
  • Searches 搜索
    • Existing Stack Overflow Questions (none seemed to deal with my monstrosity) 现有的堆栈溢出问题(似乎没有处理我的怪物)
    • Google searching serialization of user-defined objects (these could not deal with circular) Google搜索用户定义对象的序列化(这些无法处理循环)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM