aeson-gadt-th

Derivation of Aeson instances for GADTs

Stackage Nightly 2025-05-15:0.2.5.4
Latest on Hackage:0.2.5.4

See all snapshots aeson-gadt-th appears in

BSD-3-Clause licensed by Obsidian Systems LLC
Maintained by [email protected]
This version can be pinned in stack with:aeson-gadt-th-0.2.5.4@sha256:ea7d6f6f5e6ba8ed8550fa669221aac2334db025e80aa1d2d1b6f84f4b931ec1,2039

Module documentation for 0.2.5.4

aeson-gadt-th

Haskell Hackage Hackage CI Github CI BSD3 License

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

Example Usage:

> {-# LANGUAGE GADTs #-}
> {-# LANGUAGE KindSignatures #-}
> {-# LANGUAGE TemplateHaskell #-}
> {-# LANGUAGE FlexibleContexts #-}
> {-# LANGUAGE FlexibleInstances #-}
> {-# LANGUAGE UndecidableInstances #-}
> {-# LANGUAGE MultiParamTypeClasses #-}
> {-# OPTIONS_GHC -ddump-splices #-}

> import Data.Aeson
> import Data.Aeson.GADT.TH
> import Data.Dependent.Map (DMap)
> import Data.Dependent.Sum (DSum)
> import Data.Functor.Identity
> import Data.GADT.Compare
> import Data.GADT.Show.TH
> import Data.Kind (Type)
> import Data.Some (Some(..))

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

> deriveJSONGADT ''A
> deriveGShow ''A

> data B c :: Type -> Type where
>   B_a :: c -> A a -> B c a
>   B_x :: B c a

> deriveJSONGADT ''B

> data C t :: Type -> Type where
>   C_t :: t -> C t t

> deriveJSONGADT ''C

> data D t x :: Type -> Type where
>   D_t :: t -> D t x t
>   D_x :: x -> D t x x
>   D_i :: Int -> D t x Int

> deriveJSONGADT ''D

> data Auth token a where
>   Auth_Login :: String -> String -> Auth token (Either String token)

> deriveJSONGADT ''Auth

> -- Some real-world-ish examples.

> -- | Edit operations for `LabelledGraph`
> data LabelledGraphEdit v vm em :: Type -> Type where
>   LabelledGraphEdit_ClearAll :: LabelledGraphEdit v vm em ()
>   LabelledGraphEdit_AddVertex :: vm -> LabelledGraphEdit v vm em v
>   LabelledGraphEdit_AddEdge :: v -> v -> em -> LabelledGraphEdit v vm em ()
>   LabelledGraphEdit_SetVertexProperties :: v -> vm -> LabelledGraphEdit v vm em ()
>   LabelledGraphEdit_SetEdgeProperties :: v -> v -> em -> LabelledGraphEdit v vm em ()

> -- | PropertyGraphEdit operatios for `PropertyGraph`
> data PropertyGraphEdit v vp ep r where
>   PropertyGraphEdit_ClearAll :: PropertyGraphEdit v vp ep ()
>   PropertyGraphEdit_AddVertex :: (DMap vp Identity) -> PropertyGraphEdit v vp ep v
>   PropertyGraphEdit_AddEdge :: v -> v -> (DMap ep Identity) -> PropertyGraphEdit v vp ep ()
>   PropertyGraphEdit_SetVertexProperty :: GCompare vp => v -> DSum vp Identity -> PropertyGraphEdit v vp ep ()
>   PropertyGraphEdit_SetEdgeProperty :: GCompare ep => v -> v -> DSum ep Identity -> PropertyGraphEdit v vp ep ()

> -- | View operations for `LabelledGraph`
> data LabelledGraphView v vm em :: Type -> Type where
>   LabelledGraphView_All :: LabelledGraphView v vm em ()
>   LabelledGraphView_GetVertexProperties :: v -> LabelledGraphView v vm em vm
>   LabelledGraphView_GetEdgeProperties :: v -> v -> LabelledGraphView v vm em em

> deriveJSONGADT ''LabelledGraphEdit
> deriveJSONGADT ''PropertyGraphEdit
> deriveJSONGADT ''LabelledGraphView

> main :: IO ()
> main = do
>   putStrLn $ unlines
>     [ "Encoding of A_a:"
>     , show $ encode A_a
>     , "Decoding of encoded A_a:"
>     , show (decode $ encode A_a :: Maybe (Some A))
>     ]

>   putStrLn $ unlines
>     [ "Encoding of (A_b 1):"
>     , show $ encode (A_b 1)
>     , "Decoding of encoded (A_b 1):"
>     , show (decode $ encode (A_b 1) :: Maybe (Some A))
>     ]

>   putStrLn $ unlines
>     [ "Encoding of (B_a 'a' (A_b 1)):"
>     , show $ encode (B_a 'a' (A_b 1))
>     , "Decoding of encoded (B_a 'a' (A_b 1)):"
>     , case (decode $ encode (B_a 'a' (A_b 1)) :: Maybe (Some (B Char))) of
>         Just (Some (B_a 'a' (A_b 1))) -> "Succeeded"
>         _-> "Failed"
>     ]

Obsidian Systems

Changes

Revision history for aeson-gadt-th

0.2.5.4 - 2025-01-18

  • Loosen version bounds
  • Support GHC 9.10

0.2.5.3 - 2025-01-14

  • Loosen version bounds
  • Support GHC 9.10

0.2.5.2 - 2024-05-28

  • Loosen version bounds
  • Support GHC 9.8

0.2.5.1 - 2022-01-04

  • Remove dependency on th-extras. We were just using a reexport (under a different name) of something from th-abstractions anyways.
  • Support GHC 9.0

0.2.5.0 - 2020-11-18

  • Support for GHC 8.10
  • Support for aeson 1.5.*
  • Fix #21: deriveJSONGADT requires toJSON and parseJSON to be in scope
  • Fix #25: Test suite does not compile (on GHC 8.10)

0.2.4 - 2020-10-21

  • Support for GHC 8.8

0.2.2 - 2019-12-16

  • Do a better job determining which variables are rigid when looking for instances
  • Unify discovered instance head with argument type and make the same substitution in the context that constrains the instance we’re writing

0.2.1.2 - 2019-10-10

  • Add version bounds to cabal file

0.2.1.1 - 2019-05-17

  • Drop markdown-unlit in favor of “Bird”-style LHS to avoid some cross-compilation issues.

0.2.1.0 - 2019-05-09

  • Extend type variable substitution to handle all current cases in template-haskell.
  • Better deal with data constructors having an index that is polymorphic, but can be determined from the other type parameters.
  • Handle data constructors that are constrained by type classes.

0.2.0.0 - 2019-03-28

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