{-# LANGUAGE CPP #-}
module Composite.Aeson.Record
( ToJsonField(..), FromJsonField(..), JsonField(..)
, field, valField, field', fromField, valFromField, fromField', toField, toField'
, defaultField, valDefaultField, defaultField'
, optionalField, valOptionalField, optionalField', fromOptionalField, valFromOptionalField, fromOptionalField', toOptionalField, toOptionalField', defaultValFromOptionalField
, JsonFormatRecord, ToJsonFormatRecord, FromJsonFormatRecord, zipJsonFormatRecord, toJsonFormatRecord, fromJsonFormatRecord
, DefaultJsonFormatRecord, defaultJsonFormatRecord
, RecordToJsonObject, recordToJsonObject, recordToJson
, RecordFromJson, recordFromJson
, recordJsonFormat
, valMaybeParser, valMaybeField
, MaybeRecordFromJson, maybeRecordFromJson
) where
import Composite.Aeson.Base
( JsonProfunctor(JsonProfunctor)
, JsonFormat(JsonFormat)
, fromJsonWithFormat
, wrappedJsonFormat
)
import Composite.Aeson.Formats.Default (DefaultJsonFormat(defaultJsonFormat))
import Composite.Record ((:->)(Val))
import Control.Lens (Wrapped(type Unwrapped, _Wrapped'), from, review, view)
import Control.Monad (join)
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.BetterErrors as ABE
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key as Aeson.Key
import qualified Data.Aeson.KeyMap as Aeson.KeyMap
#else
import qualified Data.HashMap.Strict as HM
#endif
import Data.Functor.Contravariant (Contravariant, contramap)
import Data.Functor.Identity (Identity(Identity))
import Data.Maybe (fromMaybe)
import Data.Proxy (Proxy(Proxy))
import Data.Text (Text, pack)
import Data.Vinyl (RApply, RMap, Rec((:&), RNil), rmap, rzipWith)
import Data.Vinyl.Functor (Compose (..), (:.))
import GHC.TypeLits (KnownSymbol, symbolVal)
newtype ToJsonField a = ToJsonField { forall a. ToJsonField a -> a -> Maybe Value
unToJsonField :: a -> Maybe Aeson.Value }
instance Contravariant ToJsonField where
contramap :: forall a' a. (a' -> a) -> ToJsonField a -> ToJsonField a'
contramap a' -> a
f (ToJsonField a -> Maybe Value
g) = forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField (a -> Maybe Value
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. a' -> a
f)
newtype FromJsonField e a = FromJsonField { forall e a. FromJsonField e a -> Text -> Parse e a
unFromJsonField :: Text -> ABE.Parse e a }
instance Functor (FromJsonField e) where
fmap :: forall a b. (a -> b) -> FromJsonField e a -> FromJsonField e b
fmap a -> b
f (FromJsonField Text -> Parse e a
g) = forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Parse e a
g)
data JsonField e a = JsonField (a -> Maybe Aeson.Value) (Text -> ABE.Parse e a)
field :: (Wrapped a', Unwrapped a' ~ a) => JsonFormat e a -> JsonField e a'
field :: forall a' a e.
(Wrapped a', Unwrapped a' ~ a) =>
JsonFormat e a -> JsonField e a'
field JsonFormat e a
fmt = forall e a. JsonFormat e a -> JsonField e a
field' (forall a e.
Wrapped a =>
JsonFormat e (Unwrapped a) -> JsonFormat e a
wrappedJsonFormat JsonFormat e a
fmt)
valField :: forall s a e. JsonFormat e a -> JsonField e (s :-> a)
valField :: forall (s :: Symbol) a e. JsonFormat e a -> JsonField e (s :-> a)
valField = forall a' a e.
(Wrapped a', Unwrapped a' ~ a) =>
JsonFormat e a -> JsonField e a'
field
field' :: JsonFormat e a -> JsonField e a
field' :: forall e a. JsonFormat e a -> JsonField e a
field' (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) = forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
o) (forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m a
`ABE.key` Parse e a
i)
defaultField :: (Wrapped a', Unwrapped a' ~ a) => a -> JsonFormat e a -> JsonField e a'
defaultField :: forall a' a e.
(Wrapped a', Unwrapped a' ~ a) =>
a -> JsonFormat e a -> JsonField e a'
defaultField a
default_ (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) =
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField
(forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
o forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped')
(\Text
k -> forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (forall s t a b. AnIso s t a b -> Iso b a t s
from forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped') forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe a
default_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i))
valDefaultField :: forall s a e. a -> JsonFormat e a -> JsonField e (s :-> a)
valDefaultField :: forall (s :: Symbol) a e.
a -> JsonFormat e a -> JsonField e (s :-> a)
valDefaultField = forall a' a e.
(Wrapped a', Unwrapped a' ~ a) =>
a -> JsonFormat e a -> JsonField e a'
defaultField
defaultField' :: a -> JsonFormat e a -> JsonField e a
defaultField' :: forall a e. a -> JsonFormat e a -> JsonField e a
defaultField' a
default_ (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) =
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField
(forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
o)
(\Text
k -> forall a. a -> Maybe a -> a
fromMaybe a
default_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i))
fromField :: Wrapped a => ABE.Parse e (Unwrapped a) -> FromJsonField e a
fromField :: forall a e. Wrapped a => Parse e (Unwrapped a) -> FromJsonField e a
fromField = forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m a
ABE.key forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped')
valFromField :: forall s a e. ABE.Parse e a -> FromJsonField e (s :-> a)
valFromField :: forall (s :: Symbol) a e. Parse e a -> FromJsonField e (s :-> a)
valFromField = forall a e. Wrapped a => Parse e (Unwrapped a) -> FromJsonField e a
fromField
fromField' :: ABE.Parse e a -> FromJsonField e a
fromField' :: forall e a. Parse e a -> FromJsonField e a
fromField' = forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m a
ABE.key
toField :: (Wrapped a', Unwrapped a' ~ a) => (a -> Aeson.Value) -> ToJsonField a'
toField :: forall a' a.
(Wrapped a', Unwrapped a' ~ a) =>
(a -> Value) -> ToJsonField a'
toField a -> Value
o = forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value
o forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped'
toField' :: (a -> Aeson.Value) -> ToJsonField a
toField' :: forall a. (a -> Value) -> ToJsonField a
toField' = forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just
optionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => JsonFormat e a -> JsonField e a'
optionalField :: forall a' a e.
(Wrapped a', Unwrapped a' ~ Maybe a) =>
JsonFormat e a -> JsonField e a'
optionalField (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) =
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField
(forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped')
(\ Text
k -> forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (forall s t a b. AnIso s t a b -> Iso b a t s
from forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped') forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i))
valOptionalField :: forall s a e. JsonFormat e a -> JsonField e (s :-> Maybe a)
valOptionalField :: forall (s :: Symbol) a e.
JsonFormat e a -> JsonField e (s :-> Maybe a)
valOptionalField = forall a' a e.
(Wrapped a', Unwrapped a' ~ Maybe a) =>
JsonFormat e a -> JsonField e a'
optionalField
optionalField' :: JsonFormat e a -> JsonField e (Maybe a)
optionalField' :: forall e a. JsonFormat e a -> JsonField e (Maybe a)
optionalField' (JsonFormat (JsonProfunctor a -> Value
o Parse e a
i)) =
forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField
(forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o)
(\ Text
k -> forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i))
fromOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => ABE.Parse e a -> FromJsonField e a'
fromOptionalField :: forall a' a e.
(Wrapped a', Unwrapped a' ~ Maybe a) =>
Parse e a -> FromJsonField e a'
fromOptionalField Parse e a
i = forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField Text -> ParseT e Identity a'
f
where
f :: Text -> ParseT e Identity a'
f Text
k = forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (forall s t a b. AnIso s t a b -> Iso b a t s
from forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped') forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i)
valFromOptionalField :: forall s a e. ABE.Parse e a -> FromJsonField e (s :-> Maybe a)
valFromOptionalField :: forall (s :: Symbol) a e.
Parse e a -> FromJsonField e (s :-> Maybe a)
valFromOptionalField = forall a' a e.
(Wrapped a', Unwrapped a' ~ Maybe a) =>
Parse e a -> FromJsonField e a'
fromOptionalField
fromOptionalField' :: ABE.Parse e a -> FromJsonField e (Maybe a)
fromOptionalField' :: forall e a. Parse e a -> FromJsonField e (Maybe a)
fromOptionalField' Parse e a
i = forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField Text -> ParseT e Identity (Maybe a)
f
where
f :: Text -> ParseT e Identity (Maybe a)
f Text
k = forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
k (forall (m :: * -> *) err a.
(Functor m, Monad m) =>
ParseT err m a -> ParseT err m (Maybe a)
ABE.perhaps Parse e a
i)
defaultValFromOptionalField :: forall s a e. a -> JsonFormat e a -> FromJsonField e (s :-> a)
defaultValFromOptionalField :: forall (s :: Symbol) a e.
a -> JsonFormat e a -> FromJsonField e (s :-> a)
defaultValFromOptionalField a
default_ = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (s :: Symbol) a. a -> s :-> a
Val forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe a
default_) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Parse e a -> FromJsonField e (Maybe a)
fromOptionalField' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. JsonFormat e a -> Parse e a
fromJsonWithFormat
toOptionalField :: (Wrapped a', Unwrapped a' ~ Maybe a) => (a -> Aeson.Value) -> ToJsonField a'
toOptionalField :: forall a' a.
(Wrapped a', Unwrapped a' ~ Maybe a) =>
(a -> Value) -> ToJsonField a'
toOptionalField a -> Value
o = forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped')
toOptionalField' :: (a -> Aeson.Value) -> ToJsonField (Maybe a)
toOptionalField' :: forall a. (a -> Value) -> ToJsonField (Maybe a)
toOptionalField' a -> Value
o = forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Value
o)
valMaybeParser :: forall s a e. ABE.Parse e a -> (FromJsonField e :. Maybe) (s :-> a)
valMaybeParser :: forall (s :: Symbol) a e.
Parse e a -> (:.) (FromJsonField e) Maybe (s :-> a)
valMaybeParser Parse e a
r =
let r' :: Text -> ParseT e Identity (Maybe (s :-> a))
r' Text
str = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (s :: Symbol) a. a -> s :-> a
Val forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) err a.
(Functor m, Monad m) =>
Text -> ParseT err m a -> ParseT err m (Maybe a)
ABE.keyMay Text
str Parse e a
r
in forall l k (f :: l -> *) (g :: k -> l) (x :: k).
f (g x) -> Compose f g x
Compose (forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField Text -> ParseT e Identity (Maybe (s :-> a))
r')
valMaybeField :: forall s a e. JsonFormat e a -> (FromJsonField e :. Maybe) (s :-> a)
valMaybeField :: forall (s :: Symbol) a e.
JsonFormat e a -> (:.) (FromJsonField e) Maybe (s :-> a)
valMaybeField (JsonFormat (JsonProfunctor a -> Value
_ Parse e a
r)) = forall (s :: Symbol) a e.
Parse e a -> (:.) (FromJsonField e) Maybe (s :-> a)
valMaybeParser Parse e a
r
type JsonFormatRecord e rs = Rec (JsonField e) rs
zipJsonFormatRecord :: (RMap rs, RApply rs) => ToJsonFormatRecord rs -> FromJsonFormatRecord e rs -> JsonFormatRecord e rs
zipJsonFormatRecord :: forall (rs :: [*]) e.
(RMap rs, RApply rs) =>
ToJsonFormatRecord rs
-> FromJsonFormatRecord e rs -> JsonFormatRecord e rs
zipJsonFormatRecord = forall {u} (xs :: [u]) (f :: u -> *) (g :: u -> *) (h :: u -> *).
(RMap xs, RApply xs) =>
(forall (x :: u). f x -> g x -> h x)
-> Rec f xs -> Rec g xs -> Rec h xs
rzipWith (\ (ToJsonField x -> Maybe Value
o) (FromJsonField Text -> Parse e x
i) -> forall e a.
(a -> Maybe Value) -> (Text -> Parse e a) -> JsonField e a
JsonField x -> Maybe Value
o Text -> Parse e x
i)
type FromJsonFormatRecord e rs = Rec (FromJsonField e) rs
fromJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> FromJsonFormatRecord e rs
fromJsonFormatRecord :: forall (rs :: [*]) e.
RMap rs =>
JsonFormatRecord e rs -> FromJsonFormatRecord e rs
fromJsonFormatRecord = forall {u} (rs :: [u]) (f :: u -> *) (g :: u -> *).
RMap rs =>
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
rmap (\ (JsonField x -> Maybe Value
_ Text -> Parse e x
i) -> forall e a. (Text -> Parse e a) -> FromJsonField e a
FromJsonField Text -> Parse e x
i)
type ToJsonFormatRecord rs = Rec ToJsonField rs
toJsonFormatRecord :: RMap rs => JsonFormatRecord e rs -> ToJsonFormatRecord rs
toJsonFormatRecord :: forall (rs :: [*]) e.
RMap rs =>
JsonFormatRecord e rs -> ToJsonFormatRecord rs
toJsonFormatRecord = forall {u} (rs :: [u]) (f :: u -> *) (g :: u -> *).
RMap rs =>
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
rmap (\ (JsonField x -> Maybe Value
o Text -> Parse e x
_) -> forall a. (a -> Maybe Value) -> ToJsonField a
ToJsonField x -> Maybe Value
o)
class RecordToJsonObject rs where
recordToJsonObject :: Rec ToJsonField rs -> Rec Identity rs -> Aeson.Object
instance RecordToJsonObject '[] where
recordToJsonObject :: Rec ToJsonField '[] -> Rec Identity '[] -> Object
recordToJsonObject Rec ToJsonField '[]
_ = forall a b. a -> b -> a
const forall a. Monoid a => a
mempty
instance forall s a rs. (KnownSymbol s, RecordToJsonObject rs) => RecordToJsonObject (s :-> a ': rs) where
recordToJsonObject :: Rec ToJsonField ((s :-> a) : rs)
-> Rec Identity ((s :-> a) : rs) -> Object
recordToJsonObject (ToJsonField r -> Maybe Value
aToField :& Rec ToJsonField rs
fs) (Identity r
a :& Rec Identity rs
as) =
#if MIN_VERSION_aeson(2,0,0)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall v. Key -> v -> KeyMap v -> KeyMap v
Aeson.KeyMap.insert (String -> Key
Aeson.Key.fromString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal forall a b. (a -> b) -> a -> b
$ (forall {k} (t :: k). Proxy t
Proxy :: Proxy s))) (r -> Maybe Value
aToField r
a) forall a b. (a -> b) -> a -> b
$
#else
maybe id (HM.insert (pack . symbolVal $ (Proxy :: Proxy s))) (aToField a) $
#endif
forall (rs :: [*]).
RecordToJsonObject rs =>
Rec ToJsonField rs -> Rec Identity rs -> Object
recordToJsonObject Rec ToJsonField rs
fs Rec Identity rs
as
recordToJson :: RecordToJsonObject rs => Rec ToJsonField rs -> Rec Identity rs -> Aeson.Value
recordToJson :: forall (rs :: [*]).
RecordToJsonObject rs =>
Rec ToJsonField rs -> Rec Identity rs -> Value
recordToJson = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Object -> Value
Aeson.Object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (rs :: [*]).
RecordToJsonObject rs =>
Rec ToJsonField rs -> Rec Identity rs -> Object
recordToJsonObject
class RecordFromJson rs where
recordFromJson :: Rec (FromJsonField e) rs -> ABE.Parse e (Rec Identity rs)
instance RecordFromJson '[] where
recordFromJson :: forall e. Rec (FromJsonField e) '[] -> Parse e (Rec Identity '[])
recordFromJson Rec (FromJsonField e) '[]
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {u} (a :: u -> *). Rec a '[]
RNil
instance forall s a rs. (KnownSymbol s, RecordFromJson rs) => RecordFromJson (s :-> a ': rs) where
recordFromJson :: forall e.
Rec (FromJsonField e) ((s :-> a) : rs)
-> Parse e (Rec Identity ((s :-> a) : rs))
recordFromJson (FromJsonField Text -> Parse e r
aFromField :& Rec (FromJsonField e) rs
fs) =
forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
(:&)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. a -> Identity a
Identity forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parse e r
aFromField (String -> Text
pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal forall a b. (a -> b) -> a -> b
$ (forall {k} (t :: k). Proxy t
Proxy :: Proxy s)))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (rs :: [*]) e.
RecordFromJson rs =>
Rec (FromJsonField e) rs -> Parse e (Rec Identity rs)
recordFromJson Rec (FromJsonField e) rs
fs
recordJsonFormat :: (RMap rs, RecordToJsonObject rs, RecordFromJson rs) => JsonFormatRecord e rs -> JsonFormat e (Rec Identity rs)
recordJsonFormat :: forall (rs :: [*]) e.
(RMap rs, RecordToJsonObject rs, RecordFromJson rs) =>
JsonFormatRecord e rs -> JsonFormat e (Rec Identity rs)
recordJsonFormat JsonFormatRecord e rs
formatRec =
forall e a. JsonProfunctor e a a -> JsonFormat e a
JsonFormat forall a b. (a -> b) -> a -> b
$ forall e a b. (a -> Value) -> Parse e b -> JsonProfunctor e a b
JsonProfunctor
(forall (rs :: [*]).
RecordToJsonObject rs =>
Rec ToJsonField rs -> Rec Identity rs -> Value
recordToJson forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (rs :: [*]) e.
RMap rs =>
JsonFormatRecord e rs -> ToJsonFormatRecord rs
toJsonFormatRecord forall a b. (a -> b) -> a -> b
$ JsonFormatRecord e rs
formatRec)
(forall (rs :: [*]) e.
RecordFromJson rs =>
Rec (FromJsonField e) rs -> Parse e (Rec Identity rs)
recordFromJson forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (rs :: [*]) e.
RMap rs =>
JsonFormatRecord e rs -> FromJsonFormatRecord e rs
fromJsonFormatRecord forall a b. (a -> b) -> a -> b
$ JsonFormatRecord e rs
formatRec)
class DefaultJsonFormatRecord rs where
defaultJsonFormatRecord :: JsonFormatRecord e rs
instance (KnownSymbol s, DefaultJsonFormat a, DefaultJsonFormatRecord rs) => DefaultJsonFormatRecord (s :-> a ': rs) where
defaultJsonFormatRecord :: forall e. JsonFormatRecord e ((s :-> a) : rs)
defaultJsonFormatRecord = forall a' a e.
(Wrapped a', Unwrapped a' ~ a) =>
JsonFormat e a -> JsonField e a'
field forall a e. DefaultJsonFormat a => JsonFormat e a
defaultJsonFormat forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& forall (rs :: [*]) e.
DefaultJsonFormatRecord rs =>
JsonFormatRecord e rs
defaultJsonFormatRecord
instance DefaultJsonFormatRecord '[] where
defaultJsonFormatRecord :: forall e. JsonFormatRecord e '[]
defaultJsonFormatRecord = forall {u} (a :: u -> *). Rec a '[]
RNil
class MaybeRecordFromJson rs where
maybeRecordFromJson :: Rec (FromJsonField e :. Maybe) rs -> ABE.Parse e (Rec Maybe rs)
instance MaybeRecordFromJson '[] where
maybeRecordFromJson :: forall e.
Rec (FromJsonField e :. Maybe) '[] -> Parse e (Rec Maybe '[])
maybeRecordFromJson Rec (FromJsonField e :. Maybe) '[]
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {u} (a :: u -> *). Rec a '[]
RNil
instance forall s a rs. (KnownSymbol s, MaybeRecordFromJson rs) => MaybeRecordFromJson (s :-> a ': rs) where
maybeRecordFromJson :: forall e.
Rec (FromJsonField e :. Maybe) ((s :-> a) : rs)
-> Parse e (Rec Maybe ((s :-> a) : rs))
maybeRecordFromJson (Compose (FromJsonField Text -> Parse e (Maybe r)
aFromField) :& Rec (FromJsonField e :. Maybe) rs
fs) =
forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
(:&)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parse e (Maybe r)
aFromField (String -> Text
pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal forall a b. (a -> b) -> a -> b
$ (forall {k} (t :: k). Proxy t
Proxy :: Proxy s))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (rs :: [*]) e.
MaybeRecordFromJson rs =>
Rec (FromJsonField e :. Maybe) rs -> Parse e (Rec Maybe rs)
maybeRecordFromJson Rec (FromJsonField e :. Maybe) rs
fs