Serialkit_json.Json
JSON text definitions and codec.
type loc = Serialkit_text.Tloc.t
The type for text locations.
val loc_nil : loc
loc_nil
is an invalid input location.
and t = [
|
`Null of loc
|
`Bool of bool * loc
|
`Float of float * loc
|
`String of string * loc
|
`A of t list * loc
|
`O of mem list * loc
]
The type for generic JSON text representations.
val null : t
null
is `Null loc_nil
.
val bool : bool -> t
bool b
is `Bool (b, loc_nil)
.
val float : float -> t
float b
is `Float (f, loc_nil)
.
val string : string -> t
string s
is `String (s, loc_nil)
.
val to_null : t -> (unit, string) Stdlib.result
to_null j
extracts a null from j
. If j
is not a null an error with the location formatted according to Tloc
.pp is returned.
val to_bool : t -> (bool, string) Stdlib.result
to_bool j
extracts a bool from j
. If j
is not a bool an error with the location formatted according to Tloc
.pp is returned.
val to_float : t -> (float, string) Stdlib.result
to_float j
extracts a float from j
. If j
is not a float an error with the location formatted according to Tloc
.pp is returned.
to_array j
extracts a array from j
. If j
is not a array an error with the location formatted according to Tloc
.pp is returned.
to_obj j
extracts a array from j
. If j
is not a array an error with the location formatted according to Tloc
.pp is returned.
val get_float : t -> float
get_float j
is like to_float
but raises Invalid_argument
if j
is not a float.
val get_string : t -> string
get_string j
is like to_string
but raises Invalid_argument
if j
is not a string.
get_array j
is like to_array
but raises Invalid_argument
if j
is not a array.
get_obj j
is like to_obj
but raises Invalid_argument
if j
is not a array.
val pp : Stdlib.Format.formatter -> t -> unit
pp
formats JSON text.
Warning. Assumes all OCaml strings in the formatted value are UTF-8 encoded.
val of_string :
?file:Serialkit_text.Tloc.fpath ->
string ->
(t, string) Stdlib.result
of_string s
parses JSON text from s
according to RFC8259 with the following limitations:
string_of_float
which is not compliant.to_string
).Note. All OCaml strings returned by this function are UTF-8 encoded.