I am using the cereal library. The idea is to have a base class that serializes itself, and the derived class would serialize itself, and the base class. This is an example almost verbatim from the library documentation, but it doesn't work the same way the documentation claims - only the base serialize is ever called.
#include <cereal/archives/json.hpp>
#include <cereal/types/polymorphic.hpp>
#include <fstream>
struct EmptyBase
{
public:
virtual void foo() = 0;
std::string name;
template <class Archive>
void serialize( Archive & ar )
{
std::cout << "Base serialize" << std::endl;
ar(name);
}
void SaveToFile(const std::string& filename) const
{
std::ofstream os(filename);
cereal::JSONOutputArchive ar(os);
ar(cereal::make_nvp("settings", *this));
}
void LoadFromFile(const std::string& filename)
{
std::ifstream is(filename);
cereal::JSONInputArchive ar(is);
ar(cereal::make_nvp("settings", *this));
}
};
struct DerivedTwo: EmptyBase
{
public:
void foo() {}
std::string why;
template <class Archive>
void serialize( Archive & ar )
{
std::cout << "Child serialize" << std::endl;
ar(cereal::base_class<EmptyBase>(this));
ar( why );
}
};
CEREAL_REGISTER_TYPE(DerivedTwo)
CEREAL_REGISTER_POLYMORPHIC_RELATION(EmptyBase, DerivedTwo)
int main()
{
DerivedTwo myclass;
myclass.name = "test";
myclass.why = "because";
myclass.SaveToFile("settings.json");
}
Expected output:
Child serialize
Base serialize
Actual output:
Base serialize
I found some similar problems, but their code seems to work this way. What am I missing?
Edit: I want to have a base class with Save() Load() and serialize(). Save()/Load() will de-/serialize the class into a JSON file.
I want to inherit from this class, and use it like this:
DerivedClassInstance.name = "test"; //this property is in the base class
DerivedClassInstance.y = "why"; //this is in the derived class
DerivedClassInstance.Save(); //This will save both y and name into a file in json format
DerivedClassInstance.Load(); //This will load the JSON from the file and populate name and y.
//Both Save() and Load() are in the base class
I hope that makes sense.