0

Im trying to learn how to use serialization/deserialization at the moment, using cereal. To test things out, I serialized the objects of a 3D scene into a .xml file (it's easier to understand when you can actually read the output, after all). The serialization works without problems, and the deserialization also seems to do so. When I now want to recreate the objects, I have no problem accessing the first one...but how do I get the rest ?

Serialization in the .cpp (abbreviated):

std::ofstream os("testdata.xml");


for (int i=0; i<alloftheobjects; i++)
{
    3DObject *Object = new 3DObject;

    Object->ID = ID;
    Object->vertices = vectorOfPoints;
    Object->triangles = vectorOfTriangles;

    cereal::XMLOutputArchive archive(os);
    archive(cereal::make_nvp("ID", Object->ID),
            cereal::make_nvp("Points", Object->vertices),
            cereal::make_nvp("Triangles", Object->triangles));
    delete Object;
}

It works, and creates testdata.xml, looking something like this:

<?xml version="1.0" encoding="utf-8"?>
<cereal>
<ID>0111</ID>
<Points  size="dynamic">
    <value0 size="dynamic">
        <value0>-5</value0>
        <value1>-5</value1>
        <value2>1</value2>
    </value0>
    ....rest of the points
</Points >
<Triangles size="dynamic">
    ...triangle data
</Triangles>
</cereal>

<?xml version="1.0" encoding="utf-8"?>
<cereal>
<ID>0112</ID>
<Points  size="dynamic">
    ...pointdata
</Points >
<Triangles size="dynamic">
    ...triangle data
</Triangles>
</cereal>
...

etc.

When I now use

    std::ifstream is("testsdata.xml");
    cereal::XMLInputArchive archive(is);

    int ID;
    std::vector<std::vector<double>> vertices;
    std::vector<std::vector<int>> triangles;


        archive(ID, vertices, triangles);

to deserialize, it compiles, runs and I can access the first set of data (ID, vertices, triangles, up to the first </cereal>. But I have no bloody clue how I can access the rest of it.

It is quite possible that Im missing something blatantly obvious and Im overlooking something while staring right at it. But Im also not sure if serializing the data this way is even a sound approach.

UnHeinz
  • 1
  • 3

1 Answers1

0

Your problem is that you are making a new archive every time the for loop repeats.

This should fix your problem:

std::ofstream os("testdata.xml");
cereal::XMLOutputArchive archive(os);

for (int i=0; i<alloftheobjects; i++)
{
    3DObject *Object = new 3DObject;

    Object->ID = ID;
    Object->vertices = vectorOfPoints;
    Object->triangles = vectorOfTriangles;

    archive(cereal::make_nvp("ID", Object->ID),
            cereal::make_nvp("Points", Object->vertices),
            cereal::make_nvp("Triangles", Object->triangles));
    delete Object;
}
Dallas
  • 1
  • Sorry for answering that late. That doesn't change much, actually it adds some more weird lines of ` ` in between the datasets. – UnHeinz May 03 '16 at 12:08