I'm seeing a very strange bug when using your library in conjunction with C++ Cereal: https://github.com/USCiLab/cereal
Reproduce with the following:
#include <json.hpp>
#include <cereal/types/polymorphic.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/access.hpp> //So we can make serialize private so developers aren't tempted to call it.
#include <cereal/types/string.hpp> //This is needed to serialize std::string. There are similar ones for the other std containers
#include <cereal/types/vector.hpp> //This is needed to serialize std::vector. There are similar ones for the other std containers
using namespace std;
//Pure virtual base class
class Serializable
{
public:
virtual int getType() = 0;
protected:
template<class Archive> void serialize(Archive & ar);
};
class Bug: public Serializable
{
public:
std::string text;
int getType() {return 1;};
void load(const nlohmann::json & s) {};
private:
friend class cereal::access;
template <class Archive> void serialize(Archive &ar) {ar(text);};
};
// Register Bug
CEREAL_REGISTER_TYPE(Bug);
CEREAL_REGISTER_POLYMORPHIC_RELATION(Serializable, Bug);
int main()
{
auto obj = make_shared<Bug>();
std::ostringstream os;
{
cereal::BinaryOutputArchive oarchive(os);
oarchive(dynamic_pointer_cast<Serializable>(obj));
}
shared_ptr<Serializable> obj2;
std::istringstream is(os.str());
{
cereal::BinaryInputArchive iarchive(is);
iarchive(obj2);
}
auto m = dynamic_pointer_cast<Bug>(obj2);
return 0;
}
This code, when compiled with the latest nlohmann::json throws the following:
Trying to load an unregistered polymorphic type (Bug).
Make sure your type is registered with CEREAL_REGISTER_TYPE and that the archive you are using was included (and registered with CEREAL_REGISTER_ARCHIVE) prior to calling CEREAL_REGISTER_TYPE.
If your type is already registered and you still see this error, you may need to use CEREAL_REGISTER_DYNAMIC_INIT.
Curiously though, compiling against a 2.X version of nlohmann::json does not produce this exception.
I noticed this only happens when the method in the Bug class is load, which is why I'm opening an issue with cereal as well.
I noticed one change from nlohmann::json 2.X->3.X is the inclusion of the nlohmann::detail namespace. Could this be somehow conflicting with cereal::detail? I will also open an issue against cereal as well.
Still trying to figure out root cause of this. It may not be your library, just trying to figure out why I can no longer use nlohmann::json with Cereal when I upgrade from 2.X to 3.X
Cereal issue: USCiLab/cereal#499
I'm seeing a very strange bug when using your library in conjunction with C++ Cereal: https://github.com/USCiLab/cereal
Reproduce with the following:
This code, when compiled with the latest
nlohmann::jsonthrows the following:Curiously though, compiling against a 2.X version of
nlohmann::jsondoes not produce this exception.I noticed this only happens when the method in the
Bugclass isload, which is why I'm opening an issue with cereal as well.I noticed one change from
nlohmann::json2.X->3.X is the inclusion of thenlohmann::detailnamespace. Could this be somehow conflicting withcereal::detail? I will also open an issue againstcerealas well.Still trying to figure out root cause of this. It may not be your library, just trying to figure out why I can no longer use
nlohmann::jsonwith Cereal when I upgrade from 2.X to 3.XCereal issue: USCiLab/cereal#499