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

try_get_to #1563

Closed
TomMettam opened this issue Apr 10, 2019 · 2 comments
Closed

try_get_to #1563

TomMettam opened this issue Apr 10, 2019 · 2 comments
Labels
kind: enhancement/improvement state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@TomMettam
Copy link

TomMettam commented Apr 10, 2019

Please consider adding a try_get_to convenience function, which would assign a provided default value in case of a type mismatch (i.e, null)

json.at("someKey").try_get_to(object.someKey, "DefaultValue");

Ideally, this would work in conjunction with a similar convenience method like try_at which would return a default value rather than throwing an exception if a value is not found (and perhaps null should be the default if not specified)

json.try_at("someKey", json()).try_get_to(object.someKey, "DefaultValue");

Why? Well, the most common usage for JSON is for external interchange. In many cases, the sender is not under the control of the developer, and parameters may be added or removed at any time.

These convenience functions would avoid the need to wrap every single parameter in from_json with a try catch, or a null/exists guard.

I have achieved the same result by wrapping in a template function, but I think this functionality is important enough to warrant being included in the library itself.

@thekie
Copy link

thekie commented May 8, 2019

+1
I just updated the library and found a breaking behavior. Before the update, I was able to use json.value("key", "default") to get a default value when "key" was missing in the json or was set to "null". Now I get a type mismatch.

@stale
Copy link

stale bot commented Jun 7, 2019

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.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 7, 2019
@stale stale bot closed this as completed Jun 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants