JSON
is a JSON data type, parser, and serializer for Carp.
A sum type with six variants: Null, Bool, Num, Str, Arr, and Obj.
Arrays and objects hold Boxed values because the type is recursive.
=
(Fn [(Ref JSON a), (Ref JSON a)] Bool)
(= a b)
compares two JSON values structurally: arrays by length and order,
objects by key set independent of member order. Num holds a Double, so 1
and 1.0 are one value, which is how RFC 6902 s4.6 compares numbers.
ParseError
Module
wraps a ParseErrorKind together with the byte position
in the input where the error was detected.
ParseErrorKind
Module
describes the cause of a parse failure.
The associated ParseError carries this together with the byte position
where the error was detected.
Patch
Module
implements RFC 6902 JSON Patch, a JSON array of operation objects describing a sequence of changes to a document.
PatchError
Module
wraps a PatchErrorKind together with the index of the
operation that failed, or -1 when the patch document itself is not an
array.
PatchErrorKind
Module
describes why a patch could not be applied.
The associated PatchError carries this together with the index of the
operation that failed.
Pointer
Module
implements RFC 6901 JSON Pointer, addressing a value inside a
JSON document with a string such as /foo/0/bar.
SerializeError
Module
describes the cause of a serialization failure.
NonFiniteNumber is emitted when a JSON.Num contains NaN or infinity,
neither of which is representable in JSON.
as-arr
(Fn [(Ref JSON a)] (Maybe (Array (Box JSON))))
(as-arr j)
extracts a copy of the array value, or Nothing.
as-bool
(Fn [(Ref JSON a)] (Maybe Bool))
(as-bool j)
extracts the boolean value, or Nothing if not a bool.
as-num
(Fn [(Ref JSON a)] (Maybe Double))
(as-num j)
extracts the numeric value, or Nothing if not a number.
as-obj
(Fn [(Ref JSON a)] (Maybe (Map String (Box JSON))))
(as-obj j)
extracts a copy of the object map, or Nothing.
as-str
(Fn [(Ref JSON a)] (Maybe String))
(as-str j)
extracts a copy of the string value, or Nothing.
delete-key
(Fn [JSON, (Ref String a)] JSON)
(delete-key j k)
removes a key from a JSON object. If the value is not an object or the key is absent, returns it unchanged.
entry
(Fn [a, b] (Pair a (Box b)))
(entry k v)
creates a key-value pair for use with obj. The value is
automatically boxed.
get
(Fn [(Ref JSON a), (Ref String a)] (Maybe JSON))
(get j k)
looks up a key in a JSON object. Returns Nothing if the value is
not an object or the key is absent. Returns a copy of the value.
get-in
(Fn [(Ref JSON a), (Ref (Array String) b)] (Maybe JSON))
(get-in j keys)
performs nested lookup through objects and arrays. String path elements that parse as non-negative integers index into arrays; all other elements are used as object keys. Returns a copy.
(JSON.get-in &j &[@"data" @"users" @"0" @"name"])
merge-diff
(Fn [(Ref JSON a), (Ref JSON a)] JSON)
(merge-diff a b)
builds the smallest RFC 7386 merge patch taking a to b,
emitting null for the members b drops and omitting the ones it leaves
alone. A merge patch cannot set a member to null, so a b that introduces
one does not survive the round trip through merge-patch.
(JSON.merge-patch @&a &(JSON.merge-diff &a &b))
merge-patch
(Fn [JSON, (Ref JSON a)] JSON)
(merge-patch target patch)
applies an RFC 7386 JSON Merge Patch to target. A patch
that is not an object replaces the target wholesale, and a target that is not
an object is treated as an empty one. In an object patch a null member
deletes that key, all others merge recursively, and arrays are replaced rather
than merged, so a patch reaches neither into an array nor a null value.
(JSON.merge-patch doc &patch)
nth
(Fn [(Ref JSON a), Int] (Maybe JSON))
(nth j i)
indexes into a JSON array. Returns Nothing if the value is not an
array or the index is out of bounds. Returns a copy of the value.
obj
(Fn [(Array (Pair String (Box JSON)))] JSON)
(obj entries)
builds a JSON object from an array of key-value pairs.
(JSON.obj [(JSON.entry @"a" (JSON.Num 1.0)) (JSON.entry @"b" (JSON.Bool true))])
parse
(Fn [(Ref String a)] (Result JSON JSON.ParseError))
(parse s)
parses a JSON value from a string.
Returns a (Result JSON ParseError) -- a Success with the parsed value,
or an Error carrying the failure kind and the byte position where it
was detected. Use JSON.parse-error-str to format the error for display.
parse-error-kind-str
(Fn [(Ref JSON.ParseErrorKind a)] String)
(parse-error-kind-str k)
formats a ParseErrorKind as a human-readable
message, without position information.
parse-error-str
(Fn [(Ref JSON.ParseError a)] String)
(parse-error-str e)
formats a ParseError as a human-readable message
including the byte position where the error was detected.
patch-error-kind-str
(Fn [(Ref JSON.PatchErrorKind a)] String)
(patch-error-kind-str k)
formats a PatchErrorKind as a human-readable
message, without the operation index.
patch-error-str
(Fn [(Ref JSON.PatchError a)] String)
(patch-error-str e)
formats a PatchError as a human-readable message
including the index of the operation that failed.
pretty-str
(Fn [(Ref JSON a), Int] (Result String JSON.SerializeError))
(pretty-str j indent)
serializes a JSON value to a human-readable indented string.
The indent parameter specifies the number of spaces per indentation level.
Returns a (Result String SerializeError). Errors are returned only when
a JSON.Num contains NaN or infinity.
push
(Fn [JSON, JSON] JSON)
(push j v)
appends a value to a JSON array. If the value is not an array, returns it unchanged.
serialize-error-str
(Fn [(Ref JSON.SerializeError a)] String)
(serialize-error-str e)
formats a SerializeError as a human-readable
message.
set-in
(Fn [JSON, (Ref (Array String) a), JSON] JSON)
(set-in j keys v)
sets a value at a nested path, creating missing intermediate object keys. Path elements that parse as non-negative integers index into arrays, all others are object keys.
(JSON.set-in j &[@"users" @"0" @"name"] (JSON.Str @"Alice"))
set-key
(Fn [JSON, (Ref String a), JSON] JSON)
(set-key j k v)
sets a key in a JSON object, replacing any existing value. If the value is not an object, returns it unchanged.
set-nth
(Fn [JSON, Int, JSON] JSON)
(set-nth j i v)
replaces the value at index i in a JSON array.
If the value is not an array or the index is out of bounds, returns it
unchanged.
str
(Fn [(Ref JSON a)] (Result String JSON.SerializeError))
(str j)
serializes a JSON value to a compact string with no extra
whitespace. Returns a (Result String SerializeError), which errors only on a
JSON.Num holding NaN or infinity, neither representable in JSON.
update-in
(Fn [JSON, (Ref (Array String) a), (Fn [JSON] JSON b)] JSON)
(update-in j keys f)
applies a function to the value at a nested path, indexing
arrays as set-in does. Returns the input unchanged if the path does not
resolve to a value.
(JSON.update-in j &[@"count"] (fn [n] (JSON.Num 1.0)))