try_get_to #1563
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
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.
The text was updated successfully, but these errors were encountered: