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

[feature] an option for ObjectWithSingleField sum type encoding to allow decoding in Swift #925

Open
epoberezkin opened this issue Jan 29, 2022 · 2 comments

Comments

@epoberezkin
Copy link

epoberezkin commented Jan 29, 2022

Problem:

Decoding Aeson-encoded JSON in Swift.
Swift-equivalent of sum types - enums with parameters in cases - only supports the format that is equivalent to ObjectWithSingleField. Swift parses unnamed parameters from objects with keys "_0", "_1" etc., but an easy workaround is to have all parameters named, both in swift and in haskell.
The problem happens when some constructor has no parameters - Aeson always encodes it as an empty array, and Swift expects an empty object.

Proposal:

One of three options:

  1. Add an additional option that would only be used for sum encodings, something like sumEncodingUseObjects to always encode constructor parameters as objects (with keys "_0", "_1" etc.).
  2. An option nullaryToObject to encode nullary constructors as empty objects and keep constructors with parameters as is - records as objects, with positional parameters as arrays. Current decision to encode nullary constructors as array is quite arbitrary anyway, it simply follows positional parameters, but results in inconsistent JSON types if some constructors are records anyway...
  3. An option sumEncodingAllRecordsNullaryToObject - probably too complex to reason about...

What I need is solved with option 2 - going to implement it in a fork...

@epoberezkin epoberezkin changed the title [feature] parameter for ObjectWithSingleField sum type encoding to allow decoding in Swift [feature] an option for ObjectWithSingleField sum type encoding to allow decoding in Swift Jan 29, 2022
@epoberezkin
Copy link
Author

If it doesn't look like a feature that's needed - I'll just add it in a fork - any pointers where it is would be great, right now a bit lost in the code...

@epoberezkin
Copy link
Author

PR is mergeable, see the comment there.

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

1 participant