How to base generic derived "ToJSON" key determination on typeclass
- Dominant language
- Haskell
- Stars
- 1.3k
- Forks
- 336
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 4
Description
Lets say I have something like following:
```
data Alice = T { x1 :: T1, x2 :: T2, ... , xn :: TN } deriving Generic
class GetJSONKey t where
getJSONKey :: Proxy t -> Key
instance GetJSONKey T1 where
getJSONKey = fromString "z1"
instance GetJSONKey T2 where
getJSONKey = fromString "z2"
.
.
.
instance GetJSONKey TN where
getJSONKey = fromString "zn"
data Bob = T { y1 :: T1, y2 :: T2, ... , yn :: TN } deriving Generic
```
Is there some way I can make both `Alice` and `Bob` have the same JSON serialisation result? As in, both with field names "z1" ... "zn", ignoring the field names in the records?
Basically, can I do `deriving ToJSON` but somehow leave the key selection up to a typeclass that is defined on all my fields, instead of it being based on the record names in the code itself (and hence able to be modified by otherwise innocent refactorings). Or am I going to need to explicitly list the columns here in a manual `ToJSON` instance?
Contributor guide
Research direction
Start by examining aeson's generic ToJSON deriving behavior and the GetJSONKey class shown in the issue. Determine whether generic serialization can select keys from field types rather than record names; done is a clear answer or documented supported approach, including whether a manual ToJSON instance is required.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100