-
-
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
Can we have an exists()
method?
#1471
Comments
You could implement this yourself with bool exists(const json& j, const std::string& key)
{
return j.find(key) != j.end();
} |
No it does not. There is no key involved in an If you are referring to your You will get the same result on the last line if you leave out the
|
The answer to your question is the same as the answer to this question: https://stackoverflow.com/questions/1939953/how-to-find-if-a-given-key-exists-in-a-c-stdmap |
@nlohmann I think it's better to be available too. |
Make a PR. |
I'm still torn to extend the API, because the same can be achieved with either find (see above) or count (as mentioned in the original post). I'm afraid we blow the API, and since "exists" is also nothing someone would "know" from an STL container, it's not necessarily making the API more intuitive. So let's discuss! |
I guess that Interestingly I see cppreference describes |
I want people who read my code not to feel like they are reading an STD c++ code but an Object-Oriented code However, |
@hydratim |
I strongly agree. If we start to diverge from STL containers API, there are many possibilities and choices to the API design and a seasoned C++ developer have to put effort to learn this custom API. Even a little divergence can be confusing and irritating since the user cannot trust any longer that our API has the same behavior and semantics of the STL. Having same API as the STL has this benefit that a user familiar with STL can intuitively work with this library too. It is reasonable to expected the user of this library is familiar with STL containers and their API.
Thanks @garethsb-sony! This looks promising. The original paper makes some good arguments too, specifically on the member-vs-free function. This proposal has been merged into the latest C++2a draft (N4800) (Table 69 which specifies Associative container requirements). Also, Boost has already adopted it. So even though C++20 is not finalized yet, this addition seems to be a done deal. Based on these, I believe adding |
Agreed (and as a little aside, is the reason that while preparing #1469, I was a little surprised to find that |
I've implemented in PR #1474. Feedback/more discussions are welcomed! |
Closed by merging #1474. |
I wish to determine whether a particular key exists.
This is one of those things it's reasonable to expect, and surprising that there is no easy / obvious / direct solution.
is_null()
(rather dangerously IMHO) silently creates the key.https://stackoverflow.com/questions/49850890/c-nlohmann-json-how-to-test-nested-object-is-existent-or-empty suggests
.count()
but again is not obvious how to apply this.I've seen other solutions suggested involving iterators.
Pretty sure I saw a nice solution the other day in an issue, but now I can't find it.
Why not simply have an
exists()
method?The documentation is super helpful for ENcoding JSON, but lacking in code examples for extracting from JSON.
The text was updated successfully, but these errors were encountered: