Description
JSON encoding/decoding for dependent-sum.
Description
Newtype wrappers around Data.Dependent.Sum.DSum, for use with -XDerivingVia
. These wrappers are helpful when parsing JSON objects where certain keys determine the type of the deserialised value.
README.md
aeson-dependent-sum
If you need to (de)serialise JSON from/to a dependent sum (from the dependent-sum
package), this library provides newtype
wrappers which you can use with the -XDerivingVia
language extension. You might want to do this if:
- You want to be work with a real value representing the sum's "tag"; or
- You want to take advantage of the
f
parameter provided byDSum
.
Example
data CharacterClass a where
Fighter :: CharacterClass Fighter
Rogue :: CharacterClass Rogue
Wizard :: CharacterClass Wizard
-- From the "constraints-extras" package:
$(deriveArgDict ''CharacterClass)
-- From the "dependent-sum-template" package. Not required, but useful:
$(deriveGShow ''CharacterClass)
$(deriveGEq ''CharacterClass)
$(deriveGCompare ''CharacterClass)
-- The derived `FromJSON`/`ToJSON` instances work on JSON objects like this:
-- {
-- "class": "fighter", -- or "rogue", or "wizard"
-- "data": { ... } -- the exact fields differ depending on the value at "class".
-- }
newtype Character = Character (DSum CharacterClass Identity)
deriving (FromJSON, ToJSON)
via (TaggedObject "Character" "class" "data" CharacterClass Identity)