Newer
Older
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- |
-- Module: JSONOutput
-- Description: Conversion of JSON values to their string representations.
module JSONOutput where
import JSON
import Data.List (intersperse)
-- | Returns the JSON escaped version of a character.
--
-- Escaping is used to mark characters that would otherwise be
-- interpreted as control codes. Strings in JSON are begun and ended
-- by double-quotes, so if we want to put a double-quote in a string
-- then we need to mark it as special. JSON (and Haskell and Java and
-- ...) do this by putting a backslash in front of it. This means that
-- backslashes are also treated as special, so if we want to have a
-- backslash in a string, then we need to represent it with two
-- backslashes.
--
-- JSON also escapes common control characters such as newline, tabs,
-- carriage return, etc., and optionally non-ASCII unicode
-- characters. This function also escapes these. See the JSON
-- standards documentation for information on what characters to
-- escape.
--
-- Any character that is not escaped is return as-is in a one
-- character string.
escapeChar :: Char -> String
escapeChar = error "UNIMPLEMENTED: escapeChar"
-- | Convert every character in a String to its escaped
-- representation and concatenate them all together.
--
-- For example:
-- >>> escapeString "Hello \"world\""
-- "Hello \\\"world\\\""
-- (note that both JSON and Haskell escaping are happening here!)
escapeString :: String -> String
escapeString = error "UNIMPLEMENTED: escapeString"
-- | Quote a string by placing double-quotation marks before and after.
quote :: String -> String
quote = error "UNIMPLEMENTED: quote"
-- | Render a string as its JSON representation by escaping every
-- character and placing double-quotes around it.
--
-- For example,
-- >>> renderString "Hello \"world\""
-- "\"Hello \\\"world\\\"\""
renderString :: String -> String
renderString = error "UNIMPLEMENTED: renderString"
-- | Put square brackets [ ] around a string.
sqbracket :: String -> String
sqbracket = error "UNIMPLEMENTED: sqbracket"
-- | Put curly brackets { } around a string.
curlybracket :: String -> String
curlybracket = error "UNIMPLEMENTED: curlybracket"
-- | Concatenate a list of items with a separator between each one.
concatWith :: Monoid m => m -> [m] -> m
concatWith = error "UNIMPLEMENTED: concatWith"
-- | Render a JSON-style object field as @"field name": <value>@.
--
-- For example,
-- >>> renderField ("myNull", JsonNull)
-- "\"myNull\":null"
renderField :: (String, JSON) -> String
renderField = error "UNIMPLEMENTED: renderField"
-- | Converts a JSON value into its string representation.
--
--
renderJSON :: JSON -> String
renderJSON (JsonString s) = renderString s
renderJSON (JsonInteger i) = show i
renderJSON (JsonBoolean True) = "true"
renderJSON (JsonBoolean False) = "false"
renderJSON JsonNull = "null"
renderJSON (JsonArray jsons) =
error "UNIMPLEMENTED: renderJSON for arrays"
renderJSON (JsonObject fields) =
error "UNIMPLEMENTED: renderJSON for objects"