lundi 6 avril 2020

Cereal alternative to startNode method

I'm using cereal with c++ to serialise and deserialise JSON. My Json objects all have a base class, this sort of maps to the JSON like so...

["baseclass_var1", "baseclass_var2", {"derived_object_var1":"blah", "derived_object_var2":"blahblah"}]

The derived class contributes the JSON object at the end of this JSON array and the base class contributes the values within the array.

My classes look approximately like this,

struct baseObject
{
    baseObject() 
    {
    }

    template<typename Archive>
    void serialize(Archive& archive)
    {
        archive.makeArray();
        archive("baseclass_var1", "baseclass_var2");
    }
};

struct derivedObject : baseObject
{
    template<typename Archive>
    void serialize(Archive& archive)
    {
        baseObject::serialize(archive);
        archive.startNode();
        cereal::nvp(archive, "derived_object_var1", "blah");
        cereal::nvp(archive, "derived_object_var2", "blahblah");
        archive.finishNode();
    }
}

I can't help but feel like considering how easy cereal is to use, where the structure of the JSON is implicitly defined, I might be making a mistake by having to explicitly call startNode(), finishNode() and makeArray()? Is there an alternative implementation that would take care of the JSON structure for me, without having to explicitly state where I want the sub node to be?

Aucun commentaire:

Enregistrer un commentaire