Module Jsont.Obj

JSON object maps.

This module allows to describe JSON objects. See a simple example.

module Unknown : sig ... end

Unknown object members.

Object maps

type ('o, 'dec) mems

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

type 'o map = ('o, 'o) mems

The type for JSON objects represented by values of type 'o.

val kind : ('o, _) mems -> string

kind c is the kind of object constructed by c.

val map : ?kind:string -> ?doc:string -> ?unknown:('o, unit) Unknown.t -> 'dec -> ('o, 'dec) mems

map dec is an empty JSON object decoded by dec.

  • kind names the entities represented by type 'o. Defaults to "".
  • doc is a documentation string for kind. Defaults to "".
  • dec is a constructor eventually returning a value of type 'o to be saturated with calls to mem. This is needed for decoding. Use nodec if the result is only used for encoding.
  • unknown indicates the behaviour on unknown members it defaults to Unknown_mems.drop, Unknown_mems.error is another option, if you want to collect them use map'
val map' : ?kind:string -> ?doc:string -> ?enc_meta:(Context.t -> 'o -> Meta.t) -> unknown:('o, 'mems) Unknown.t -> ('mems -> 'dec) -> ('o, 'dec) mems

map' dec is like map members that are not described by mem are parsed by unknown_mems and given to dec.

val map'' : ?kind:string -> ?doc:string -> ?enc_meta:(Context.t -> 'o -> Meta.t) -> unknown:('o, 'mems) Unknown.t -> (Context.t -> Meta.t -> 'mems -> 'dec) -> ('o, 'dec) mems

map'' dec is like map' except you get the full decoding context in dec.

val nodec : ?kind:string -> ?doc:string -> ?enc_meta:(Context.t -> 'o -> Meta.t) -> ?unknown:('o, 'mems) Unknown.t -> unit -> ('o, 'mems -> 'b) mems

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

val finish : 'o map -> 'o t

finish o is the map for objects represented by the fully constructed object map o.

val unfinish : 'o t -> 'o map

unfinish o is the object description of o. Raises Invalid_argument if o is not the result of finish.

Members

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

mem ?doc ?omit ?absent ?proj n t map is a member named n 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 n 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. It can be omitted if the result is only used for decoding.
  • enc_omit is for the encoding direction. If the member value returns true the member is omited in the JSON. Defaults to Fun.const false. See this example.
val opt_mem : ?doc:string -> ?enc:('o -> 'a option) -> string -> 'a t -> ('o, 'a option -> 'b) mems -> ('o, 'b) mems

opt_mem ?doc ?enc n t map is mem ?doc ?enc ~dec_absent:None ~enc_omit:Option.is_none n (some t) map. A shortcut to represent optional members with OCaml option values.

Case objects

This is for dealing with JSON object types or classes. See this example.

module Case : sig ... end

Case objects.

val case_mem : ?doc:string -> ?dec_absent:('cases, 'tag) Case.value -> ?enc:('o -> 'cases) -> ?enc_omit:(('cases, 'tag) Case.value -> bool) -> ?enc_case:('cases -> ('cases, 'tag) Case.value) -> string -> 'tag t -> ('cases, 'tag) Case.part list -> ('o, 'cases -> 'b) mems -> ('o, 'b) mems

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

  • doc is a documentation string for the member. Defaults to "".
  • dec_absent, if specified, is the case value used for the decoding direction when the case member named n 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.
  • parts enumerates all the cases, it is needed for decoding.

Derived descriptions

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

as_string_map t represents objects as a key-value map of type t.

val ignore : unit t

ignore ignores JSON objects on decode and errors on encoding.