Skip to content
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

Merged
merged 2 commits into from
Feb 13, 2019
Merged

Conversation

nickaein
Copy link
Contributor

This pull request is intended to tackle #1471.

This adds contains() method which similar to std::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:

if(j.contains("foo"))
{
    // "foo" key exists
}
else
{
    // "foo" key not exists	
}

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.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header file single_include/nlohmann/json.hpp. The whole process is described here.

Please don't

  • The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with #ifdefs or other means.
  • Specifically, I am aware of compilation problems with Microsoft Visual Studio (there even is an issue label for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
  • Please refrain from proposing changes that would break JSON conformance. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
  • Please do not open pull requests that address multiple issues.

{
if (is_object())
{
return (m_value.object->find(key) != m_value.object->end());
Copy link
Owner

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))?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! Done.

@nlohmann
Copy link
Owner

Very nice - I only have a small question, see above.

Copy link
Owner

@nlohmann nlohmann left a 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.

@nlohmann nlohmann added this to the Release 3.5.1 milestone Feb 13, 2019
@nlohmann nlohmann merged commit e326df2 into nlohmann:develop Feb 13, 2019
@nlohmann
Copy link
Owner

Thanks!

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling e93f305 on nickaein:develop into 46ff13d on nlohmann:develop.

@nickaein
Copy link
Contributor Author

I initially assumed this will be available for the next minor release (3.6.0) since it's an backward-compatible API change. So I put @since version 3.6.0 in the documentation. Sorry that I forgot to mention it.

Will this addition be available in 3.5.1 and should I open another PR to fix the documentation?

}
else
{
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this throw?

Copy link
Contributor Author

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.

@jaredgrubb
Copy link
Contributor

If I ask if a number contains a key, I think it should throw.
Even more so, if I ask an array if it contains a value, getting false back feels odd.

@nickaein
Copy link
Contributor Author

What would a better behavior is such cases is debatable. I tried to keep it on par with the already existing json::find(). For non-object types, it returns std::end() which is the same return value if the type is object and the key is not found.

Essentially json.contains(x) is equivalent to json.find(x) != end() and I believe this is probably expected behavior. It might be confusing that json.find(x) (and json.count(x), etc) doesn't throw for non-objects but json::contains(x) does.

@jaredgrubb
Copy link
Contributor

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.

@nickaein
Copy link
Contributor Author

@nlohmann What's your take on #1474 (comment)

@nlohmann
Copy link
Owner

I already adjusted the target version to 3.6.0 if this is what you mean.

@nickaein
Copy link
Contributor Author

Yes Thanks. The timeline still says

nlohmann added this to the Release 3.5.1 milestone 9 days ago

but I can now see the sidebar has the correct milestone.

@nlohmann
Copy link
Owner

I renamed the milestone which apparently does not change past log messages. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants