Module Basic.Util
exception
Type_error of string * t
Raised when the JSON value is not of the correct type to support an operation, e.g.
member
on an`Int
. The string message explains the mismatch.
exception
Undefined of string * t
Raised when the equivalent JavaScript operation on the JSON value would return undefined. Currently this only happens when an array index is out of bounds.
val (|>) : 'a -> ('a -> 'b) -> 'b
- deprecated
Forward pipe operator; useful for composing JSON access functions without too many parentheses
val keys : t -> string list
Returns all the key names in the given JSON object
val member : string -> t -> t
member k obj
returns the value associated with the keyk
in the JSON objectobj
, or`Null
ifk
is not present inobj
.
val index : int -> t -> t
index i arr
returns the value at indexi
in the JSON arrayarr
. Negative indices count from the end of the list (so -1 is the last element).
val map : (t -> t) -> t -> t
map f arr
calls the functionf
on each element of the JSON arrayarr
, and returns a JSON array containing the results.
val to_option : (t -> 'a) -> t -> 'a option
Return
None
if the JSON value is null or map the JSON value toSome
value using the provided function.
val to_bool : t -> bool
Extract a boolean value or raise
Type_error
.
val to_bool_option : t -> bool option
Extract
Some
boolean value, returnNone
if the value is null, or raiseType_error
otherwise.
val to_number : t -> float
Extract a number or raise
Type_error
.
val to_number_option : t -> float option
Extract
Some
number, returnNone
if the value is null, or raiseType_error
otherwise.
val to_float : t -> float
Extract a float value or raise
Type_error
.to_number
is generally preferred as it also works with int literals.
val to_float_option : t -> float option
Extract
Some
float value, returnNone
if the value is null, or raiseType_error
otherwise.to_number_option
is generally preferred as it also works with int literals.
val to_int : t -> int
Extract an int from a JSON int or raise
Type_error
.
val to_int_option : t -> int option
Extract
Some
int from a JSON int, returnNone
if the value is null, or raiseType_error
otherwise.
val to_string : t -> string
Extract a string from a JSON string or raise
Type_error
.
val to_string_option : t -> string option
Extract
Some
string from a JSON string, returnNone
if the value is null, or raiseType_error
otherwise.
val convert_each : (t -> 'a) -> t -> 'a list
The conversion functions above cannot be used with
map
, because they do not return JSON values. This convenience functionconvert_each to_f arr
is equivalent toList.map to_f (to_list arr)
.
Exception-free filters
val filter_map : ('a -> 'b option) -> 'a list -> 'b list
filter_map f l
maps each element of the listl
to an optional value using functionf
and unwraps the resulting values.
val flatten : t list -> t list
Expects JSON arrays and returns all their elements as a single list.
flatten l
is equivalent toList.flatten (filter_list l)
.
val filter_index : int -> t list -> t list
Expects JSON arrays and returns all their elements existing at the given position.
val filter_member : string -> t list -> t list
Expects JSON objects and returns all the fields of the given name (at most one field per object).
val filter_bool : t list -> bool list
Expects JSON booleans and unwraps them.
val filter_int : t list -> int list
Expects JSON integers (
`Int
nodes) and unwraps them.
val filter_float : t list -> float list
Expects JSON floats (
`Float
nodes) and unwraps them.
val filter_number : t list -> float list
Expects JSON numbers (
`Int
or`Float
) and unwraps them. Ints are converted to floats.
val filter_string : t list -> string list
Expects JSON strings and unwraps them.