Module Process.Yojson_safe
Yojson_safe
supports processing JSON data that conforms to Yojson's Safe json type.
type json
=[
|
`Null
|
`Bool of bool
|
`Int of int
|
`Intlit of string
|
`Float of float
|
`String of string
|
`Assoc of (string * json) list
|
`List of json list
|
`Tuple of json list
|
`Variant of string * json option
]
include Jsonxt__.Process_intf.Shared with type json := json
val member : string -> [> `Assoc of (string * json) list ] -> json
member key json
searches the JSON objectjson
, which must be an`Assoc
element, forkey
and returns the value or`Null
if thekey
is missing
val index : int -> [> `List of json list ] -> json
index idx json
returns theidx
-th JSON object in thejson
array, which must be an`List
element. A negativeidx
value starts from the end with -1 being the last element. AFailure
exception is raise if the idx is out of bounds
val map : (json -> json) -> [> `List of json list ] -> [> `List of json list ]
map f json
applies the functionf
to each element of the JSON arrayjson
, which must be an`List
element, and returns a`List
element
val to_assoc : [> `Assoc of (string * json) list ] -> (string * json) list
to_assoc json
converts the JSON object`Assoc a
toa
val to_bool_option : [> `Bool of bool | `Null ] -> bool option
to_bool_option json
converts`Bool b
toSome b
and`Null
toNone
val to_float_option : [> `Float of float | `Null ] -> float option
to_float_option json
converts`Float f
toSome f
and`Null
toNone
val to_option : ([> `Null ] as 'a -> json) -> 'a -> json option
to_option f json
returnsNone
ifjson
is`Null
otherwiseSome (f json)
.
val to_number_option : [> `Float of float | `Null ] -> float option
to_number_option json
converts`Float f
toSome f
and`Null
toNone
val to_string_option : [> `String of string | `Null ] -> string option
to_string_option json
converts`String s
toSome s
and`Null
toNone
val convert_each : (json -> json) -> [> `List of json list ] -> json list
convert_each f json
applies the functionf
to each element of the JSON arrayjson
, which must be an`List
element, and returns a list of the returned values.
val filter_map : ('a -> 'a option) -> 'a list -> 'a list
filter_map f l
appliesf
to each element of the listl
and returns a new list with the valuesv
for whichf
returnedSome v
.
val rev_filter_map : ('a -> 'a option) -> 'a list -> 'a list -> 'a list
rev_filter_map f acc l
appliesf
to each element of the listl
and prepends the values for whichf
returnedSome v
to listacc
.acc
is returned as the result and is in reverse order to the input. This is a tail call optimised version offilter_map
val flatten : [> `List of 'a list ] list -> 'a list
flatten l
given a list ofjson
elements filters the`List
elements and flattens them into a single list. This is the same asfilter_list |> List.flatten
val rev_flatten : 'a list -> [> `List of 'a list ] list -> 'a list
rev_flatten acc l
is the tail recursive version offlatten
with the result accumulated inacc
. The result is in reverse order.
val filter_index : int -> [> `List of json list ] list -> json list
filter_index i l
returns thei
'th element from each`List l1
inl
. Thus,[[`List [`Int 2; `Int 3]; `List [`Int 4; `Int 5]] |> filter_index 1]
returns
[`Int 3; `Int 5]
val filter_list : [> `List of 'a ] list -> 'a list
filter_list l
returns a list of all the values of`List value
elements in l
val filter_assoc : [> `Assoc of 'a ] list -> 'a list
filter_assoc l
returns a list of all the values of`Assoc value
elements in l
val filter_bool : [> `Bool of bool ] list -> bool list
filter_bool l
returns a list of all the values of`Bool value
elements in l
val filter_float : [> `Float of float ] list -> float list
filter_float l
returns a list of all the values of`Float value
elements in l
val filter_string : [> `String of string ] list -> string list
filter_string l
returns a list of all the values of`String value
elements in l
val filter_member : string -> [> `Assoc of (string * json) list ] list -> json list
filter_member key js
given akey
and a list of json`Assoc
-s,js
, returns the list of values extracted from each of the`Assoc
-s. Thus,[[`Assoc [("id", `Int 1)]; `Assoc [("id", `Int 2)]]] |> filter_member "id"]
returns
[`Int 1; `Int 2]
val filter_number : [> `Float of float ] list -> float list
filter_number l
returns a list of all the values of`Float value
elements in l
val keys : [> `Assoc of (string * 'a) list ] -> string list
keys assoc
returns all the keys from the`Assoc
element
val values : [> `Assoc of (string * 'a) list ] -> 'a list
values assoc
returns all the values from the`Assoc
element
include Jsonxt__.Process_intf.Basic with type json := json
val to_number : [> `Int of int | `Float of float ] -> float
to_number json
converts`Float f
tof
andInt i
tofloat i
val to_number_option : [> `Int of int | `Float of float | `Null ] -> float option
to_number_option json
converts`Float f
toSome f
,`Int i
toSome (float i)
and`Null
toNone
val to_int_option : [> `Int of int | `Null ] -> int option
to_int_option json
converts`Int i
toSome i
and`Null
toNone
include Jsonxt__.Process_intf.Extended with type json := json
val sort : [> `Assoc of (string * 'a) list | `List of 'a list | `Tuple of 'a list | `Variant of 'b * 'a option ] as 'a -> 'a
sort json
sorts thejson
tree based on field names. Objects and lists are sorted recursively. Note that the function only sorts field names and not the values. The sort is stable