-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Check value for existence by json_pointer #1194
Comments
Internally, the JSON Pointer implementation also relies on exceptions. That is, there would be no real difference between implementing a |
yep, now I have looked in code. So, looks like I have to avoid such logics... |
Related: #1017 |
@nlohmann I need this |
Just one question, what value should be for end() in this case? |
@nlohmann Hmm good question, in mind answer is Problem is what I was forced to write this on my side: std::istringstream s(key);
std::string k;
const Data* ptr = nullptr;
while (std::getline(s, k, '/'))
{
if (k.empty())
{
ptr = &m_cData;
}
else if (ptr == nullptr)
{
return false;
}
else
{
auto it = ptr->find(k);
if (it == ptr->end())
{
return false;
}
else
{
ptr = &it.value();
}
}
} |
The issue is that iterators of different containers cannot be compared as this would be undefined behavior. Therefore, writing code like if (j.find("/foo/bar"_json_pointer) != j.end())
... is not possible. |
@nlohmann Ok, maybe this: |
Passing a lambda feels strange. I'd rather pass a |
@nlohmann You said that with iterator no possible. Maybe this: std::string value;
if (json.try_get(/foo/bar"_json_pointer, value))
{
//do something when found
}
|
What would |
You may try to get value() work without exceptions. It will help in most cases. Additional, you can add some value, that can't be found in json, and use it as default. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
will it resolved or just closed? ;( |
There is no solution for the issue yet. That's why it was closed eventually. |
|
|
|
Why you do not want to do your own std::optional? It would be modern solution (but should be not called find I think) :) |
Maybe this? |
I don't like the idea that it would return a If you really want iterators, then I think you need to have a different kind of iterator, because you're modeling the structure very different. I think this is a bit like directory_iterator versus recursive_directory_iterator, where the first can only iterate across one level (like json::iterator), and the second can iterate across a full tree. I think drawing inspiration from might be helpful. On the other hand, doing the Does anyone have any prior art on other ways that boost or STL or some other solid library has offered a "reach inside some arbitrary heterogenous tree structure" has approached this? I can't imagine we're the first ones :) |
I would not like to add our own implementation of |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have found no proper way to check element existence.
I can use
json.at("..."_json_pointer)
but it will throw an exception if it is missed.I can use
json.value()
method. But I have to do it twice to check all cases (or use default that will not used in my json).The text was updated successfully, but these errors were encountered: