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

Decoding 'Maybe Whatever' with .: doesn't throw an error at compile time #614

Closed
kvanbere opened this issue Dec 24, 2017 · 3 comments
Closed

Comments

@kvanbere
Copy link

kvanbere commented Dec 24, 2017

Decoding 'Maybe Whatever' with (.:) doesn't throw an error at compile time. It just lets you know that it fails at run time, throwing an error instead of returning Nothing when the key doesn't exist.

This really isn't ideal. Could the API be 'safe-by-default' and only allow you to decode Maybe Whatever with (.:?) and not (.:) ? In my use cases I've never wanted to fail at runtime if the key doesn't exist and I intended to decode 'Maybe Whatever' but forgot to update the FromJSON instance.

@phadej
Copy link
Collaborator

phadej commented Dec 25, 2017

there are good semantics for FromJSON (Maybe a), null decodes to Nothing, and something else decodes to Just x. This means that key&value should exist in the JSON object. .:? would also succeed with Nothing when key doesn't exist.

In fact, we have .:! to return Nothing if key&value doesn't exist, but otherwise parse as intended.
Therefore, one can parse to Maybe (Maybe Whaverer) with .:!`, distinguishing between successful, non-existing and null cases.

I'm afraid there is no simple way to change aeson so it would be "safe-by-default", somebody will be unhappy with a change! I recommend you to use generic/TH deriving, it's very capable. Maybe instance is useful, yet generics or TH can omitNothingFields and rename fields with fieldLabelModifier.

@phadej
Copy link
Collaborator

phadej commented Dec 25, 2017

EDIT: we could make the default better by reworking whole aeson API: we could have FromJSONField type class, then FromJSONField (Maybe a) could "succeed" with Nothing if key doesn't exist. That's however very major change. Something like that is in #564 e.g.

@bergmark
Copy link
Collaborator

I think we can close this.

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

No branches or pull requests

3 participants