~jack/aeson-dependent-sum

JSON encoding/decoding for dependent-sum
Fix more nits, release 0.1.0.1
Fix documentation nits

refs

master
browse  log 
v0.1.0.1
release notes 

clone

read-only
https://git.sr.ht/~jack/aeson-dependent-sum
read/write
git@git.sr.ht:~jack/aeson-dependent-sum

You can also use your local clone with git send-email.

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