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

value() does not work with _json_pointer types #283

Closed
clwill opened this issue Jul 21, 2016 · 4 comments
Closed

value() does not work with _json_pointer types #283

clwill opened this issue Jul 21, 2016 · 4 comments

Comments

@clwill
Copy link

clwill commented Jul 21, 2016

It would be great if value(), which supports default values, worked like at() which can handle _json_pointer types.

This would allow the handling of nested objects, and still be robust to missing elements.

To wit:

#include <json.hpp>

using json = nlohmann::json;

int main()
{
    json j =
    {
        {"object", {{"key1", 1}, {"key2", 2}}},
    };

    int at_integer = j.at("/object/key2"_json_pointer);
    int val_integer = j.value("/object/key2"_json_pointer, 0);

    std::cout << std::boolalpha << at_integer << " " << val_integer
              << " " << (at_integer == val_integer) << "\n";
}

This code produces the output:

2 0 false

Would be nice if it was 2 2 true.

@nlohmann
Copy link
Owner

Hi @clwill, thanks for reporting. It definitely makes sense to have the symmetry between value and at. I shall extend the function once #278 is solved.

@clwill
Copy link
Author

clwill commented Jul 21, 2016

Sounds great, thanks.

Chris

nlohmann added a commit that referenced this issue Jul 23, 2016
@nlohmann nlohmann self-assigned this Jul 24, 2016
nlohmann added a commit that referenced this issue Jul 24, 2016
@nlohmann
Copy link
Owner

As the discussion in #278 got quieter, I extended the value function accordingly.

@clwill
Copy link
Author

clwill commented Jul 24, 2016

Thanks so much!

Unfortunately, the project I'm currently embroiled in requires CUDA, and their tools only work on VS 2013 (until they release 8.0). And your great code doesn't compile over there...

I'll be back to your library when CUDA releases 8.0 in August.

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

No branches or pull requests

2 participants