-
-
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
json::contains usage to find a path #1727
Comments
There is maybe one Isn't it either
or
What do you see when printing your jPtr? |
Thanks for replying but I've tested your suggestions and neither of them work. |
Also, is there any way to enumerate all key value pairs in a json file? The |
Indeed there is a double invocation of the JSON Pointer constructor if you call json::json_pointer("/root/settings/logging"_json_pointer) For your example, this code works: #include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main()
{
json j;
j["root"]["settings"]["logging"] = true;
std::cout << std::boolalpha
<< j.contains("/root/settings/logging"_json_pointer) << '\n'
<< j.contains("/no/value/here"_json_pointer)
<< std::endl;
} Output:
|
Thanks but my question was about passing an instance of It is related to the
....both return
Is this a bug because this really caught me out. |
I see. Calling template<typename KeyT, typename std::enable_if<
not std::is_same<KeyT, json_pointer>::value, int>::type = 0>
bool contains(KeyT && key) const
{
return is_object() and m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
} which is wrong, whereas calling it with a bool contains(const json_pointer& ptr) const
{
return ptr.contains(this);
} which is correct. I am puzzled. Any ideas? |
I'm not familiar enough with the codebase to suggest a proper fix but I've added a bool contains(json_pointer& ptr) const
{
return ptr.contains(this);
} Could you explain to me what the template<typename KeyT, typename std::enable_if<
not std::is_same<KeyT, json_pointer>::value, int>::type = 0>
bool contains(KeyT && key) const
{
return is_object() and m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
} |
Hey @devhandle I have explained why this wasn't working in the pull requests but if you need more explanation from my side, don't doubt into dropping me a message :) |
Thanks @tete17! I've tested your change and it fixes my issue. Also, thanks for the explanation - much appreciated. |
I want to test whether a path exists in a json file. For example, "/root/settings/logging".
exists1
is false andexists2
is true.I expected both of these to be true. Is this a bug or is this expected behaviour?
UPDATE: There are two
contains
functions. One which takes an rvalue reference to aKeyT
and one which takes a reference to a json_pointer. Both do different things. This seems inconsistent to me. If Istd::move
jPtr it works but I don't want to move the pointer.g++ (Ubuntu 8.3.0-6ubuntu1~18.10.1) 8.3.0
v3.7.0
The text was updated successfully, but these errors were encountered: