aeson-gadt-th

Derivation of Aeson instances for GADTs

Latest on Hackage:0.2.0.0

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

BSD-3-Clause licensed by Obsidian Systems LLC
# aeson-gadt-th

Provides Template Haskell expressions for deriving `ToJSON` and `FromJSON` instances for GADTs.

## Example Usage:

```haskell
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}

import Data.Aeson
import Data.Aeson.GADT.TH

data A :: * -> * where
A_a :: A a
A_b :: Int -> A ()

data B c :: * -> * where
B_a :: c -> A a -> B c a
B_x :: B c x

deriveJSONGADT ''A
deriveJSONGADT ''B

main :: IO ()
main = return ()
```

## Encoding:
```
encode A_a
> "[\"A_a\",[]]"
```

## Decoding:

When decoding a JSON-encoded GADT, the result will be wrapped using [Data.Some.This](http://hackage.haskell.org/package/dependent-sum-0.4/docs/Data-Some.html).
```
case (decode $ encode A_a) :: Maybe (Some A) of
Nothing -> error "Couldn't decode
Just (This A_a) -> putStrLn "it worked"
Just (This A_b) -> putStrLn "wat"
> it worked
```

Changes

Revision history for aeson-gadt-th

0.2.0.0

  • Add changelog
  • Add option to modify constructor tag in derived JSON
  • Add test suite
comments powered byDisqus