Skip to content

detail namespace collision with nlohmann::json? #499

Description

@RPGillespie6

I'm seeing a very strange bug when using your library in conjunction with the newest version of https://github.com/nlohmann/json.

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, it only happens when the name of the function in the Bug class is load, which is why I'm opening an issue with your library (it sseems to have special significance with your library).

Again, curiously, compiling against a 2.X version of nlohmann::json does not product this exception.

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 nlohmann::json.

Still trying to figure out root cause

nlohmann::json issue: nlohmann/json#1082

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions