bitsery icon indicating copy to clipboard operation
bitsery copied to clipboard

How to serializate and deserializate std::vector<std::any> or std::vector<std::variant> ?

Open Curry-Messi opened this issue 5 years ago • 1 comments

Curry-Messi avatar Aug 03 '20 07:08 Curry-Messi

Hi, Variant serialization is straightforward. An example of how different variant types are handled is provided here. To serialize vector of variants you need to use lambda overload for container method. The easiest way is probably to define serialize function for your variant, then it could look like this:

using MyVariant = std::variant<int64_t, MyTuple, MyStruct>;
   ...
template <typename S>
void serialize(S& s, MyVariant& obj) {
   ...
}
   ...
ser.container(obj, [](S& s, MyVariant& data) { s.ext(data, bitsery::ext::StdVariant{});});

As for std::any it's worse... there is no extension for it, so for the moment, you need to it write your self.

fraillt avatar Aug 07 '20 12:08 fraillt