Simple C++ serializer
Simple C++ serializer
Then, let’s try to serialize some simple type:
int i = 5; // your value
std::string buf; // your buffer
BytesCount b = Serializer::SerializeUnit<int>::serializeUnit(buf, &i);
Here, ‘b’ is the number of bytes that was read;
And after that, you can try to get your number back:
int i1;
b = Deserializer::DeserializeUnit<int>::deserializeUnit(buf, 0, &i);
Here, the second argument of deserializeUnit is the offset, from which value in the ‘buf’ will be looked for.
It is very convenient to use the ‘serializeAll’ function. It uses variadic templates, and with it you could serialize a bunch of things just in one line:
buf.clear();
double d = 1.1;
std::unordered_map<int, std::string> um{{1, "neko"}, {2, "wanko"}, {3, "manko"}};
std::vector<std::vector<std::string>> vvs{{"some", "thing"}, {"some", "other", "thing"}};
b = Serializer::serializeAll(buf, &um, &d, &vvs);
// getting back
decltype(um) um1;
decltype(vvs) vvs1;
decltype(d) d1;
b = Deserializer::deserializeAll(buf, 0, &um1, &d1, &vvs1);
To serialize array types(including std::array), you shoud use ArrayWrapper
buf.clear();
int* pi = new int[10];
... // initializng 'i'
ArrayWrapper aw{pi, 10};
b = Serializer::serializeAll(buf, &aw);
As you can see, pointers are used as arguments, so if you try to pass a raw pointer as a parameter, just the first value of this type starting from the passed address will be processed.
And, to get it back, we should use ArrayWrapper again.
WARNING: before deserialization, memory for array should be already allocated:
ArrayWrapper<int> aw1;
aw11.start = new int[5];
b = Deserializer::deserializeAll(buf, 0, &aw1);
And, of course, you can serialize your own objects. Here you have two options:
If serialization/deserialization of your object can be done only in one way, you should inherit Serializable/Deserializable class(or both):
```Cpp
struct User : public Serialization::Serializable, public Serialization::Deserializable
{
std::string name;
int age;
std::vector
S::BytesCount serialize(std::string &buf)
{
return S:
:serializeAll(buf, &name, &age, &hobbies);
}
S::BytesCount deserialize(const std::string& buf, S::BytesCount offset)
{
return S:
:deserializeAll(buf, offset, &name, &age, &hobbies);
}
};
…
User user;
user.age = 22;
user.name = “Vasya”;
user.hobbies = {“swimming”, “anime”, “fishing”};
std::string buf;
BytesCount b = Serializer::serializeAll(buf, &user);
- If serialization/deserialization of your object can be done in multiple ways, you should inherit MultipleSerializable/MultipleDeserializable(or both):
```Cpp
struct Cat : public MultipleSerializable, public MultipleDeserializable
{
enum {NameLegsSerializer, AllSerializer, NameLegsDeserializer, AllDeserializer};
Cat()
: MultipleSerializable{}, MultipleDeserializable{}
{
registerSerializer(SerializerId(NameLegsSerializer), [this](std::string& buf) { return Serializer::serializeAll(buf, &name, &legs); } );
registerSerializer(SerializerId(AllSerializer), [this](std::string& buf) { return Serializer::serializeAll(buf, &name, &legs, &age, &places_to_sleep); } );
registerDeserializer(SerializerId(NameLegsDeserializer), [this](const std::string& buf, BytesCount offset) { return Deserializer::deserializeAll(buf, offset, &name, &legs); } );
registerDeserializer(SerializerId(AllDeserializer), [this](const std::string& buf, BytesCount offset) { return Deserializer::deserializeAll(buf, offset, &name, &legs, &age, &places_to_sleep); } );
}
std::string name;
int legs;
short age;
std::vector<std::string> places_to_sleep;
};
...
Cat cat;
cat.age = 5;
cat.legs = 4;
cat.name = "Sugrob";
cat.places_to_sleep = std::move(std::vector<std::string>{"chair", "laptop", "my head"});
cat.setSerializerId((SerializerId)(Cat::AllSerializer));
Serializer::serializeAll(buf, &cat);