Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cereal serialization error

So i'm legit confused. It won't compile for an external serialization function. It gives the error

cereal could not find any output serialization functions for the provided type and archive combination.

So the code below doesn't compile

#include <fstream>
#include <glm/glm.hpp>
#include "SceneObject.h"
#include <cereal/cereal.hpp>
#include <cereal/archives/json.hpp>

template<typename Archive> void serialize(Archive& archive, glm::vec3& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

struct something
{
public:
    float x, y, z;
};
template<typename Archive> void serialize(Archive& archive, something& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

int main(int argc, char** argv)
{
    SceneObject test;
    test.transform().setPosition(1.0f,2.0f,3.0f);

    {
        std::ofstream file("TestPath.json");
        cereal::JSONOutputArchive output(file);
        glm::vec3 p = test.transform().getPosition();
        output(p);
    }

    return 0;
}

but this DOES compile

#include <fstream>
#include <glm/glm.hpp>
#include "SceneObject.h"
#include <cereal/cereal.hpp>
#include <cereal/archives/json.hpp>

template<typename Archive> void serialize(Archive& archive, glm::vec3& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

struct something
{
public:
    float x, y, z;
};
template<typename Archive> void serialize(Archive& archive, something& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y), cereal::make_nvp("z", v3.z));
}

int main(int argc, char** argv)
{
    SceneObject test;
    test.transform().setPosition(1.0f,2.0f,3.0f);

    {
        std::ofstream file("TestPath.json");
        cereal::JSONOutputArchive output(file);
        glm::vec3 p = test.transform().getPosition();
        something s;
        s.x = p.x;
        s.y = p.y;
        s.z = p.z;
        output(s);
    }

    return 0;
}

I literally copy and pasted the save code from glm::vec3 to something and just changed glm::vec3 to 'something'. It makes NO sense to me why it would work for one and not the other. I think it might be a namespace thing, but I have no clue how to fix that.

like image 648
Chase R Lewis Avatar asked Apr 27 '16 12:04

Chase R Lewis


1 Answers

Well apparently posting made me find the solution.

You need to make sure the serialize functions share the same namespace so if I wrap them like

namespace glm
{
template<typename Archive> void serialize(Archive& archive, glm::vec3& v3)
{
    archive(cereal::make_nvp("x", v3.x), cereal::make_nvp("y", v3.y),    cereal::make_nvp("z", v3.z));
}
}

It works. Kinda odd, but it is what it is.

like image 160
Chase R Lewis Avatar answered Sep 19 '22 21:09

Chase R Lewis