module Haddock.Utils.Json.Types
  ( Value (..)
  , typeOf
  , Pair
  , Object
  , object
  ) where

import Data.String

-- TODO: We may want to replace 'String' with 'Text' or 'ByteString'

-- | A JSON value represented as a Haskell value.
data Value
  = Object !Object
  | Array [Value]
  | String String
  | Number !Double
  | Bool !Bool
  | Null
  deriving (Value -> Value -> Bool
(Value -> Value -> Bool) -> (Value -> Value -> Bool) -> Eq Value
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Value -> Value -> Bool
== :: Value -> Value -> Bool
$c/= :: Value -> Value -> Bool
/= :: Value -> Value -> Bool
Eq, ReadPrec [Value]
ReadPrec Value
Int -> ReadS Value
ReadS [Value]
(Int -> ReadS Value)
-> ReadS [Value]
-> ReadPrec Value
-> ReadPrec [Value]
-> Read Value
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Value
readsPrec :: Int -> ReadS Value
$creadList :: ReadS [Value]
readList :: ReadS [Value]
$creadPrec :: ReadPrec Value
readPrec :: ReadPrec Value
$creadListPrec :: ReadPrec [Value]
readListPrec :: ReadPrec [Value]
Read, Int -> Value -> ShowS
[Value] -> ShowS
Value -> String
(Int -> Value -> ShowS)
-> (Value -> String) -> ([Value] -> ShowS) -> Show Value
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Value -> ShowS
showsPrec :: Int -> Value -> ShowS
$cshow :: Value -> String
show :: Value -> String
$cshowList :: [Value] -> ShowS
showList :: [Value] -> ShowS
Show)

typeOf :: Value -> String
typeOf :: Value -> String
typeOf Value
v = case Value
v of
  Object Object
_ -> String
"Object"
  Array [Value]
_ -> String
"Array"
  String String
_ -> String
"String"
  Number Double
_ -> String
"Number"
  Bool Bool
_ -> String
"Boolean"
  Value
Null -> String
"Null"

-- | A key\/value pair for an 'Object'
type Pair = (String, Value)

-- | A JSON \"object\" (key/value map).
type Object = [Pair]

-- | Create a 'Value' from a list of name\/value 'Pair's.
object :: [Pair] -> Value
object :: Object -> Value
object = Object -> Value
Object

instance IsString Value where
  fromString :: String -> Value
fromString = String -> Value
String