Module B0_json.Json

Generic JSON values.

JSON text

type meta = B0_text.Textloc.t

The type for node metadata.

val meta_none : meta

meta_none is an invalid input location.

type 'a node = 'a * meta

Abstract syntax tree nodes.

type name = string node

The type for generic JSON object names.

type mem = name * t

The type for generic JSON object members.

and object' = mem list

The type for generic JSON objects.

and t =
  1. | Null of unit node
  2. | Bool of bool node
  3. | Number of float node
    (*

    Encoders must use Null if float is not finite

    *)
  4. | String of string node
  5. | Array of t list node
  6. | Object of object' node
    (*

    The type for generic JSON text representations.

    *)
val meta : t -> meta

meta j is j's node meta data.

val equal : t -> t -> bool

equal j0 j1 is compare j0 j1 = 0.

val compare : t -> t -> int

compare j0 j1 is a total order on JSON values:

  • Floating point values are compared with Float.compare, this means NaN values are equal.
  • Strings are compared byte wise.
  • Objects members are sorted before being compared.
  • meta values are ignored.
val normalize : t -> t

normalize j normalizes JSON j by sorting object's members by name using String.compare.

Constructors

type 'a cons = ?meta:meta -> 'a -> t

The type for constructing JSON values from an OCaml value of type 'a. meta default to meta_none.

Nulls and options

val null : unit cons

null is Null (unit, meta).

val option : 'a cons -> 'a option cons

null is Null (unit, meta).

Booleans

val bool : bool cons

bool b is Bool (b, meta).

Numbers

val number : float cons

number n is Number (b, meta).

val any_float : float cons

any_float v is number v if Float.is_finit v is true and string Float.to_string v otherwise.

Strings

val string : string cons

string s is `String (s, meta).

Arrays

val list : t list cons

list vs is Array (vs, meta).

val array : t array cons

array a is Array (Array.to_list a, meta).

Objects

val name : ?meta:meta -> string -> name

name ?meta n is n, meta. meta defaults to meta_none.

val mem : name -> t -> mem

mem n v is (name, v).

val object' : mem list cons

object' mems is Object (mems, meta).

Codec

val of_string : ?file:B0_text.Textloc.filepath -> string -> (t, string) Stdlib.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.

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.

Formatters

type number_format = (float -> unit, Stdlib.Format.formatter, unit) Stdlib.format

The type for JSON number formatters.

val default_number_format : number_format

default_number_format is "%.17g". This number formats ensures that finite floating point values can be interchanged without loss of precision.

val pp' : ?number_format:number_format -> unit -> Stdlib.Format.formatter -> t -> unit

pp' ~format ~number_format () ppf j formats j on ppf. The output is indented but may be more compact than an Indent JSON encoder may do. For example arrays may be output on one line if they fit etc.

  • number_format is used to format JSON numbers. Defaults to default_number_format
  • Non-finite numbers are output as JSON nulls (explanation).
  • Strings are assumed to be valid UTF-8.
val pp : Stdlib.Format.formatter -> t -> unit

pp formats JSON, see pp'.