module TD.Data.JsonObjectMember
( JsonObjectMember(..)
, defaultJsonObjectMember
) where
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T
import {-# SOURCE #-} qualified TD.Data.JsonValue as JsonValue
data JsonObjectMember
= JsonObjectMember
{ JsonObjectMember -> Maybe Text
key :: Maybe T.Text
, JsonObjectMember -> Maybe JsonValue
value :: Maybe JsonValue.JsonValue
}
deriving (JsonObjectMember -> JsonObjectMember -> Bool
(JsonObjectMember -> JsonObjectMember -> Bool)
-> (JsonObjectMember -> JsonObjectMember -> Bool)
-> Eq JsonObjectMember
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JsonObjectMember -> JsonObjectMember -> Bool
== :: JsonObjectMember -> JsonObjectMember -> Bool
$c/= :: JsonObjectMember -> JsonObjectMember -> Bool
/= :: JsonObjectMember -> JsonObjectMember -> Bool
Eq, Int -> JsonObjectMember -> ShowS
[JsonObjectMember] -> ShowS
JsonObjectMember -> String
(Int -> JsonObjectMember -> ShowS)
-> (JsonObjectMember -> String)
-> ([JsonObjectMember] -> ShowS)
-> Show JsonObjectMember
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JsonObjectMember -> ShowS
showsPrec :: Int -> JsonObjectMember -> ShowS
$cshow :: JsonObjectMember -> String
show :: JsonObjectMember -> String
$cshowList :: [JsonObjectMember] -> ShowS
showList :: [JsonObjectMember] -> ShowS
Show)
instance I.ShortShow JsonObjectMember where
shortShow :: JsonObjectMember -> String
shortShow JsonObjectMember
{ key :: JsonObjectMember -> Maybe Text
key = Maybe Text
key_
, value :: JsonObjectMember -> Maybe JsonValue
value = Maybe JsonValue
value_
}
= String
"JsonObjectMember"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
[ String
"key" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
key_
, String
"value" String -> Maybe JsonValue -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe JsonValue
value_
]
instance AT.FromJSON JsonObjectMember where
parseJSON :: Value -> Parser JsonObjectMember
parseJSON v :: Value
v@(AT.Object Object
obj) = do
String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String
case String
t of
String
"jsonObjectMember" -> Value -> Parser JsonObjectMember
parseJsonObjectMember Value
v
String
_ -> Parser JsonObjectMember
forall a. Monoid a => a
mempty
where
parseJsonObjectMember :: A.Value -> AT.Parser JsonObjectMember
parseJsonObjectMember :: Value -> Parser JsonObjectMember
parseJsonObjectMember = String
-> (Object -> Parser JsonObjectMember)
-> Value
-> Parser JsonObjectMember
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"JsonObjectMember" ((Object -> Parser JsonObjectMember)
-> Value -> Parser JsonObjectMember)
-> (Object -> Parser JsonObjectMember)
-> Value
-> Parser JsonObjectMember
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Maybe Text
key_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"key"
Maybe JsonValue
value_ <- Object
o Object -> Key -> Parser (Maybe JsonValue)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:? Key
"value"
JsonObjectMember -> Parser JsonObjectMember
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JsonObjectMember -> Parser JsonObjectMember)
-> JsonObjectMember -> Parser JsonObjectMember
forall a b. (a -> b) -> a -> b
$ JsonObjectMember
{ key :: Maybe Text
key = Maybe Text
key_
, value :: Maybe JsonValue
value = Maybe JsonValue
value_
}
parseJSON Value
_ = Parser JsonObjectMember
forall a. Monoid a => a
mempty
instance AT.ToJSON JsonObjectMember where
toJSON :: JsonObjectMember -> Value
toJSON JsonObjectMember
{ key :: JsonObjectMember -> Maybe Text
key = Maybe Text
key_
, value :: JsonObjectMember -> Maybe JsonValue
value = Maybe JsonValue
value_
}
= [Pair] -> Value
A.object
[ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"jsonObjectMember"
, Key
"key" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
key_
, Key
"value" Key -> Maybe JsonValue -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe JsonValue
value_
]
defaultJsonObjectMember :: JsonObjectMember
defaultJsonObjectMember :: JsonObjectMember
defaultJsonObjectMember =
JsonObjectMember
{ key :: Maybe Text
key = Maybe Text
forall a. Maybe a
Nothing
, value :: Maybe JsonValue
value = Maybe JsonValue
forall a. Maybe a
Nothing
}