简体   繁体   中英

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?


Things I have Tried :

  • Libraries
    • Flatted.js
    • Cyro.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)

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.

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 is an simple persistence framework for JavaScript that overcomes two main limitation of JSON persistence:

  • serialijse deals well with cyclic objects.
  • serialijse preserve object class upon deserialization.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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