package tezt
Install
Dune Dependency
Authors
Maintainers
Sources
md5=88c2d9d3da75ff554599bc34cbf5acbe
sha512=e60294514ecc4a989ce663ebb306e56f654dcfaffb7dbe5e3f05f5a13c9c2ff64dadde4a77b0d9a8567a76a6a7a2b25e0940ccd2a956ffcb85ff9300bfebe3bc
doc/tezt.json/JSON/index.html
Module JSON
Source
JSON decoding.
Errors that can happen when parsing or reading JSON.
JSON unannotated ASTs.
Raise Error
.
The JSON.t
argument is used to annotate the error message with the location of the JSON value.
Encoding
Decoding
Same as parse
, but return None
instead of raising Error
.
The general pattern for the accessors below is that only as_x
functions can fail. Getters get
and geti
return `Null
instead of failing. This allows to chain them and only test for errors at the end with as_x
, either by raising Error
or by returning None
(with the as_x_opt
variant).
Internally, the actual error which is printed is the correct one. For instance, with json |-> "x" |> as_int
, if json
is not an object, the call to as_int
causes the test to fail with "<origin>: not an object"
where <origin>
is the ~origin
you gave to parse
. If json
is an object but "x"
does not exist, as_int
causes the test to fail with "<origin>: missing field: x"
. If "x"
exists but is not an integer, as_int
causes the test to fail with "<origin> at .x: expected an integer"
.
Get the value for a field of a JSON object.
If the JSON value is not an object, or if the field does not exist, return `Null
.
Get the value for a field of a JSON array.
If the JSON value is not an array, or if the field does not exist, return `Null
.
Updates an object with a (key, value)
pair.
put (key, value) obj
puts value
under key
in obj
. If the key
already exists, it is overwritten. Otherwise a new key is added at the end of the object.
Alters the value of a specific key in a JSON object by applying its value to a function. Returns updated object.
update key f obj
is equivalent to put (key, f (get key obj)) obj
.
Note: if key
is not present in obj
, `Null
is passed to f
instead.
filter_map_object obj f
maps f
over each field in obj
.
If f key value
is None
then the key
is removed from obj
. If f key value
is Some value'
then the key
is overwritten with value'
.
filter_object obj f
filters the bindings in obj
.
filter_object obj f
removes each binding key, value
in obj
for which f key value
is false
.
Non-recursively merges two objects.
merge_objects o1 o2
returns an object containing all fields of o1
and o2
. If a key exists in both o1
and o2
, then it will be bound to its value in o2
.
Return None
if a JSON value is `Null
, Some
otherwise.
Example: JSON.(json |> as_opt |> Option.map read_record)
Get the integer value from a `Float
or `String
node (64-bit version).
Same as as_int64
, but return None
instead of raising Error
.
Get the integer value from a `Float
or `String
node (32-bit version).
Same as as_int32
, but return None
instead of raising Error
.
Same as as_float
, but return None
instead of raising Error
.
Same as as_string
, but return None
instead of raising Error
.
Same as as_list
, but return None
instead of raising Error
.
Same as as_object
, but return None
instead of raising Error
.
Equality for JSON unannotated ASTs.
Objects are equal only when they have the exact same number of fields with the same contents (their order does not matter). Consequently, e.g. {"a": 1}
is not equal to {"a": 1, "a": 1}
.
Arrays are equal when they contain the same number of pair-wise equal elements.