haskell / haskell/aeson

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

Open
#925 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
1.3k
Forks
336
Avg merge
3d 7h
Merged PRs (30d)
4

Description

# 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...

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.