package jsont

  1. Overview
  2. Docs

Mapping JSON base types.

Maps

type ('a, 'b) map

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

val map : ?kind:string -> ?doc:string -> ?dec:(Meta.t -> 'a -> 'b) -> ?enc:('b -> 'a) -> ?enc_meta:('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 the map and doc documents them. Both default to "".
  • dec is used to decode values of type 'a to values of type 'b. Can be omitted if the map 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 map is only used for decoding, the default unconditionally errors.
  • enc_meta is used to recover JSON metadata (source text layout information) from a value to encode. The default unconditionnaly returns Jsont.Meta.none.

These functions can be used to quickly devise dec and enc functions from standard OCaml conversion interfaces.

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 map maps with map JSON nulls represented by () to values of type 'a. See also Jsont.null.

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

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

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

number map maps with map 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 map maps with map unescaped JSON strings represented by UTF-8 encoded string values to 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 OCaml conversion interfaces. See also Jsont.of_of_string.

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

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

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

dec f is a decoding function from f. Error _ values are given to Error.msg, prefixed by kind: (if specified).

val dec_failure : ?kind:string -> ('a -> 'b) -> Meta.t -> 'a -> 'b

dec f is a decoding function from f. Failure _ exceptions are catched and given to Error.msg, prefixed by kind: (if specified).

val enc : ('b -> 'a) -> 'b -> 'a

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

val enc_result : ?kind:string -> ('b -> ('a, string) Stdlib.result) -> 'b -> 'a

enc_result f is an encoding function from f. Error _ values are given to Error.msg, prefixed by kind: (if specified).

val enc_failure : ?kind:string -> ('b -> 'a) -> 'b -> 'a

enc_failure f is an encoding function from f. Failure _ exceptions are catched and given to Error.msg, prefixed by kind: (if specified).

OCaml

Innovation. Community. Security.