-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Implement contains() member function #1474
Conversation
include/nlohmann/json.hpp
Outdated
{ | ||
if (is_object()) | ||
{ | ||
return (m_value.object->find(key) != m_value.object->end()); |
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.
I'm not sure, but shouldn't this be find(std::forward(key))
?
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.
Good point! Done.
Very nice - I only have a small question, see above. |
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! |
I initially assumed this will be available for the next minor release ( Will this addition be available in |
} | ||
else | ||
{ | ||
return false; |
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.
Should this throw?
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.
It seems std::map::find cannot be marked as noexcept. I am not sure if we can guarantee that conditions not happening in our case.
If I ask if a number contains a key, I think it should throw. |
What would a better behavior is such cases is debatable. I tried to keep it on par with the already existing Essentially |
Those are good arguments and, with that justification, I have to reluctantly agree. :) In another world I would try to argue that they all should throw, but the cure is probably worse than the cause at this point. |
@nlohmann What's your take on #1474 (comment) |
I already adjusted the target version to 3.6.0 if this is what you mean. |
Yes Thanks. The timeline still says
but I can now see the sidebar has the correct milestone. |
I renamed the milestone which apparently does not change past log messages. :) |
This pull request is intended to tackle #1471.
This adds
contains()
method which similar tostd::map::contains()
available in C++20. While there could be various APIs and choices for such function, this PR tries to be aligned with STL counterpart.Having such method, the user can check the existence of a key in a JSON object more concisely:
std::map::contains()
has been proposed in P0458R2 and is merged to the latest C++20 draft N4800 (as now). Boost library has already adopted it since then.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.Please don't
#ifdef
s or other means.