package jsont

  1. Overview
  2. Docs

Mapping JSON objects.

Maps

type ('o, 'dec) map

The type for mapping JSON objects to values of type 'o. The 'dec type is used to construct 'o from members see mem.

val map : ?kind:string -> ?doc:string -> 'dec -> ('o, 'dec) map

map dec is an empty JSON object decoded by function dec.

  • kind names the entities represented by the map and doc documents them. Both default to "".
  • dec is a constructor eventually returning a value of type 'o to be saturated with calls to mem, case_mem or keep_unknown. This is needed for decoding. Use enc_only if the result is only used for encoding.
val map' : ?kind:string -> ?doc:string -> ?enc_meta:('o -> Meta.t) -> (Meta.t -> 'dec) -> ('o, 'dec) map

map' dec is like map except you get the object's decoding metdata in dec and enc_meta is used to recover it on encoding.

val enc_only : ?kind:string -> ?doc:string -> ?enc_meta:('o -> Meta.t) -> unit -> ('o, 'a) map

enc_only () is like map' but can only be used for encoding.

val finish : ('o, 'o) map -> 'o t

finish map is a JSON type for objects mapped by map. Raises Invalid_argument if map describes a member name more than once.

Members

module Mem : sig ... end

Member maps.

val mem : ?doc:string -> ?dec_absent:'a -> ?enc:('o -> 'a) -> ?enc_omit:('a -> bool) -> string -> 'a t -> ('o, 'a -> 'b) map -> ('o, 'b) map

mem name t map is a member named name of type t for an object of type 'o being constructed by map.

  • doc is a documentation string for the member. Defaults to "".
  • dec_absent, if specified, is the value used for the decoding direction when the member named name is missing. If unspecified decoding errors when the member is absent. See also opt_mem and this example.
  • enc is used to project the member's value from the object representation 'o for encoding to JSON with t. It can be omitted if the result is only used for decoding.
  • enc_omit is for the encoding direction. If the member value returned by enc returns true on enc_omit, the member is omited in the encoded JSON object. Defaults to Fun.const false. See also opt_mem and this example.
val opt_mem : ?doc:string -> ?enc:('o -> 'a option) -> string -> 'a t -> ('o, 'a option -> 'b) map -> ('o, 'b) map

opt_mem name t map is:

let dec_absent = None and enc_omit = Option.is_none in
Jsont.Object.mem name (Jsont.some t) map ~dec_absent ~enc_omit

A shortcut to represent optional members of type 'a with 'a option values.

Case objects

Read the cookbook on case objects.

module Case : sig ... end

Case objects.

val case_mem : ?doc:string -> ?tag_compare:('tag -> 'tag -> int) -> ?tag_to_string:('tag -> string) -> ?dec_absent:'tag -> ?enc:('o -> 'cases) -> ?enc_omit:('tag -> bool) -> ?enc_case:('cases -> ('cases, 'tag) Case.value) -> string -> 'tag t -> ('cases, 'tag) Case.t list -> ('o, 'cases -> 'a) map -> ('o, 'a) map

case_mem name t cases map is mostly like mem except the member name selects an object representation according to the member value of type t:

  • doc is a documentation string for the member. Defaults to "".
  • tag_compare is used to compare tags. Defaults to Stdlib.compare
  • tag_to_string is used to stringify tags for improving error reporting.
  • dec_absent, if specified, is the case value used for the decoding direction when the case member named name is missing. If unspecified decoding errors when the member is absent.
  • enc is used to project the value in which cases are stored from the object representation 'o for encoding to JSON. It can be omitted if the result is only used for decoding.
  • enc_case determines the actual case value from the value returned by enc.
  • enc_omit is used on the tag of the case returned by enc_case to determine if the case member can be ommited in the encoded JSON object
  • cases enumerates all the cases, it is needed for decoding.

The names of the members of each case must be disjoint from name or those of map otherwise Invalid_argument is raised on finish. Raises Invalid_argument if case_mem was already called on map.

Unknown members

Read the cookbook on unknown object members.

On case objects each individual case has its own behaviour unless the combinators are used on the case object map in which case it overrides the behaviour of cases. For those cases that use keep_unknown they will get the result of an empty builder in their decoding function and the encoder is ignored on encode.

module Mems : sig ... end

Uniform members.

val skip_unknown : ('o, 'dec) map -> ('o, 'dec) map

skip_unknown map makes map skip unknown members. This is the default, no need to specify it. Raises Invalid_argument if keep_unknown was already specified on map.

val error_unknown : ('o, 'dec) map -> ('o, 'dec) map

error_unknown map makes map error on unknown members. Raises Invalid_argument if keep_unknown was already specified on map. See this example.

val keep_unknown : ?enc:('o -> 'mems) -> ('mems, _, _) Mems.map -> ('o, 'mems -> 'a) map -> ('o, 'a) map

keep_unknown mems map makes map keep unknown member with mems. Raises Invalid_argument if keep_unknown was already specified on map. See this this example, Mems.string_map and Jsont.json_mems.

JSON types

val as_string_map : ?kind:string -> ?doc:string -> 'a t -> 'a Stdlib.Map.Make(Stdlib.String).t t

as_string_map t maps object to key-value maps of type t. See also Mems.string_map and Jsont.json_mems.

val zero : unit t

zero ignores JSON objects on decoding and encodes an empty object.

OCaml

Innovation. Community. Security.