Module Serialk_json.Jsonq
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
Success and failure
val succeed : 'a -> 'a t
succeed v
is a query that succeeds with valuev
on any JSON value.
val fail : string -> 'a t
fail msg
is a query that fails on any JSON value with messagemsg
. Do not include position information inmsg
, this is automatically handled by the module.
Query combinators
val app : ('a -> 'b) t -> 'a t -> 'b t
app fq q
queries a s-expression first withfq
and then withq
and applies the result of latter to the former.
val pair : 'a t -> 'b t -> ('a * 'b) t
pair q0 q1
queries first withq0
and then withq1
and returns the pair of their 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.
Nulls
val is_null : bool t
is_null
tests for a JSON null value.
val null : unit t
null
queries JSON null as unit and fails otherwise.
Atomic values
val bool : bool t
bool
queries JSON bool values as abool
value and fails otherwise.
val float : float t
float
queries JSON number values as afloat
value and fails otherwise.
val int : int t
int
ismap truncate float
.
val string : string t
string
queries JSON string values as astring
value and fails otherwise.
val string_to : kind:string -> (string -> ('a, string) Stdlib.result) -> 'a t
string_to ~kind parse
queries a JSON string and parses it withp
. In case ofError m
errorfail
s withm
.kind
is the kind of value parsed, it is used for the error in case no JSON string is found.
val enum : kind:string -> Stdlib.Set.Make(Stdlib.String).t -> string t
enum ~kind ss
queries a JSON string for one of the elements ofss
and fails otherwise.kind
is for the kind of elements inss
, it used for error reporting.
val enum_map : kind:string -> 'a Stdlib.Map.Make(Stdlib.String).t -> 'a t
enum_map ~kind sm
queries a string for it's map insm
and fails if the string is not bound insm
.kind
is for the kind elements insm
, it is used for error reporting.
Arrays
These queries only succeed on JSON array values.
val is_empty_array : bool t
is_empty_array
queries an array for emptyness.
val fold_array : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b t
fold_array f q acc
queries the elements of an array from left to right withq
and folds the result withf
starting withacc
.
Array index queries
Objects
These queries only succeed on JSON object values.
val mem : string -> 'a t -> 'a t
mem n q
queries the membern
of a JSON object withq
. The query fails ifn
is unbound in the object.
val opt_mem : string -> 'a t -> absent:'a -> 'a t
opt_mem n q ~absent
queries the membern
of a JSON object withq
. absent is returned ifn
is unbound in the object.
val mem_dom : validate:Stdlib.Set.Make(Stdlib.String).t option -> Stdlib.Set.Make(Stdlib.String).t t
mem_dom ~validate
queries the member domain of a JSON object. Ifvalidate
isSome dom
, the query fails if a member name is not indom
.