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

Feature request :: Better testing for key existance without try/catch #1074

Closed
mlfarrell opened this issue May 2, 2018 · 4 comments
Closed
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@mlfarrell
Copy link

Correct me if I'm wrong, but using a const json obj + .at() + try/catch is the only way safe to test for object key exists. This seems REALLY brutal compared to the ease of use using the rest of the library

Perhaps there'd be some way to return a bool operator comparable for object gets along the lines of

if(auto possibleVal = json["maybeExists"])
{
  //possibleVal exists
}

or.. at the very least, a .exists(const std::string &) method would be helpful..

@pboettch
Copy link
Contributor

pboettch commented May 3, 2018

You can use find():

nlohmann::json instance; 
// [..] fill instance
auto field = instance.find("id");
if (field == instance.end()) {
    std::cerr << "id not found\n";
} else {
    std::cerr << "value of id " << field.value() << "\n";
}

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 3, 2018
@nlohmann
Copy link
Owner

nlohmann commented May 3, 2018

@nlohmann
Copy link
Owner

nlohmann commented May 4, 2018

@mlfarrell Do you need further assistance?

@mlfarrell
Copy link
Author

No this will help. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants