Module Jsont.Json

Generic JSON values.

JSON values

val meta : json -> Meta.t

meta v is the metadata of value v.

val sort : json -> Sort.t

sort v is the sort of value v.

val find_mem : string -> obj -> mem option

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

Constructors

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.

Base types

val null : unit cons

null is Null (unit, meta).

val bool : bool cons

bool b is Bool (b, meta).

val number : float cons

number n is Number (n, meta).

val string : string cons

string s is String (s, 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 obj : obj cons

obj o is Obj (o, meta).

Derived

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

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

val list : json list cons

list l is Array (l, meta).

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 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 int_as_string : int cons

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

val int64_as_string : int64 cons

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

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 stub : json cons

stub j is a stub value of the sort value of j.

Converting

val decode : ?ctx:Context.t -> 'a t -> json -> ('a, string) Stdlib.result

dcode t j decodes a value from the generic JSON j according to type t. ctx defaults to Context.root.

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

decode' is like decode but preserves the error structure.

val encode : ?ctx:Context.t -> 'a t -> 'a -> (json, string) Stdlib.result

encode t v encodes a generic JSON value for v according to type t. ctx default to Contet.root.

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

encode' is like encode but preserves the error structure.

Errors

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

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

val error_kind : Context.t -> exp:string -> fnd:json -> 'a

error_kind' p m ~exp ~fnd errors when kind exp was expected but sort fnd was found.