B0_json.JsonqSourceJSON 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.
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.
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.
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.
val fold :
null:'a t ->
bool:'a t ->
float:'a t ->
string:'a t ->
array:'a t ->
obj:'a t ->
'a tfold 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 tpartial_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.
nullable q is None on JSON null and otherwise queries the value with q.
string_to ~kind parse queries a JSON string and parses it with p. In case of Error m error fails 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.
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.
nth ?absent n q queries the nth 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.
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.