I'm using nlohmann::json to (de)serialize some C++ objects in JSON. I haven't yet understood how to set up the code to work with a third-party library (SoapySDR). My code is in the global namespace in this example, while the SoapySDR code is in its own namespace. I'm getting a ton of compile errors with this simplified example:
#include "json.hpp"
using nlohmann::json;
namespace SoapySDR
{
class Range
{
public:
double minimum(void) const;
double maximum(void) const;
double step(void) const;
private:
double _min, _max, _step;
};
class ArgInfo
{
public:
Range range;
};
}; // namespace SoapySDR
void to_json(json &j, const SoapySDR::Range &r)
{
j += {"min",r.minimum()};
j += {"max",r.maximum()};
j += {"step",r.step()};
}
void from_json(const json &j, SoapySDR::Range &r)
{
// r = SoapySDR::Range(j.at("min"), j.at("max"), j.at("step"));
}
void to_json(json &j, const SoapySDR::ArgInfo &ai)
{
j = json{{"range", ai.range}};
}
void from_json(const json &j, SoapySDR::ArgInfo &ai)
{
j.at("range").get_to(ai.range);
}
These are the error messages, excluding all the extra information about attempted deductions:
bob.cpp:41:31: error: no matching function for call to ‘nlohmann::basic_json<>::basic_json(<brace-enclosed initializer list>)’
bob.cpp:41:31: note: couldn't deduce template parameter ‘JsonRef’
bob.cpp:41:31: note: couldn't deduce template parameter ‘BasicJsonType’
bob.cpp:41:31: note: couldn't deduce template parameter ‘CompatibleType’
j = json{{"range", ai.range}};
bob.cpp:46:32: error: no matching function for call to ‘nlohmann::basic_json<>::get_to(SoapySDR::Range&) const’
j.at("range").get_to(ai.range);
json.hpp:19588:28: error: no type named ‘type’ in ‘struct std::enable_if<false, int>’
int > = 0 >
json.hpp:19613:11: note: template argument deduction/substitution failed:
bob.cpp:46:32: note: mismatched types ‘T [N]’ and ‘SoapySDR::Range’
j.at("range").get_to(ai.range);
The
to_json
andfrom_json
overloads for your data types need to be in the type's namespace for the library to find them:After that, your code compiles: https://godbolt.org/z/5cafYx