I tried the Cereal library for the first time today and it seems really great.
However, I was wondering if we must create template specialization for the ::serialize(Archive & ar) method for each class we want to serialize, with all Archive possibilities?
As far as I can see, this is the only way to do if you have your classes defined into a library, then you want to call the serialization from a program with the library linked to it.
Here's an example :
Library :
// color.hpp
struct Color {
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
m_data[0] = r;
m_data[1] = g;
m_data[2] = b;
m_data[3] = a;
}
uint8_t m_data[4];
template<class Archive>
void serialize(Archive & ar)
{
ar(m_data);
}
}
Program :
// main.cpp
#include <color.h>
#include <iostream>
#include <cereal/archives/json.hpp>
int main()
{
cereal::JSONOutputArchive ar(std::cout);
ar(Color(1,2,3,4));
std::cout << std::endl;
}
Of course, since serialize is a template function, I receive a link error :
Undefined symbols for architecture x86_64:
"void Color::serialize<cereal::JSONOutputArchive>(cereal::JSONOutputArchive&)", referenced from:
...
So does that mean I have to create template specialization for my hundreds of classes that need to be serialized, multiplied by the number of available Archives?
Other question completely different, but how to interpret my class Color as a primitive? I would like a JSON serialization like this : [1,2,3,4] and not {"value0", {"value1": { "value2": 1, "value3": 2, "value4": 3, "value5": 4 } }}
Thank you.