项目作者: onyazuka

项目描述 :
Simple C++ serializer
高级语言: C++
项目地址: git://github.com/onyazuka/Serializer.git
创建时间: 2019-04-05T08:06:46Z
项目社区:https://github.com/onyazuka/Serializer

开源协议:MIT License

下载


Serializer

Simple C++ serializer

Description


Simple c++ serialization library.
It allows you to serialize such types:

  • Simple arithmetic types
  • Arrays
  • All std containers
  • Your custom types


It makes a std::string, filled with binary data, on output. Why use std::string? Later, you can write it to a file or make some manipulations and write it somewhere after that.

Requirements


It is just one header, without any dependencies. So, any c++ compiler that supports c++17 should be ok, but I have only tested it with g++.

Examples


First of all you should include “serializer.hpp” header and, if you want, do “using namespace Serialization;”.

Then, let’s try to serialize some simple type:

  1. int i = 5; // your value
  2. std::string buf; // your buffer
  3. 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:

  1. int i1;
  2. 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:

  1. buf.clear();
  2. double d = 1.1;
  3. std::unordered_map<int, std::string> um{{1, "neko"}, {2, "wanko"}, {3, "manko"}};
  4. std::vector<std::vector<std::string>> vvs{{"some", "thing"}, {"some", "other", "thing"}};
  5. b = Serializer::serializeAll(buf, &um, &d, &vvs);
  6. // getting back
  7. decltype(um) um1;
  8. decltype(vvs) vvs1;
  9. decltype(d) d1;
  10. b = Deserializer::deserializeAll(buf, 0, &um1, &d1, &vvs1);

To serialize array types(including std::array), you shoud use ArrayWrapper from Serialization namespace:

  1. buf.clear();
  2. int* pi = new int[10];
  3. ... // initializng 'i'
  4. ArrayWrapper aw{pi, 10};
  5. 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:

  1. ArrayWrapper<int> aw1;
  2. aw11.start = new int[5];
  3. 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 hobbies;

    S::BytesCount serialize(std::string &buf)
    {

    1. return S::Serializer::serializeAll(buf, &name, &age, &hobbies);

    }

    S::BytesCount deserialize(const std::string& buf, S::BytesCount offset)
    {

    1. return S::Deserializer::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);

  1. - If serialization/deserialization of your object can be done in multiple ways, you should inherit MultipleSerializable/MultipleDeserializable(or both):
  2. ```Cpp
  3. struct Cat : public MultipleSerializable, public MultipleDeserializable
  4. {
  5. enum {NameLegsSerializer, AllSerializer, NameLegsDeserializer, AllDeserializer};
  6. Cat()
  7. : MultipleSerializable{}, MultipleDeserializable{}
  8. {
  9. registerSerializer(SerializerId(NameLegsSerializer), [this](std::string& buf) { return Serializer::serializeAll(buf, &name, &legs); } );
  10. registerSerializer(SerializerId(AllSerializer), [this](std::string& buf) { return Serializer::serializeAll(buf, &name, &legs, &age, &places_to_sleep); } );
  11. registerDeserializer(SerializerId(NameLegsDeserializer), [this](const std::string& buf, BytesCount offset) { return Deserializer::deserializeAll(buf, offset, &name, &legs); } );
  12. registerDeserializer(SerializerId(AllDeserializer), [this](const std::string& buf, BytesCount offset) { return Deserializer::deserializeAll(buf, offset, &name, &legs, &age, &places_to_sleep); } );
  13. }
  14. std::string name;
  15. int legs;
  16. short age;
  17. std::vector<std::string> places_to_sleep;
  18. };
  19. ...
  20. Cat cat;
  21. cat.age = 5;
  22. cat.legs = 4;
  23. cat.name = "Sugrob";
  24. cat.places_to_sleep = std::move(std::vector<std::string>{"chair", "laptop", "my head"});
  25. cat.setSerializerId((SerializerId)(Cat::AllSerializer));
  26. Serializer::serializeAll(buf, &cat);