Module Jsont.Base

Mapping JSON base types.

Maps

type ('a, 'b) map

The type for mapping values of type 'a to values of type 'b.

val map : ?kind:string -> ?doc:string -> ?dec:(Context.t -> Meta.t -> 'a -> 'b) -> ?enc:(Context.t -> 'b -> 'a) -> ?enc_meta:(Context.t -> 'b -> Meta.t) -> unit -> ('a, 'b) map

map ~kind ~doc ~dec ~enc ~enc_meta () maps JSON base types represented by value of type 'a to values of type 'b with:

  • kind names the entities represented by type 'b. Defaults to "".
  • doc is a documentation string for kind. Defaults to "".
  • dec is used to decode values of type 'a to values of type 'b. Can be omitted if the result is only used for encoding, the default unconditionally errors.
  • enc is used to encode values of type 'b to values of type 'a. Can be omitted if the result is only used for decoding, the default unconditionally errors.
  • enc_meta is used to recover JSON metadata from a value to encode. The default unconditionnaly returns Json.Meta.none.

See also Base.map_result, Base.map_failure.

val map_result : ?kind:string -> ?doc:string -> ?dec:('a -> ('b, string) Stdlib.result) -> ?enc:('b -> 'a) -> ?enc_meta:(Context.t -> 'b -> Meta.t) -> unit -> ('a, 'b) map

map_result is like map but interfaces with a result based decoding function and assumes enc always succeeds but handles Failure _ exceptions and turns them into Jsont.Json.Error exceptions.

val map_failure : ?kind:string -> ?doc:string -> ?dec:('a -> 'b) -> ?enc:('b -> 'a) -> ?enc_meta:(Context.t -> 'b -> Meta.t) -> unit -> ('a, 'b) map

map_failure is like map but interfaces with t_{of,to} function pairs that raise Stdlib.Failure.

val id : ('a, 'a) map

id is the identity map.

val ignore : ('a, unit) map

ignore is the ignoring map. It ignores decodes and errors on encodes.

JSON types

val null : (unit, 'a) map -> 'a t

null m maps with m JSON nulls represented by () to values of type 'a with map. See also Jsont.null.

val bool : (bool, 'a) map -> 'a t

bool m maps with m JSON booleans represented by bool values to values of type 'a with map. See also Jsont.bool.

val number : (float, 'a) map -> 'a t

number m maps with m JSON nulls or numbers represented by float values to values of type 'a. The float representation decodes JSON nulls to Float.nan and lossily encodes any non-finite to JSON null (explanation). See also Jsont.number.

val string : (string, 'a) map -> 'a t

string m maps with m JSON strings represented by UTF-8 encoded and unescaped string values by values of type 'a. See also Jsont.string.