package b0
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=e9aa779e66c08fc763019f16d4706f465d16c05d6400b58fbd0313317ef33ddea51952e2b058db28e65f7ddb7012f328c8bf02d8f1da17bb543348541a2587f0
doc/b0.std/B0_json/Jsonq/index.html
Module B0_json.Jsonq
Source
JSON value queries.
TODO maybe we could expose a bit more options for error reporting. In particular the internal path
type and a combinator in the vein of loc
to report back the path trace. Basically see Serialk_sexp
.
Queries
The type JSON value queries. A query either fails or succeeds against a JSON value returning a value of type 'a
.
query q j
is Ok v
if que query q
succeeds on s
and a (multiline) Error e
otherwise.
Success and failure
fail msg
is a query that fails on any JSON value with message msg
. Do not include position information in msg
, this is automatically handled by the module.
failf fmt ...
is like fail
but formats the message according to fmt
.
Query combinators
app fq q
queries a s-expression first with fq
and then with q
and applies the result of latter to the former.
pair q0 q1
queries first with q0
and then with q1
and returns the pair of their result.
bind q f
queries a s-expression with q
, applies the result to f
and re-queries the s-expression with the result.
JSON queries
val fold :
null:'a t ->
bool:'a t ->
float:'a t ->
string:'a t ->
array:'a t ->
obj:'a t ->
'a t
fold
queries JSON values according to their kind using the provided queries.
val partial_fold :
?null:'a t ->
?bool:'a t ->
?float:'a t ->
?string:'a t ->
?array:'a t ->
?obj:'a t ->
unit ->
'a t
partial_fold
is like fold
but only queries the kinds that are explicitely specified. It errors on other kinds.
with_loc q
queries with q
and returns the result with the location of the queried JSON value.
Nulls
nullable q
is None on JSON null and otherwise queries the value with q
.
Atomic values
string_to ~kind parse
queries a JSON string and parses it with p
. In case of Error m
error fail
s with m
. kind
is the kind of value parsed, it is used for the error in case no JSON string is found.
enum ~kind ss
queries a JSON string for one of the elements of ss
and fails otherwise. kind
is for the kind of elements in ss
, it used for error reporting.
enum_map ~kind sm
queries a string for it's map in sm
and fails if the string is not bound in sm
. kind
is for the kind elements in sm
, it is used for error reporting.
Arrays
These queries only succeed on JSON array values.
hd q
queries the first element of an array with q
. Fails on empty arrays.
fold_array f q acc
queries the elements of an array from left to right with q
and folds the result with f
starting with acc
.
Array index queries
nth ?absent n q
queries the n
th element of an array with q
. If n
is negative counts from the end of the array, so -1
is the last array element. If the element does not exist this fails if absent
is None
and succeeds with v
if absent
is Some v
.
Objects
These queries only succeed on JSON object values.
mem n q
queries the member n
of a JSON object with q
. The query fails if n
is unbound in the object.
opt_mem n q ~absent
queries the member n
of a JSON object with q
. absent is returned if n
is unbound in the object.
mem_dom ~validate
queries the member domain of a JSON object. If validate
is Some dom
, the query fails if a member name is not in dom
.