-
-
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
[Question] How do I parse JSON into custom types? #1669
Comments
I don't have time to compile the code in the moment. But one note: The signature of the void from_json(const json& j, boost::gregorian::date& d) instead of void from_json(json& j, boost::gregorian::date& d) (see https://github.com/nlohmann/json#basic-usage). |
Thanks for the quick response. To help you out, I put this code on Wandbox along with your single header file and the above JSON. You can easily compile it that way: https://wandbox.org/permlink/rgECm599lctiFXkA Note with your fix it still has problems. The Again I appreciate your time. Let me know if you get a spare moment to code review this. Thanks! |
Your constructor should accept a const reference: WalletCampaign(const json& config) That makes the code compile at Wandbox. You may want to replace the constructor taking a What makes you feel you write boilerplate code? |
Thanks, your fix was the missing piece. The compiler diagnostic did not make the const issue obvious. Very cryptic output. Maybe you can add a In terms of boilerplate, I'm still learning things, but so far making JSON elements "optional" is pretty tedious. There's no 1 line solution to this that I've seen so far. It would be nice to see built-in support for Right now I do this: boost::gregorian::date startDate;
auto element = config.find("start_date");
if (element != config.end())
{
element->get_to(startDate);
} Would be nicer instead to have something like this: std::optional<boost::gregorian::date> startDate;
config.try_get_to("start_date", startDate);
EDIT: Actually my example of EDIT2: Looks like it doesn't compile because I was wrapping the |
Any objection to making these part of your library? namespace nlohmann
{
template<typename T>
struct adl_serializer<std::optional<T>>
{
static void to_json(json& j, const std::optional<T>& value)
{
if (!value)
{
j = nullptr;
}
else
{
// this will call adl_serializer::to_json which will
// find the free function to_json in T's namespace!
j = *value;
}
}
static void from_json(json const& j, std::optional<T>& value)
{
if (j.is_null())
{
value.reset();
}
else
{
// same as above, but with adl_serializer<T>::from_json
value = j.get<T>();
}
}
};
template<typename T>
struct adl_serializer<std::unique_ptr<T>>
{
static void to_json(json& j, const std::unique_ptr<T>& value)
{
if (!value)
{
j = nullptr;
}
else
{
j = *value;
}
}
static void from_json(json const& j, std::unique_ptr<T>& value)
{
if (j.is_null())
{
value.reset();
}
else
{
value = std::make_unique<T>();
*value = j.get<T>();
}
}
};
} Probably need to add one for |
(There is a function I'm hesitating to add the conversions for optionals and smart pointers right now. I need to understand what this may break first. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Reviewing the documentation, I don't see a lot of helpful examples on more complex parsing scenarios. Really it seems like most of the documentation describes going from types to JSON, but I need to completely deserialize JSON data into C++ objects.
Here is the JSON data I'm working with:
For the
campaigns
array, I want to parse each element into an array of custom objects:I'm compiling this using Visual Studio 2019 with C++17 enabled. The code above doesn't compile, with these errors:
I'm not sure why it doesn't compile. But I'm sure it has to do with something about how I'm trying to use the library. Can you give me an example of how to parse this JSON document in an object oriented way as I'm trying to do?
The text was updated successfully, but these errors were encountered: