Module Jsont.Json

Generic JSON values and types.

This module provides a generic representation for JSON values and JSON types for it.

JSON values

type 'a jsont := 'a t

See Jsont.t.

type name = string node

The type for JSON member names.

type mem = name * t

The type for JSON object members.

and obj = mem list

The type for 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. | Obj of obj node

The type for generic JSON values.

val meta : t -> Meta.t

meta v is the metadata of value v.

val sort : t -> 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.


val null : t jsont

null represents JSON nulls by their generic representation.

val bool : t jsont

bool represents JSON bools by their generic representation.

val number : t jsont

number represents JSON nulls or numbers (explanation) by their generic representation.

val string : t jsont

string represents JSON strings by their generic representation.

val array : t jsont

array represents JSON arrays by their generic representation.

val obj : t jsont

obj represents JSON objects by their generic representation.

val any : t jsont

any represents any JSON value by its generic representation.

val unknown_obj : (obj, t) Obj.Unknown.Map.t

unknown_obj collects unknown members and their generic values into a generic JSON object.


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

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

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

decode' is like decode but preserves the error structure.

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

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

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

encode' is like encode but preserves the error structure.


module Cons : sig ... end

JSON value constructors.


val error_sort : Context.t -> exp:Sort.t -> t -> '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:t -> 'a

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


type format =
  1. | Minify

    Compact, no whitespace.

  2. | Indent

    Indented output.

  3. | Layout

    Follow Meta layout information.


The type JSON encoding formats.

val pp_null : unit fmt

pp_null formats a JSON null.

val pp_bool : bool fmt

pp_null formats a JSON bool.

val pp_number : float fmt

pp_number formats a JSON number of a JSON null if the float is not finite.

val pp_number' : int -> float fmt

pp_number' prec is like pp_number but uses prec with the format string "%.*g".

val pp_string : string fmt

pp_string formats a JSON string (quoted and escaped).

val pp' : ?format:format -> ?float_precision:int -> unit -> t fmt

pp' ~format ~float_precision () ppf j formats j on ppf.

  • format specifies how the output is formatted. Defaults to `Indent.
  • float_precision is used with the format string "%.*g" for numbers. It defaults to 17.
  • Non-finite numbers are output as JSON nulls, see Jsonit.Jsont.number.
  • Strings are assumed to be valid UTF-8.
val pp : t fmt

pp is pp' ~format:`Indent ~float_precision:17 ().