{-# LANGUAGE DeriveDataTypeable #-}
module Text.JSON.Types (
JSValue(..)
, JSString(..)
, toJSString
, JSObject(..)
, toJSObject
, get_field
, set_field
) where
import Data.Typeable ( Typeable )
import Data.String(IsString(..))
data JSValue
= JSNull
| JSBool !Bool
| JSRational Bool !Rational
| JSString JSString
| JSArray [JSValue]
| JSObject (JSObject JSValue)
deriving (Int -> JSValue -> ShowS
[JSValue] -> ShowS
JSValue -> String
(Int -> JSValue -> ShowS)
-> (JSValue -> String) -> ([JSValue] -> ShowS) -> Show JSValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSValue -> ShowS
showsPrec :: Int -> JSValue -> ShowS
$cshow :: JSValue -> String
show :: JSValue -> String
$cshowList :: [JSValue] -> ShowS
showList :: [JSValue] -> ShowS
Show, ReadPrec [JSValue]
ReadPrec JSValue
Int -> ReadS JSValue
ReadS [JSValue]
(Int -> ReadS JSValue)
-> ReadS [JSValue]
-> ReadPrec JSValue
-> ReadPrec [JSValue]
-> Read JSValue
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS JSValue
readsPrec :: Int -> ReadS JSValue
$creadList :: ReadS [JSValue]
readList :: ReadS [JSValue]
$creadPrec :: ReadPrec JSValue
readPrec :: ReadPrec JSValue
$creadListPrec :: ReadPrec [JSValue]
readListPrec :: ReadPrec [JSValue]
Read, JSValue -> JSValue -> Bool
(JSValue -> JSValue -> Bool)
-> (JSValue -> JSValue -> Bool) -> Eq JSValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSValue -> JSValue -> Bool
== :: JSValue -> JSValue -> Bool
$c/= :: JSValue -> JSValue -> Bool
/= :: JSValue -> JSValue -> Bool
Eq, Eq JSValue
Eq JSValue
-> (JSValue -> JSValue -> Ordering)
-> (JSValue -> JSValue -> Bool)
-> (JSValue -> JSValue -> Bool)
-> (JSValue -> JSValue -> Bool)
-> (JSValue -> JSValue -> Bool)
-> (JSValue -> JSValue -> JSValue)
-> (JSValue -> JSValue -> JSValue)
-> Ord JSValue
JSValue -> JSValue -> Bool
JSValue -> JSValue -> Ordering
JSValue -> JSValue -> JSValue
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: JSValue -> JSValue -> Ordering
compare :: JSValue -> JSValue -> Ordering
$c< :: JSValue -> JSValue -> Bool
< :: JSValue -> JSValue -> Bool
$c<= :: JSValue -> JSValue -> Bool
<= :: JSValue -> JSValue -> Bool
$c> :: JSValue -> JSValue -> Bool
> :: JSValue -> JSValue -> Bool
$c>= :: JSValue -> JSValue -> Bool
>= :: JSValue -> JSValue -> Bool
$cmax :: JSValue -> JSValue -> JSValue
max :: JSValue -> JSValue -> JSValue
$cmin :: JSValue -> JSValue -> JSValue
min :: JSValue -> JSValue -> JSValue
Ord, Typeable)
newtype JSString = JSONString { JSString -> String
fromJSString :: String }
deriving (JSString -> JSString -> Bool
(JSString -> JSString -> Bool)
-> (JSString -> JSString -> Bool) -> Eq JSString
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSString -> JSString -> Bool
== :: JSString -> JSString -> Bool
$c/= :: JSString -> JSString -> Bool
/= :: JSString -> JSString -> Bool
Eq, Eq JSString
Eq JSString
-> (JSString -> JSString -> Ordering)
-> (JSString -> JSString -> Bool)
-> (JSString -> JSString -> Bool)
-> (JSString -> JSString -> Bool)
-> (JSString -> JSString -> Bool)
-> (JSString -> JSString -> JSString)
-> (JSString -> JSString -> JSString)
-> Ord JSString
JSString -> JSString -> Bool
JSString -> JSString -> Ordering
JSString -> JSString -> JSString
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: JSString -> JSString -> Ordering
compare :: JSString -> JSString -> Ordering
$c< :: JSString -> JSString -> Bool
< :: JSString -> JSString -> Bool
$c<= :: JSString -> JSString -> Bool
<= :: JSString -> JSString -> Bool
$c> :: JSString -> JSString -> Bool
> :: JSString -> JSString -> Bool
$c>= :: JSString -> JSString -> Bool
>= :: JSString -> JSString -> Bool
$cmax :: JSString -> JSString -> JSString
max :: JSString -> JSString -> JSString
$cmin :: JSString -> JSString -> JSString
min :: JSString -> JSString -> JSString
Ord, Int -> JSString -> ShowS
[JSString] -> ShowS
JSString -> String
(Int -> JSString -> ShowS)
-> (JSString -> String) -> ([JSString] -> ShowS) -> Show JSString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSString -> ShowS
showsPrec :: Int -> JSString -> ShowS
$cshow :: JSString -> String
show :: JSString -> String
$cshowList :: [JSString] -> ShowS
showList :: [JSString] -> ShowS
Show, ReadPrec [JSString]
ReadPrec JSString
Int -> ReadS JSString
ReadS [JSString]
(Int -> ReadS JSString)
-> ReadS [JSString]
-> ReadPrec JSString
-> ReadPrec [JSString]
-> Read JSString
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS JSString
readsPrec :: Int -> ReadS JSString
$creadList :: ReadS [JSString]
readList :: ReadS [JSString]
$creadPrec :: ReadPrec JSString
readPrec :: ReadPrec JSString
$creadListPrec :: ReadPrec [JSString]
readListPrec :: ReadPrec [JSString]
Read, Typeable)
toJSString :: String -> JSString
toJSString :: String -> JSString
toJSString = String -> JSString
JSONString
instance IsString JSString where
fromString :: String -> JSString
fromString = String -> JSString
toJSString
instance IsString JSValue where
fromString :: String -> JSValue
fromString = JSString -> JSValue
JSString (JSString -> JSValue) -> (String -> JSString) -> String -> JSValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> JSString
forall a. IsString a => String -> a
fromString
newtype JSObject e = JSONObject { forall e. JSObject e -> [(String, e)]
fromJSObject :: [(String, e)] }
deriving (JSObject e -> JSObject e -> Bool
(JSObject e -> JSObject e -> Bool)
-> (JSObject e -> JSObject e -> Bool) -> Eq (JSObject e)
forall e. Eq e => JSObject e -> JSObject e -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall e. Eq e => JSObject e -> JSObject e -> Bool
== :: JSObject e -> JSObject e -> Bool
$c/= :: forall e. Eq e => JSObject e -> JSObject e -> Bool
/= :: JSObject e -> JSObject e -> Bool
Eq, Eq (JSObject e)
Eq (JSObject e)
-> (JSObject e -> JSObject e -> Ordering)
-> (JSObject e -> JSObject e -> Bool)
-> (JSObject e -> JSObject e -> Bool)
-> (JSObject e -> JSObject e -> Bool)
-> (JSObject e -> JSObject e -> Bool)
-> (JSObject e -> JSObject e -> JSObject e)
-> (JSObject e -> JSObject e -> JSObject e)
-> Ord (JSObject e)
JSObject e -> JSObject e -> Bool
JSObject e -> JSObject e -> Ordering
JSObject e -> JSObject e -> JSObject e
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {e}. Ord e => Eq (JSObject e)
forall e. Ord e => JSObject e -> JSObject e -> Bool
forall e. Ord e => JSObject e -> JSObject e -> Ordering
forall e. Ord e => JSObject e -> JSObject e -> JSObject e
$ccompare :: forall e. Ord e => JSObject e -> JSObject e -> Ordering
compare :: JSObject e -> JSObject e -> Ordering
$c< :: forall e. Ord e => JSObject e -> JSObject e -> Bool
< :: JSObject e -> JSObject e -> Bool
$c<= :: forall e. Ord e => JSObject e -> JSObject e -> Bool
<= :: JSObject e -> JSObject e -> Bool
$c> :: forall e. Ord e => JSObject e -> JSObject e -> Bool
> :: JSObject e -> JSObject e -> Bool
$c>= :: forall e. Ord e => JSObject e -> JSObject e -> Bool
>= :: JSObject e -> JSObject e -> Bool
$cmax :: forall e. Ord e => JSObject e -> JSObject e -> JSObject e
max :: JSObject e -> JSObject e -> JSObject e
$cmin :: forall e. Ord e => JSObject e -> JSObject e -> JSObject e
min :: JSObject e -> JSObject e -> JSObject e
Ord, Int -> JSObject e -> ShowS
[JSObject e] -> ShowS
JSObject e -> String
(Int -> JSObject e -> ShowS)
-> (JSObject e -> String)
-> ([JSObject e] -> ShowS)
-> Show (JSObject e)
forall e. Show e => Int -> JSObject e -> ShowS
forall e. Show e => [JSObject e] -> ShowS
forall e. Show e => JSObject e -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall e. Show e => Int -> JSObject e -> ShowS
showsPrec :: Int -> JSObject e -> ShowS
$cshow :: forall e. Show e => JSObject e -> String
show :: JSObject e -> String
$cshowList :: forall e. Show e => [JSObject e] -> ShowS
showList :: [JSObject e] -> ShowS
Show, ReadPrec [JSObject e]
ReadPrec (JSObject e)
Int -> ReadS (JSObject e)
ReadS [JSObject e]
(Int -> ReadS (JSObject e))
-> ReadS [JSObject e]
-> ReadPrec (JSObject e)
-> ReadPrec [JSObject e]
-> Read (JSObject e)
forall e. Read e => ReadPrec [JSObject e]
forall e. Read e => ReadPrec (JSObject e)
forall e. Read e => Int -> ReadS (JSObject e)
forall e. Read e => ReadS [JSObject e]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall e. Read e => Int -> ReadS (JSObject e)
readsPrec :: Int -> ReadS (JSObject e)
$creadList :: forall e. Read e => ReadS [JSObject e]
readList :: ReadS [JSObject e]
$creadPrec :: forall e. Read e => ReadPrec (JSObject e)
readPrec :: ReadPrec (JSObject e)
$creadListPrec :: forall e. Read e => ReadPrec [JSObject e]
readListPrec :: ReadPrec [JSObject e]
Read, Typeable )
toJSObject :: [(String,a)] -> JSObject a
toJSObject :: forall a. [(String, a)] -> JSObject a
toJSObject = [(String, a)] -> JSObject a
forall a. [(String, a)] -> JSObject a
JSONObject
get_field :: JSObject a -> String -> Maybe a
get_field :: forall a. JSObject a -> String -> Maybe a
get_field (JSONObject [(String, a)]
xs) String
x = String -> [(String, a)] -> Maybe a
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
x [(String, a)]
xs
set_field :: JSObject a -> String -> a -> JSObject a
set_field :: forall a. JSObject a -> String -> a -> JSObject a
set_field (JSONObject [(String, a)]
xs) String
k a
v = [(String, a)] -> JSObject a
forall a. [(String, a)] -> JSObject a
JSONObject ((String
k,a
v) (String, a) -> [(String, a)] -> [(String, a)]
forall a. a -> [a] -> [a]
: ((String, a) -> Bool) -> [(String, a)] -> [(String, a)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
k)(String -> Bool) -> ((String, a) -> String) -> (String, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(String, a) -> String
forall a b. (a, b) -> a
fst) [(String, a)]
xs)