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

[ bsd3, json, library ] [ Propose Tags ] [ Report a vulnerability ]

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


[Skip to Readme]

Modules

[Index] [Quick Jump]

Flags

Automatic Flags
NameDescriptionDefault
build-readmeEnabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 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, 0.2.2, 0.2.3, 0.2.4, 0.2.5.0, 0.2.5.1, 0.2.5.2 (info)
Change log ChangeLog.md
Dependencies aeson (>=1.3 && <2.3), aeson-gadt-th, base (>=4.8 && <4.20), containers (>=0.5 && <0.7), dependent-map (>=0.3 && <0.5), dependent-sum (>=0.4 && <0.8), dependent-sum-template (>=0.2 && <0.3), template-haskell (>=2.11.0 && <2.22), th-abstraction (>=0.4 && <0.8), transformers (>=0.5 && <0.7) [details]
Tested with ghc ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.4 || ==8.10.2
License BSD-3-Clause
Copyright 2019 Obsidian Systems LLC
Author Obsidian Systems LLC
Maintainer maintainer@obsidian.systems
Category JSON
Source repo head: git clone git://github.com/obsidiansystems/aeson-gadt-th.git
Uploaded by abrar at 2024-05-28T20:40:45Z
Distributions NixOS:0.2.5.2
Reverse Dependencies 2 direct, 0 indirect [details]
Executables readme
Downloads 5319 total (8 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for aeson-gadt-th-0.2.5.2

[back to package description]

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.Some (Some(..))
>
> 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
>
> data D t x :: * -> * 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 :: * -> * 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 (Some (B_a 'a' (A_b 1))) -> "Succeeded"
>         _-> "Failed"
>     ]

Obsidian Systems