-
-
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
Add a get overload taking a parameter. #1231
Add a get overload taking a parameter. #1231
Conversation
25a8e3e
to
a45f67d
Compare
This overloads is chosen if: | ||
- @a ValueType is not @ref basic_json, | ||
- @ref json_serializer<ValueType> has a `from_json()` method of the form | ||
`void from_json(const basic_json&, ValueType&)`, and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment seems to end in the middle of the sentence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, will fix
Should I add support for |
What do you mean? |
// one can do the following
nlohmann::json j;
auto v = j.get<my_type>();
auto j2 = j.get<nlohmann::json>();
auto j3 = j.get<my_custom_json>(); Should we support the following: nlohmann::json j;
nlohmann::json j2;
my_custom_json j3;
j.get(j2);
j.get(j3); It would be consistent with the non-parameter overload. |
a45f67d
to
699886d
Compare
I've added support for I also added tests in However, it will throw when using But this is a bit tricky, it requires retrieving the current string, and throw(?) if the string length does not match the C-array/std::array size... |
7384a9a
to
db5e00a
Compare
@theodelrieu Why not both? |
I don't really mind, but since it's a new feature it would be more fit in a minor release. We can make 3.3 directly though |
Oh, I see. Maybe I need to better understand the feature. Could you add a snippet for the README? |
I knew I forgot something... |
README.md
Outdated
@@ -226,6 +226,9 @@ json j_string = "this is a string"; | |||
std::string cpp_string = j_string; | |||
// retrieve the string value (explicit JSON to std::string conversion) | |||
auto cpp_string2 = j_string.get<std::string>(); | |||
// retrieve the string value (alternative explicit JSON to std::string conversion) | |||
std::string cpp_string3; | |||
j_string.get(cpp_string3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be so late to the party, but now when I see this in the documentation: isn't the name get
a bit misleading? It's rather a setter, but to the passed reference. But in particular the example below
j.at("name").get(p.name);
seems counter-intuitive to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe get_to
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_to
sounds good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, could you please change the name to get_to
and also add an example to doc/examples
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Takes an lvalue reference, and returns the same reference. This allows non-default constructible types to be converted without specializing adl_serializer. This overload does not require CopyConstructible either. Implements nlohmann#1227
dd9299f
to
521fe49
Compare
Let me know if the example looks fine, I could not upload them to wandbox by running |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Thanks a lot! Great work as always. |
Closes #1227.
Takes an lvalue reference, and returns the same reference.
This allows non-default constructible types to be used with get (i.e. without specializing
adl_serializer
).This overload does not require
CopyConstructible
either.Implements #1227
This can be a step forward to resolve #958. Indeed, we can recommend users to replace implicit conversions from json values by using this new overload. I believe we could start deprecating
operator T
if we decide to merge this PR.Note that this new overload returns its parameter, which allows chaining (i.e. a bit like C++17's
vector::emplace_back
).Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.