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 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.

Decoding and encoding functions

These function create suitable dec and enc functions to give to map from standard decoding and encoding interfaces.

val dec : ('a -> 'b) -> Context.t -> Meta.t -> 'a -> 'b

dec f is a decoding function from f. This assumes f never fails.

val dec_result : ('a -> ('b, string) Stdlib.result) -> Context.t -> Meta.t -> 'a -> 'b

dec f is a decoding function from f, Error _ values are given to Error.msg.

val dec_failure : ('a -> 'b) -> Context.t -> Meta.t -> 'a -> 'b

dec f is a decoding function from f, Failure _ exceptions are catched and given to Error.msg.

val enc : ('b -> 'a) -> Context.t -> 'b -> 'a

enc f is an encoding function from f. This assumes f never fails.

val enc_result : ('b -> ('a, string) Stdlib.result) -> Context.t -> 'b -> 'a

enc_result f is an encoding function from f, Error _ values are given to Error.msg.

val enc_failure : ('b -> 'a) -> Context.t -> 'b -> 'a

enc_failure f is an encoding function from f, Failure _ exceptions are catched and given to Error.msg.