Hello, I am trying to use the nlohmann json library and I am getting an issue I really can't understand.
Basically, I have multiple custom types (struct) defined in different files but under the same namespace, they are basically defined like this:
// type1.hpp
#include "nlohmann/json.hpp"
namespace types
{
struct type1 { /* bunch of fields */ };
void from_json(const nlohmann::json& j, type1& t);
}
// type1.cpp
#include "type1.hpp"
void types::from_json(const nlohmann::json& j, types::type1& t) { /* implementation */ }
// type2.hpp
#include "nlohmann/json.hpp"
#include "type1.hpp" // because type2 has type1 as a field
namespace types
{
struct type2 { /* fields */ };
void from_json(const nlohmann::json& j, type2& t);
}
// type2.cpp
#include "type2.hpp"
void types::from_json(const nlohmann::json& j, types::type2& t) { /* implementation */ }
// some other .cpp file
#include <vector>
#include "nlohmann/json.hpp"
#include "type1.hpp"
#include "type2.hpp"
const std::vector<types::type1> getX()
{
// assuming I retrieve the data from an API
nlohmann::json json = nlohmann::json::parse(data);
return json["x"].get<std::vector<types::type1>>(); // no errors
}
const std::vector<types::type2> getY()
{
// retrieving data from API
nlohmann::json json = nlohmann::json::parse(data);
return json["y"].get<std::vector<types::type2>>(); // <- issue here returning error: no matching member function for call to 'get'
// note: candidate template ignored: substitution failure [...] no matching member function for call to 'get_impl'
}
Did anyone encounter such an issue before? Or what am I doing wrong here?