package jsont

  1. Overview
  2. Docs

Generic JSON values.

JSON values

type 'a jsont := 'a t
type 'a cons = ?meta:Meta.t -> 'a -> json

The type for constructing JSON values from an OCaml value of type 'a. meta defaults to Meta.none.

type t = json
val meta : json -> Meta.t

meta v is the metadata of value v.

val set_meta : Meta.t -> json -> json

set_meta m v replaces v's meta with m.

val copy_layout : json -> dst:json -> json

copy_layout src ~dst copies the layout of src and sets it on dst using Meta.copy_ws.

val sort : json -> Sort.t

sort v is the sort of value v.

val zero : json cons

zero j is a stub value of the sort value of j. The stub value is the “natural” zero: null, false, 0, empty string, empty array, empty object.

val equal : json -> json -> bool

equal j0 j1 is compare j0 j1 = 0.

val compare : json -> json -> 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.t values are ignored.
val pp : t fmt

Nulls and options

val null : unit cons

null is Null (unit, meta).

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

option c constructs Some v values with c v and None ones with null.

Booleans

val bool : bool cons

bool b is Bool (b, meta).

Numbers

val number : float cons

number n is Number (n, meta).

val any_float : float cons

any_float v is number v if Float.is_finite v is true and string (Float.to_string v) otherwise. See Jsont.any_float.

val int32 : int32 cons

int32 is i as a JSON number.

val int64 : int64 cons

int64 i is i as a JSON number or a JSON string if not in the range [-253;253]. See also int64_as_string.

val int64_as_string : int64 cons

int64_as_string i is i as a JSON string. See also int64.

val int : int cons

int is i as a JSON number or a JSON string if not in the range [-253;253]. See also int_as_string.

val int_as_string : int cons

int_as_string i is i as a JSON string. See also int.

Strings

val string : string cons

string s is String (s, meta).

Arrays

val list : json list cons

list l is Array (l, meta).

val array : json array cons

array l is Array (Array.to_list a, meta). See also list.

Objects

val name : ?meta:Meta.t -> string -> name

name ?meta n is (n, meta). meta defaults to Meta.none.

val mem : name -> json -> mem

mem n v is (n, v). meta defaults to Meta.none.

val object' : object' cons

object o is Object (o, meta).

val find_mem : string -> object' -> mem option

find_mem n ms find the first member whose name matches n in ms.

val find_mem' : name -> object' -> mem option

find_mem n ms is find_mem (fst n) ms.

val object_names : object' -> string list

object_names ms are the names of ms.

val object_names' : object' -> name list

object_names ms are the names of ms.

Decode

val decode : 'a jsont -> json -> ('a, string) Stdlib.result

decode t j decodes a value from the generic JSON j according to type t.

val decode' : 'a jsont -> json -> ('a, Error.t) Stdlib.result

decode' is like decode but preserves the error structure.

Encode

val encode : 'a jsont -> 'a -> (json, string) Stdlib.result

encode t v encodes a generic JSON value for v according to type t.

val encode' : 'a jsont -> 'a -> (json, Error.t) Stdlib.result

encode' is like encode but preserves the error structure.

Recode

val recode : 'a jsont -> json -> (json, string) Stdlib.result

recode t v decodes v with t and encodes it with t.

val recode' : 'a jsont -> json -> (json, Error.t) Stdlib.result

recode' is like recode but preserves the error structure.

val update : 'a jsont -> json -> json

update is like recode but raises Jsont.Error.

Errors

val error_sort : exp:Sort.t -> json -> 'a

error_sort ~exp fnd errors when sort exp was expected but generic JSON fnd was found.

val error_type : 'a jsont -> json -> 'a

error_type t fnd errors when the type expected by t does not match fnd.

OCaml

Innovation. Community. Security.