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

json.at_or_default(key, defaultval) #1852

Closed
adreed-msft opened this issue Nov 23, 2019 · 2 comments
Closed

json.at_or_default(key, defaultval) #1852

adreed-msft opened this issue Nov 23, 2019 · 2 comments
Labels
kind: enhancement/improvement solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@adreed-msft
Copy link

  • Describe the feature in as much detail as possible.
if (j.contains("access_token"))
{
    j.at("access_token").get_to(t.access_token);
} 
else 
{
    t.access_token = "";
}

This tends to be a lot of boilerplate to ensure something's there. I just want to supply a default within the single line if the key does not exist in the json struct.

  • Include sample usage where appropriate.

json.at_or_default("nonexistantkey", "default") should return "default", as the key does not exist.

However,

json.at_or_default("existing", "default") // "existing" exists, and contains "nondefault" should return "nondefault" as the key does exist.

@ghost
Copy link

ghost commented Nov 23, 2019

Have a a look at value.

https://nlohmann.github.io/json/classnlohmann_1_1basic__json_adcf8ca5079f5db993820bf50036bf45d.html#adcf8ca5079f5db993820bf50036bf45d

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Nov 23, 2019
@adreed-msft
Copy link
Author

Works for me!

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

No branches or pull requests

2 participants