This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONOutput.hs
More file actions
64 lines (57 loc) · 2.14 KB
/
JSONOutput.hs
File metadata and controls
64 lines (57 loc) · 2.14 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module JSONOutput where
import JSON
import Data.List (intersperse, intercalate)
-- | Converts a 'String' into its string JSON representation by
-- placing quotes around it and escaping any quote characters that
-- appear in the string.
--
-- For example,
--
-- > renderString "hello \"world\""
--
-- Returns
--
-- > "\"hello \\\"world\\\"\""
renderString :: String -> String
renderString xs = "\"" ++ escapeString xs ++ "\""
escapeString :: String -> String
escapeString [] = []
escapeString ('"':xs) = "\\\"" ++ escapeString xs
escapeString ('\\':xs) = "\\\\" ++ escapeString xs
escapeString ('/':xs) = "\\/" ++ escapeString xs
escapeString ('\b':xs) = "\\b" ++ escapeString xs
escapeString ('\f':xs) = "\\f" ++ escapeString xs
escapeString ('\n':xs) = "\\n" ++ escapeString xs
escapeString ('\r':xs) = "\\r" ++ escapeString xs
escapeString ('\t':xs) = "\\t" ++ escapeString xs
-- hex chars?
escapeString (c:xs) = c : escapeString xs
-- HINT: one way of doing the escaping is the same way that we did the
-- htmlEscape function in the Week01 problems.
-- | Converts `JSON` into its string representation, as documented in
-- the JSON standard (https://www.json.org/json-en.html). The format
-- should be as is understood by the parsing functions in `JSONInput`.
--
-- For example,
--
-- > renderJSON (Object [("a",Number 1), ("b",Number 2)])
--
-- should give
--
-- > "{\"a\": 1, \"b\": 2}"
renderJSON :: JSON -> String
renderJSON (String s) = renderString s
renderJSON (Boolean b) = if b then "true" else "false"
renderJSON Null = "null"
renderJSON (Number n) = show n
renderJSON (Array a) = "[" ++ intercalate ", " (map renderJSON a) ++ "]"
renderJSON (Object o) = "{" ++ intercalate ", " (parseJSONObject o) ++ "}"
parseJSONObject :: [(String, JSON)] -> [String]
parseJSONObject [] = []
parseJSONObject ((s, j):xs) = (renderString s ++ ": " ++ renderJSON j) : parseJSONObject xs
-- HINT: the `intersperse` function (imported above) is a good way of
-- putting something between every element of a list. It is the
-- "printing" counterpart of the 'sepBy' parser combinator.
--
-- You should use 'renderString' to implement rendering of 'String's
-- in the JSON.