1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
module JSON.Pretty (ppJSON, ppJValue) where
import JSON.Data
import Text.Pretty
ppJSON :: JValue -> String
ppJSON j = pPrint (ppJValue j)
ppJValue :: JValue -> Doc
ppJValue JTrue = text "true"
ppJValue JFalse = text "false"
ppJValue JNull = text "null"
ppJValue (JNumber f) = float f
ppJValue (JArray vs) = ppJArray vs
ppJValue (JObject ps) = ppJObject ps
ppJValue (JString s) = text $ '"' : concatMap ppJChar s ++ "\""
where
ppJChar c | c == '"' = "\\\""
| c == '\\' = "\\\\"
| c == '\b' = "\\b"
| c == '\f' = "\\f"
| c == '\n' = "\\n"
| c == '\r' = "\\r"
| c == '\t' = "\\t"
| otherwise = [c]
ppJArray :: [JValue] -> Doc
ppJArray vs = listSpaced $ map ppJValue vs
ppJObject :: [(String, JValue)] -> Doc
ppJObject ps =
(nest 2 $ lbrace $$ vsep (punctuate comma $ map ppKVP ps)) $$ rbrace
where ppKVP (k, v) = (text $ show k) <> colon <+> ppJValue v
|