-
I'm coming from primarily a Unity/C# background, trying to learn C++. I read from the docs that I needa define my own adl_serializer to handle to/from JSON from a given type, using templates. #include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"
#include "glm/glm.hpp"
using std::cout;
using std::endl;
using std::getline;
using std::ifstream;
using json = nlohmann::json;
using glm::vec3;
NLOHMANN_JSON_NAMESPACE_BEGIN
template <typename T>
struct adl_serializer {
static void to_json(json& j, const vec& value) {
j = json({
{"x", value.x},
{"y", value.y},
{"z", value.z}
});
}
static void from_json(const json& j, vec3& value) {
j.at("x").get_to(value.x);
j.at("y").get_to(value.y);
j.at("z").get_to(value.z);
}
};
NLOHMANN_JSON_NAMESPACE_END
void testJSON() {
cout << "JSON testing..." << endl;
vec3 position = vec3(2, -9, 6);
json jPosition = position; //ERROR: no suitable user-defined conversion from "glm::vec3" to "json" existsC/C++(312)
cout << jPosition.dump(4) << endl;
cout << endl;
} I just get an error that I can't convert from vec3 to json, in this example. |
Beta Was this translation helpful? Give feedback.
Answered by
nlohmann
Sep 24, 2023
Replies: 1 comment 1 reply
-
You need no define the serializer inside the glm namespace. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Lunar2kPS
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need no define the serializer inside the glm namespace.