Skip to content
Snippets Groups Projects
JSONOutput.hs 2.93 KiB
Newer Older
Robert Atkey's avatar
Robert Atkey committed
-- |
-- 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"