module TD.Data.JsonValue
  (JsonValue(..)) 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.JsonObjectMember as JsonObjectMember

-- | Represents a JSON value
data JsonValue
  = JsonValueNull -- ^ Represents a null JSON value
  | JsonValueBoolean -- ^ Represents a boolean JSON value
    { JsonValue -> Maybe Bool
value :: Maybe Bool -- ^ The value
    }
  | JsonValueNumber -- ^ Represents a numeric JSON value
    { JsonValue -> Maybe Double
_value :: Maybe Double -- ^ The value
    }
  | JsonValueString -- ^ Represents a string JSON value
    { JsonValue -> Maybe Text
__value :: Maybe T.Text -- ^ The value
    }
  | JsonValueArray -- ^ Represents a JSON array
    { JsonValue -> Maybe [JsonValue]
values :: Maybe [JsonValue] -- ^ The list of array elements
    }
  | JsonValueObject -- ^ Represents a JSON object
    { JsonValue -> Maybe [JsonObjectMember]
members :: Maybe [JsonObjectMember.JsonObjectMember] -- ^ The list of object members
    }
  deriving (JsonValue -> JsonValue -> Bool
(JsonValue -> JsonValue -> Bool)
-> (JsonValue -> JsonValue -> Bool) -> Eq JsonValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JsonValue -> JsonValue -> Bool
== :: JsonValue -> JsonValue -> Bool
$c/= :: JsonValue -> JsonValue -> Bool
/= :: JsonValue -> JsonValue -> Bool
Eq, Int -> JsonValue -> ShowS
[JsonValue] -> ShowS
JsonValue -> String
(Int -> JsonValue -> ShowS)
-> (JsonValue -> String)
-> ([JsonValue] -> ShowS)
-> Show JsonValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JsonValue -> ShowS
showsPrec :: Int -> JsonValue -> ShowS
$cshow :: JsonValue -> String
show :: JsonValue -> String
$cshowList :: [JsonValue] -> ShowS
showList :: [JsonValue] -> ShowS
Show)

instance I.ShortShow JsonValue where
  shortShow :: JsonValue -> String
shortShow JsonValue
JsonValueNull
      = String
"JsonValueNull"
  shortShow JsonValueBoolean
    { value :: JsonValue -> Maybe Bool
value = Maybe Bool
value_
    }
      = String
"JsonValueBoolean"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"value" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
value_
        ]
  shortShow JsonValueNumber
    { _value :: JsonValue -> Maybe Double
_value = Maybe Double
_value_
    }
      = String
"JsonValueNumber"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_value" String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
_value_
        ]
  shortShow JsonValueString
    { __value :: JsonValue -> Maybe Text
__value = Maybe Text
__value_
    }
      = String
"JsonValueString"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"__value" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
__value_
        ]
  shortShow JsonValueArray
    { values :: JsonValue -> Maybe [JsonValue]
values = Maybe [JsonValue]
values_
    }
      = String
"JsonValueArray"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"values" String -> Maybe [JsonValue] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [JsonValue]
values_
        ]
  shortShow JsonValueObject
    { members :: JsonValue -> Maybe [JsonObjectMember]
members = Maybe [JsonObjectMember]
members_
    }
      = String
"JsonValueObject"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"members" String -> Maybe [JsonObjectMember] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [JsonObjectMember]
members_
        ]

instance AT.FromJSON JsonValue where
  parseJSON :: Value -> Parser JsonValue
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
"jsonValueNull"    -> JsonValue -> Parser JsonValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure JsonValue
JsonValueNull
      String
"jsonValueBoolean" -> Value -> Parser JsonValue
parseJsonValueBoolean Value
v
      String
"jsonValueNumber"  -> Value -> Parser JsonValue
parseJsonValueNumber Value
v
      String
"jsonValueString"  -> Value -> Parser JsonValue
parseJsonValueString Value
v
      String
"jsonValueArray"   -> Value -> Parser JsonValue
parseJsonValueArray Value
v
      String
"jsonValueObject"  -> Value -> Parser JsonValue
parseJsonValueObject Value
v
      String
_                  -> Parser JsonValue
forall a. Monoid a => a
mempty
    
    where
      parseJsonValueBoolean :: A.Value -> AT.Parser JsonValue
      parseJsonValueBoolean :: Value -> Parser JsonValue
