module Distribution.Simple.Utils.Json
( Json(..)
, renderJson
) where
data Json = JsonArray [Json]
| JsonBool !Bool
| JsonNull
| JsonNumber !Int
| JsonObject [(String, Json)]
| JsonString !String
renderJson :: Json -> ShowS
renderJson :: Json -> ShowS
renderJson (JsonArray [Json]
objs) =
String -> String -> ShowS -> ShowS
surround String
"[" String
"]" forall a b. (a -> b) -> a -> b
$ String -> [ShowS] -> ShowS
intercalate String
"," forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Json -> ShowS
renderJson [Json]
objs
renderJson (JsonBool Bool
True) = String -> ShowS
showString String
"true"
renderJson (JsonBool Bool
False) = String -> ShowS
showString String
"false"
renderJson Json
JsonNull = String -> ShowS
showString String
"null"
renderJson (JsonNumber Int
n) = forall a. Show a => a -> ShowS
shows Int
n
renderJson (JsonObject [(String, Json)]
attrs) =
String -> String -> ShowS -> ShowS
surround String
"{" String
"}" forall a b. (a -> b) -> a -> b
$ String -> [ShowS] -> ShowS
intercalate String
"," forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (String, Json) -> ShowS
render [(String, Json)]
attrs
where
render :: (String, Json) -> ShowS
render (String
k,Json
v) = (String -> String -> ShowS -> ShowS
surround String
"\"" String
"\"" forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString' String
k) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
":" forall b c a. (b -> c) -> (a -> b) -> a -> c
. Json -> ShowS
renderJson Json
v
renderJson (JsonString String
s) = String -> String -> ShowS -> ShowS
surround String
"\"" String
"\"" forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString' String
s
surround :: String -> String -> ShowS -> ShowS
surround :: String -> String -> ShowS -> ShowS
surround String
begin String
end ShowS
middle = String -> ShowS
showString String
begin forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
middle forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
end
showString' :: String -> ShowS
showString' :: String -> ShowS
showString' String
xs = String -> ShowS
showStringWorker String
xs
where
showStringWorker :: String -> ShowS
showStringWorker :: String -> ShowS
showStringWorker (Char
'\"':String
as) = String -> ShowS
showString String
"\\\"" forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showStringWorker String
as
showStringWorker (Char
'\\':String
as) = String -> ShowS
showString String
"\\\\" forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showStringWorker String
as
showStringWorker (Char
'\'':String
as) = String -> ShowS
showString String
"\\\'" forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showStringWorker String
as
showStringWorker (Char
x:String
as) = String -> ShowS
showString [Char
x] forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showStringWorker String
as
showStringWorker [] = String -> ShowS
showString String
""
intercalate :: String -> [ShowS] -> ShowS
intercalate :: String -> [ShowS] -> ShowS
intercalate String
sep = [ShowS] -> ShowS
go
where
go :: [ShowS] -> ShowS
go [] = forall a. a -> a
id
go [ShowS
x] = ShowS
x
go (ShowS
x:[ShowS]
xs) = ShowS
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString' String
sep forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ShowS] -> ShowS
go [ShowS]
xs