on da76712 help sign in
rel8/src/Rel8/Type/JSONEncoded.hs
29 lines 835 B history raw
1{-# language StandaloneKindSignatures #-}
3module Rel8.Type.JSONEncoded ( JSONEncoded(..) ) where
5-- aeson
6import Data.Aeson ( FromJSON, ToJSON, parseJSON, toJSON )
7import Data.Aeson.Types ( parseEither )
9-- base
10import Data.Kind ( Type )
11import Prelude
13-- rel8
14import Rel8.Type ( DBType(..) )
15import Rel8.Type.Information ( parseTypeInformation )
18-- | A deriving-via helper type for column types that store a Haskell value
19-- using a JSON encoding described by @aeson@'s 'ToJSON' and 'FromJSON' type
20-- classes.
21type JSONEncoded :: Type -> Type
22newtype JSONEncoded a = JSONEncoded { fromJSONEncoded :: a }
25instance (FromJSON a, ToJSON a) => DBType (JSONEncoded a) where
26 typeInformation = parseTypeInformation f g typeInformation
27 where
28 f = fmap JSONEncoded . parseEither parseJSON
29 g = toJSON . fromJSONEncoded