@hackage aeson-dependent-sum0.1.0.1

JSON encoding/decoding for dependent-sum

aeson-dependent-sum

builds.sr.ht status GPLv3

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 by DSum.

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)