-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
questions regarding from_json #1226
Comments
There is an example with |
Hi theodelrieu, Thanks for the quick answer but the example does not work for me and Im trying to figure it out I have a class Person that contains optional members something like: class Pesron {
std::string id;
std::string name;
std::optional<std::string> eye_color;
} static void from_json(const json& j, Person& r) {
r.id = j.at("ID").get<std::string>();
r.name = j.at("Name").get<std::string>();
r.eye_color = j.at("Eye_Color").get<std::optional<std::string>>(); // gives me error compilation
} namespace nlohmann {
template <typename T>
struct adl_serializer<std::optional<T>> {
static void to_json(json& j, const std::optional<T>& opt) {
if (!opt) {
j = nullptr;
}
else {
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
}
}
static void from_json(const json& j, std::optional<T>& opt) {
if (j.is_null()) {
opt.reset();
}
else {
opt = j.get<T>(); // same as above, but with
// adl_serializer<T>::from_json
}
}
};
} what am I doing wrong here? |
What is your error message? Can you provide a minimal program that produces the error? |
My guess would be that you did not put the I would advise you to put every That way your specializations will be available in every translation unit. |
Hi Im sorry my mistake, |
If you copy-pasted the code from the doc, you will always have a field, which will be |
(Sorry for the confusion on milestones.) |
I dont really understand your answer |
Here is a full example (using #include <nlohmann/json.hpp>
#include <boost/optional.hpp>
using nlohmann::json;
namespace nlohmann {
template <typename T> struct adl_serializer<boost::optional<T>> {
static void to_json(json &j, const boost::optional<T> &opt) {
if (!opt) {
j = nullptr;
} else {
j = *opt; // this will call adl_serializer<T>::to_json which will
// find the free function to_json in T's namespace!
}
}
static void from_json(const json &j, boost::optional<T> &opt) {
if (j.is_null()) {
opt.reset();
} else {
opt = j.get<T>(); // same as above, but with
// adl_serializer<T>::from_json
}
}
};
}
struct test
{
boost::optional<int> i;
};
void to_json(json& j, test const& t)
{
j["opt"] = t.i;
}
void from_json(json const& j, test& t)
{
t.i = j.at("opt").get<boost::optional<int>>();
}
int main(int argc, char const *argv[])
{
nlohmann::json j(test{});
auto t = j.get<test>();
}
|
Thanks for showing a detail solution, when I tried to use std::optional instead of boost::optional, it did throw an exception, does json provide solution only to boost:optional and not std::optional? |
It should work the same, I just could not test with Could you paste a complete reproducible example? |
This code is giving me exception: using nlohmann::json; namespace nlohmann {
} struct test void to_json(json& j, test const& t) void from_json(json const& j, test& t) int main(int argc, char const *argv[]) so is that only works when you initialize json with Thanks. |
Could you edit your answer to put the code between triple backticks? |
is that only works when you initialize json with |
Thanks, indeed it will throw because you did not put I personally find it much easier to always have the field present in the json, and to have a |
I tried to add
I think is because as you said you need to have it within json. |
Oh, I finally get it. I did not see the end of your snippet... Yes, the field must be present if you implement from json that way (i.e. with But it's up to you to use If you do so, the I strongly suggest the first approach though. If your JSON format cannot be changed, then you must use I hope I was clear enough :) |
@tamarlev Do you need further assistance? |
Thanks for the quick replies and no further assistance is required 👍 |
I have a problem where if I try to convert json to a class, and the class have std::optional members I need to always check if the keys exists in the json and only if they exists (in json) then I can capture their data, something like:
m_id = (j.find("ID") != j.end() && j.at("ID") != nullptr)? j.at("ID").getstd::string() : nullopt;
how can I avoid it?
is there a way to change 'get' j.at("ID").getstd::string() and return either nullopt and avoid asking if 'ID' exists and not nullptr?
The text was updated successfully, but these errors were encountered: