NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT #4395
demacia666666
started this conversation in
General
Replies: 2 comments
-
You need to enclose your code in with triple backticks to get appropriate formatting and to be able to see the |
Beta Was this translation helpful? Give feedback.
0 replies
-
The issue is the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
`#include
#include
#include
#include
#include "json.hpp"
using json = nlohmann::json;
class User {
public:
std::string name = {};
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(User, name)
};
int main() {
std::string jsonString = u8R"({"name": null})";
try {
/*
I would prefer a default value,
although it can be solved using ADL with std::optional.
Is there a more elegant way?
*/
json jsonData = json::parse(jsonString);
User user = jsonData;
}
catch (const std::exception& e) {
//[json.exception.type_error.302] type must be string, but is null
std::cerr << "" << e.what() << std::endl;
}
return 0;
}
`
Beta Was this translation helpful? Give feedback.
All reactions