Module Basic.Util
exceptionType_error of string * tRaised when the JSON value is not of the correct type to support an operation, e.g.
memberon an`Int. The string message explains the mismatch.
exceptionUndefined of string * tRaised 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 listReturns all the key names in the given JSON object
val member : string -> t -> tmember k objreturns the value associated with the keykin the JSON objectobj, or`Nullifkis not present inobj.
val index : int -> t -> tindex i arrreturns the value at indexiin the JSON arrayarr. Negative indices count from the end of the list (so -1 is the last element).
val map : (t -> t) -> t -> tmap f arrcalls the functionfon each element of the JSON arrayarr, and returns a JSON array containing the results.
val to_option : (t -> 'a) -> t -> 'a optionReturn
Noneif the JSON value is null or map the JSON value toSomevalue using the provided function.
val to_bool : t -> boolExtract a boolean value or raise
Type_error.
val to_bool_option : t -> bool optionExtract
Someboolean value, returnNoneif the value is null, or raiseType_errorotherwise.
val to_number : t -> floatExtract a number or raise
Type_error.
val to_number_option : t -> float optionExtract
Somenumber, returnNoneif the value is null, or raiseType_errorotherwise.
val to_float : t -> floatExtract a float value or raise
Type_error.to_numberis generally preferred as it also works with int literals.
val to_float_option : t -> float optionExtract
Somefloat value, returnNoneif the value is null, or raiseType_errorotherwise.to_number_optionis generally preferred as it also works with int literals.
val to_int : t -> intExtract an int from a JSON int or raise
Type_error.
val to_int_option : t -> int optionExtract
Someint from a JSON int, returnNoneif the value is null, or raiseType_errorotherwise.
val to_string : t -> stringExtract a string from a JSON string or raise
Type_error.
val to_string_option : t -> string optionExtract
Somestring from a JSON string, returnNoneif the value is null, or raiseType_errorotherwise.
val convert_each : (t -> 'a) -> t -> 'a listThe conversion functions above cannot be used with
map, because they do not return JSON values. This convenience functionconvert_each to_f arris equivalent toList.map to_f (to_list arr).
Exception-free filters
val filter_map : ('a -> 'b option) -> 'a list -> 'b listfilter_map f lmaps each element of the listlto an optional value using functionfand unwraps the resulting values.
val flatten : t list -> t listExpects JSON arrays and returns all their elements as a single list.
flatten lis equivalent toList.flatten (filter_list l).
val filter_index : int -> t list -> t listExpects JSON arrays and returns all their elements existing at the given position.
val filter_member : string -> t list -> t listExpects JSON objects and returns all the fields of the given name (at most one field per object).
val filter_bool : t list -> bool listExpects JSON booleans and unwraps them.
val filter_int : t list -> int listExpects JSON integers (
`Intnodes) and unwraps them.
val filter_float : t list -> float listExpects JSON floats (
`Floatnodes) and unwraps them.
val filter_number : t list -> float listExpects JSON numbers (
`Intor`Float) and unwraps them. Ints are converted to floats.
val filter_string : t list -> string listExpects JSON strings and unwraps them.