How to serialize a tree structure in C ++?

I am trying to serialize / deserialize a game scene to send / receive a network and save / load from / to disk.

My game engine uses nodes and components, and as such, these are the only objects that require serialization. The scene may look like this:

Root Node - Node - SpecializedComponent - SpecializedComponent - Node - Node - Node - Node - Node - Node - Node - SpecializedComponent - Node 

A Node is basically:

 class Node { map<String, Node> mChildren; map<String, Component> mComponents; uuid_t mId; Node* mParent; }; 

A SpecializedComponent is basically:

 class SpecializedComponent : public Component { uuid_t mId; Node* mNode; }; 

I would like to use YAML or JSON for my textual representation. I have Qt, Boost, and any other library that I would like to have at my disposal, so dependencies are not a problem. Actually the nodes are already Q_OBJECTS, so I have a reflection.

Despite the reflection, deserializing this correct return to the C ++ tree structure seems to be a problem.

Optimally, I would like to get an elegant and efficient solution for serializing / deserializing such a structure in both binary and text format.

+6
source share
4 answers

A recursive descent parser is usually a simpler and more powerful way to handle part of the recovery. I could try setting some code to show in concrete, in a pseudo-code something like this:

  Node *parse(stream s) { content = new Node; s >> content >> nsons; for (int c = 0; c < nsons; ++c) content->addChild(parse(s)); return content; } 

Of course, when a component is read, we must check the type.

+3
source

I suggest you use the protocol protocol library for this. Since you serialize C ++ data to send over the network, you will be very useful for this library.

+2
source

Serialize node attributes Serialize the components for this node. IE serializes the number of child components, then each component.

To serialize a component, you must bind an Id to each type of component (either int or string) so that you can determine how to deserialize a specific component of type hs) Serialize the number of child nodes Serialize the child nodes (call the same function recursively )

Then deserialization is the exact world. Unserialize, node attributes, and then the number of components. For each component, get the type of component, deserialize it according to that type) and so on ...

If you use Qt, QTextStream / QDataStream are the easiest options. The binary format must be well known during deserialization, so you must put the version number or identifier at the beginning of your serialized content in order to add or change it later.

Also, if you must be compatible with other languages โ€‹โ€‹(like java), keep in mind that Qt does not use the same norms for types as the string

+1
source

I think you can do it with Boost Serialization . It supports serialization of all STL containers, including std::map .

+1
source

Source: https://habr.com/ru/post/899908/


All Articles