aeson-gadt-th: Derivation of Aeson instances for GADTs

[ bsd3, json, library, program ] [ Propose Tags ]

Template Haskell for generating ToJSON and FromJSON instances for GADTs. See README.md for examples.


[Skip to Readme]
Versions [faq] 0.1.0.0, 0.1.1.0, 0.1.2.0, 0.1.2.1, 0.2.0.0, 0.2.1.0, 0.2.1.1, 0.2.1.2
Change log ChangeLog.md
Dependencies aeson, aeson-gadt-th, base (>=4.11 && <4.13), containers, dependent-map, dependent-sum, dependent-sum-template, template-haskell, transformers [details]
License BSD-3-Clause
Copyright 2019 Obsidian Systems LLC
Author Obsidian Systems LLC
Maintainer maintainer@obsidian.systems
Revised Revision 1 made by phadej at Tue Oct 8 19:24:35 UTC 2019
Category JSON
Source repo head: git clone git://github.com/obsidiansystems/aeson-gadt-th.git
Uploaded by abrar at Fri May 17 09:25:00 UTC 2019
Distributions NixOS:0.2.1.2
Executables readme
Downloads 987 total (244 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs uploaded by user
Build status unknown [no reports yet]

Modules

[Index]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees


Readme for aeson-gadt-th-0.2.1.1

[back to package description]

aeson-gadt-th

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 #-}
> 
> import Data.Aeson
> import Data.Aeson.GADT.TH
> 
> import Data.Dependent.Map (DMap, Some(..))
> import Data.Dependent.Sum (DSum)
> import Data.Functor.Identity
> import Data.GADT.Compare
> import Data.GADT.Show.TH
> 
> data A :: * -> * where
>   A_a :: A a
>   A_b :: Int -> A ()
> 
> deriveJSONGADT ''A
> deriveGShow ''A
>
> data B c :: * -> * where
>   B_a :: c -> A a -> B c a
>   B_x :: B c a
>
> deriveJSONGADT ''B
> 
> data C t :: * -> * where
>   C_t :: t -> C t t
> 
> deriveJSONGADT ''C
> 
> -- Some real-world-ish examples.
> 
> -- | Edit operations for `LabelledGraph`
> data LabelledGraphEdit v vm em :: * -> * 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 :: * -> * 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 (This (B_a 'a' (A_b 1))) -> "Succeeded"
>         _-> "Failed"
>     ]