Incorrect namespace in C++ to/from json (nlohmann)
See original GitHub issueThe to_json/from_json functions should be in the same namespace as the structure. I’m using Visual Studio 2017 (VC++)
Additional information: https://github.com/nlohmann/json/issues/780#issuecomment-376696058
QuickType Code Gen
#pragma once
#include "json.hpp"
namespace quicktype {
using nlohmann::json;
inline json get_untyped(const json & j, const char * property) {
if (j.find(property) != j.end()) {
return j.at(property).get<json>();
}
return json();
}
inline json get_untyped(const json & j, std::string property) {
return get_untyped(j, property.data());
}
struct Welcome {
int64_t response_code;
std::string response_message;
};
}
namespace nlohmann {
void from_json(const json & j, quicktype::Welcome & x);
void to_json(json & j, const quicktype::Welcome & x);
inline void from_json(const json & j, quicktype::Welcome& x) {
x.response_code = j.at("response_code").get<int64_t>();
x.response_message = j.at("response_message").get<std::string>();
}
inline void to_json(json & j, const quicktype::Welcome & x) {
j = json::object();
j["response_code"] = x.response_code;
j["response_message"] = x.response_message;
}
}
Working Code
#pragma once
namespace quicktype {
using nlohmann::json;
inline json get_untyped(const json & j, const char * property) {
if (j.find(property) != j.end()) {
return j.at(property).get<json>();
}
return json();
}
inline json get_untyped(const json & j, std::string property) {
return get_untyped(j, property.data());
}
struct Welcome {
int64_t response_code;
std::string response_message;
};
void from_json(const json & j, quicktype::Welcome & x);
void to_json(json & j, const quicktype::Welcome & x);
inline void from_json(const json & j, quicktype::Welcome& x) {
x.response_code = j.at("response_code").get<int64_t>();
x.response_message = j.at("response_message").get<std::string>();
}
inline void to_json(json & j, const quicktype::Welcome & x) {
j = json::object();
j["response_code"] = x.response_code;
j["response_message"] = x.response_message;
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (1 by maintainers)
Top Results From Across the Web
nlohmann Namespace - JSON for Modern C++
Disabling the namespace version component and mixing ABI-incompatible versions will result in crashes or incorrect behavior. You have been ...
Read more >When deserializing a struct with from_json: error: no matching ...
I'm using the nlohmann/json library and trying to implement serialization ... and from_json obviously reside in the same namespace (global), ...
Read more >JSON for Modern C++_wjjontheway的博客
Intuitive syntax. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of modern...
Read more >json/CHANGELOG and json Releases | LibHunt - Awesome C++
#include <filesystem> doesn't work with gcc-7 when -std=c++17 is specified. #3203; Not able to use nlohmann json with c++ code built using emscripten...
Read more >thirdparty/json · master · SAUGER Gabriel / TANGUE · GitLab
json.hpp is the single required file in single_include/nlohmann or ... process JSON and set the necessary switches to enable C++11 (e.g., -std=c++11 for...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am facing a similar issue with Visual Studio 2017. What I have done is to add an additional
useTopLevelNamespace
option for C++. This basically reuses the top level namespace provided for wrappingto_json
andfrom_json
functions.I can create a PR for this, but haven’t had time to run tests.
Here’s the diff:
While the solution is very simple, the issue cause recurring support in the popular Nlohmann repo https://github.com/nlohmann/json/discussions/3280 Indeed the generated code need to be corrected so that the to and from are not in the lib namespace, or at least in the generated header comments, please mention the possibility that the compilation error “no matching overloaded function found when implementing from_json function” relate to the choice of namespace where “to” and “from” are implemented in the autogenerated code. The combination of the quicktype tool and the Nlohmann is a very powerfull tool that should gain in popularity but we can’t evaluate how many users could be discouraged at the first occurence of the issue !