haskell / haskell/aeson

Allow customization of record field unpacking for TaggedObject

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

Description

Currently the only way to get record field unpacking when using a sum-of-records is to avoid intermediate types and use the horrible misfeature that is partial record fields.

```
data Vehicle = Car { make :: String, speed :: Int } | Bicycle { brand :: String, gears :: Int }
deriving (Generic, ToJSON)

encode $ Car { make = "MINI", speed = 150 }
> {"tag":"Car","make":"MINI","speed":150}
```

vs:

```
data Vehicle = VehicleCar Car | VehicleBicycle Bicycle
deriving (Generic, ToJSON)

data Car = Car { make :: String, speed :: Int }
deriving (Generic, ToJSON)

data Bicycle = Bicycle { brand :: String, gears :: Int }
deriving (Generic, ToJSON)

encode . VehicleCar $ Car { make = "MINI", speed = 150 }
> {"tag":"VehicleCar","contents":{"speed":150,"make":"MINI"}}
```

If `TaggedObject` had an `unpack :: Bool` parameter users would be able to decide if they want unpacking or not, using `contentsFieldName` when it's `False`. Alternatively you could make `contentsFieldName` a `Maybe String`, this would just prevent any kind of error recovery if one of the sub-objects serializes to something other than a record, although it's unclear if such error recovery would be desirable anyway.

Contributor guide

Open the contributing guide

Research direction

Start by reading the TaggedObject implementation and its contentsFieldName handling. Compare the proposed unpack :: Bool and Maybe String APIs, then verify the chosen behavior for record and non-record sub-objects; done means users can select unpacked or contents-field encoding without breaking existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.