JSON
module JSON
type Error
Why a document could not be parsed. Every variant carries the position in the input where the parser stopped, so a caller can point at the problem.
JSON.parse("[1, 2") # => Error(UnexpectedEnd(5))
JSON.parse("// c\n1") # => Error(UnexpectedCharacter("/", 0))
Variants
UnexpectedCharacter(String, Integer)UnexpectedEnd(Integer)InvalidLiteral(String, Integer)InvalidNumber(String, Integer)InvalidEscape(String, Integer)InvalidUnicodeEscape(String, Integer)UnterminatedComment(Integer)TrailingInput(Integer)UnknownOption(Atom)
function parse
Parses a JSON document, strictly.
The whole text must be one JSON value with nothing after it: trailing input is TrailingInput, not a silently ignored tail. Objects come back as maps with atom keys, arrays as lists, null as None.
parse(text) : String -> Result<Any, Error>
parse(text) : String -> {Atom: Bool} -> Result<Any, Error>
Parameters
textString- the JSON document
Returns: Result<Any, Error> — the parsed value, or why it failed
Examples
JSON.parse("{\"a\": 1, \"b\": [true, null, 2.5]}")
# => Ok({ a: 1, b: [true, None, 2.5] })
JSON.parse("[1, 2") # => Error(UnexpectedEnd(5))
Reading a config file
match JSON.parse(FS.File.read("config.json").or("")) do
Ok(config) => IO.printLine(config)
Error(e) => IO.printError("config.json is not valid JSON: ${e}")
end
function stringify
Renders a Kex value as strict JSON text.
Maps become objects, lists become arrays, None becomes null, and strings are escaped. A map written with atom keys (the usual Kex spelling) renders with those names as strings, so { name: "Ada" } becomes {"name":"Ada"}. Object keys come out in canonical key order.
Anything the encoder does not recognise renders as null rather than failing, so this never raises.
stringify(value) : Any -> String
Parameters
valueAny- the value to render
Returns: String — the JSON text
Examples
JSON.stringify({ name: "Ada", n: 1, ok: true })
# => '{"n":1,"name":"Ada","ok":true}'
JSON.stringify([1, 2, 3])
# => '[1,2,3]'
Writing a JSON file
FS.File.write("out.json", JSON.stringify(report))
A round trip
JSON.parse(JSON.stringify({ a: 1 })) # => Ok({ a: 1 })