# aeson-dependent-sum
[](https://builds.sr.ht/~jack/aeson-dependent-sum?)
[](https://www.gnu.org/licenses/gpl-3.0.html)
If you need to (de)serialise JSON from/to a dependent sum (from the
[`dependent-sum`](https://hackage.haskell.org/package/dependent-sum)
package), this library provides `newtype` wrappers which you can use
with the
[`-XDerivingVia`](https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/deriving_via.html#extension-DerivingVia)
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
```haskell
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)
```