package b0

  1. Overview
  2. Docs

JSON text

The type for text locations.

val loc_nil : loc

loc_nil is an invalid input location.

type mem = (string * loc) * t

The type for JSON object members.

and t = [
  1. | `Null of loc
  2. | `Bool of bool * loc
  3. | `Float of float * loc
  4. | `String of string * loc
  5. | `A of t list * loc
  6. | `O of mem list * loc
]

The type for generic JSON text representations.

val loc : t -> loc

loc j is j's input location.

Constructors

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 array : t list -> t

a vs is `A (vs, loc_nil).

val mem : string -> t -> mem

mem n v is ((n, loc_nil), v).

val obj : mem list -> t

obj mems is `O (mems, loc_nil).

Accessors

val to_null : t -> (unit, string) 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) 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) 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.

val to_array : t -> (t list, string) result

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.

val to_obj : t -> (mem list, string) result

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_null : t -> unit

get_null j is like to_null but raises Invalid_argument if j is not a null.

val get_bool : t -> bool

get_bool j is like to_bool but raises Invalid_argument if j is not a bool.

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.

val get_array : t -> t list

get_array j is like to_array but raises Invalid_argument if j is not a array.

val get_obj : t -> mem list

get_obj j is like to_obj but raises Invalid_argument if j is not a array.

Formatters

val pp : Format.formatter -> t -> unit

pp formats JSON text.

Warning. Assumes all OCaml strings in the formatted value are UTF-8 encoded.

Codec

val of_string : ?file:B00_serialk_text.Tloc.fpath -> string -> (t, string) result

of_string s parses JSON text from s according to RFC8259 with the following limitations:

  • Numbers are parsed with string_of_float which is not compliant.
  • TODO Unicode escapes are left unparsed (this will not round trip with to_string).

Note. All OCaml strings returned by this function are UTF-8 encoded.

val to_string : t -> string

to_string j is j as JSON text, encoded according to RFC8259.

Warning. Assumes all OCaml strings in j are UTF-8 encoded.

OCaml

Innovation. Community. Security.