parseJsonValueBoolean = String -> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"JsonValueBoolean" ((Object -> Parser JsonValue) -> Value -> Parser JsonValue)
-> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
value_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        JsonValue -> Parser JsonValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JsonValue -> Parser JsonValue) -> JsonValue -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ JsonValueBoolean
          { value :: Maybe Bool
value = Maybe Bool
value_
          }
      parseJsonValueNumber :: A.Value -> AT.Parser JsonValue
      parseJsonValueNumber :: Value -> Parser JsonValue
parseJsonValueNumber = String -> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"JsonValueNumber" ((Object -> Parser JsonValue) -> Value -> Parser JsonValue)
-> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Double
_value_ <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        JsonValue -> Parser JsonValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JsonValue -> Parser JsonValue) -> JsonValue -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ JsonValueNumber
          { _value :: Maybe Double
_value = Maybe Double
_value_
          }
      parseJsonValueString :: A.Value -> AT.Parser JsonValue
      parseJsonValueString :: Value -> Parser JsonValue
parseJsonValueString = String -> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"JsonValueString" ((Object -> Parser JsonValue) -> Value -> Parser JsonValue)
-> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
__value_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        JsonValue -> Parser JsonValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JsonValue -> Parser JsonValue) -> JsonValue -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ JsonValueString
          { __value :: Maybe Text
__value = Maybe Text
__value_
          }
      parseJsonValueArray :: A.Value -> AT.Parser JsonValue
      parseJsonValueArray :: Value -> Parser JsonValue
parseJsonValueArray = String -> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"JsonValueArray" ((Object -> Parser JsonValue) -> Value -> Parser JsonValue)
-> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [JsonValue]
values_ <- Object
o Object -> Key -> Parser (Maybe [JsonValue])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"values"
        JsonValue -> Parser JsonValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JsonValue -> Parser JsonValue) -> JsonValue -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ JsonValueArray
          { values :: Maybe [JsonValue]
values = Maybe [JsonValue]
values_
          }
      parseJsonValueObject :: A.Value -> AT.Parser JsonValue
      parseJsonValueObject :: Value -> Parser JsonValue
parseJsonValueObject = String -> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"JsonValueObject" ((Object -> Parser JsonValue) -> Value -> Parser JsonValue)
-> (Object -> Parser JsonValue) -> Value -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [JsonObjectMember]
members_ <- Object
o Object -> Key -> Parser (Maybe [JsonObjectMember])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"members"
        JsonValue -> Parser JsonValue
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JsonValue -> Parser JsonValue) -> JsonValue -> Parser JsonValue
forall a b. (a -> b) -> a -> b
$ JsonValueObject
          { members :: Maybe [JsonObjectMember]
members = Maybe [JsonObjectMember]
members_
          }
  parseJSON Value
_ = Parser JsonValue
forall a. Monoid a => a
mempty

instance AT.ToJSON JsonValue where
  toJSON :: JsonValue -> Value
toJSON JsonValue
JsonValueNull
      = [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
"jsonValueNull"
        ]
  toJSON JsonValueBoolean
    { value :: JsonValue -> Maybe Bool
value = Maybe Bool
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
"jsonValueBoolean"
        , Key
"value" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
value_
        ]
  toJSON JsonValueNumber
    { _value :: JsonValue -> Maybe Double
_value = Maybe Double
_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
"jsonValueNumber"
        , Key
"value" Key -> Maybe Double -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Double
_value_
        ]
  toJSON JsonValueString
    { __value :: JsonValue -> Maybe Text
__value = Maybe Text
__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
"jsonValueString"
        , Key
"value" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
__value_
        ]
  toJSON JsonValueArray
    { values :: JsonValue -> Maybe [JsonValue]
values = Maybe [JsonValue]
values_
    }
      = [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
"jsonValueArray"
        , Key
"values" Key -> Maybe [JsonValue] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [JsonValue]
values_
        ]
  toJSON JsonValueObject
    { members :: JsonValue -> Maybe [JsonObjectMember]
members = Maybe [JsonObjectMember]
members_
    }
      = [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
"jsonValueObject"
        , Key
"members" Key -> Maybe [JsonObjectMember] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [JsonObjectMember]
members_
        ]