on 92f7c08 help sign in
rel8/src/Rel8/Type/JSONEncoded.hs
25 lines 733 B history raw
1module Rel8.Type.JSONEncoded ( JSONEncoded(..) ) where
3-- aeson
4import Data.Aeson ( FromJSON, ToJSON, parseJSON, toJSON )
5import Data.Aeson.Types ( parseEither )
7-- base
8import Prelude
10-- rel8
11import Rel8.Type ( DBType(..) )
12import Rel8.Type.Information ( parseTypeInformation )
15-- | A deriving-via helper type for column types that store a Haskell value
16-- using a JSON encoding described by @aeson@'s 'ToJSON' and 'FromJSON' type
17-- classes.
18newtype JSONEncoded a = JSONEncoded { fromJSONEncoded :: a }
21instance (FromJSON a, ToJSON a) => DBType (JSONEncoded a) where
22 typeInformation = parseTypeInformation f g typeInformation
23 where
24 f = fmap JSONEncoded . parseEither parseJSON
25 g = toJSON . fromJSONEncoded