package jsont
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=6206f73a66cb170b560a72e58f70b9fb2c20397b9ab819dceba49b6602b9b79e47ba307e6910e61ca4694555c66fdcd7a17490afb99548e8f43845a5a88913e7
doc/jsont/Jsont/Object/index.html
Module Jsont.ObjectSource
Mapping JSON objects.
Maps
The type for mapping JSON objects to values of type 'o. The 'dec type is used to construct 'o from members see mem.
map dec is an empty JSON object decoded by function dec.
kindnames the entities represented by the map anddocdocuments them. Both default to"".decis a constructor eventually returning a value of type'oto be saturated with calls tomem,case_memorkeep_unknown. This is needed for decoding. Useenc_onlyif the result is only used for encoding.
val map' :
?kind:string ->
?doc:string ->
?enc_meta:('o -> Meta.t) ->
(Meta.t -> 'dec) ->
('o, 'dec) mapmap' 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) mapenc_only () is like map' but can only be used for encoding.
finish map is a JSON type for objects mapped by map. Raises Invalid_argument if map describes a member name more than once.
Members
val mem :
?doc:string ->
?dec_absent:'a ->
?enc:('o -> 'a) ->
?enc_omit:('a -> bool) ->
string ->
'a t ->
('o, 'a -> 'b) map ->
('o, 'b) mapmem name t map is a member named name of type t for an object of type 'o being constructed by map.
docis a documentation string for the member. Defaults to"".dec_absent, if specified, is the value used for the decoding direction when the member namednameis missing. If unspecified decoding errors when the member is absent. See alsoopt_memand this example.encis used to project the member's value from the object representation'ofor encoding to JSON witht. It can be omitted if the result is only used for decoding.enc_omitis for the encoding direction. If the member value returned byencreturnstrueonenc_omit, the member is omited in the encoded JSON object. Defaults toFun.const false. See alsoopt_memand this example.
val opt_mem :
?doc:string ->
?enc:('o -> 'a option) ->
string ->
'a t ->
('o, 'a option -> 'b) map ->
('o, 'b) mapopt_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_omitA shortcut to represent optional members of type 'a with 'a option values.
Case objects
Read the cookbook on 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) mapcase_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:
docis a documentation string for the member. Defaults to"".tag_compareis used to compare tags. Defaults toStdlib.comparetag_to_stringis 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 namednameis missing. If unspecified decoding errors when the member is absent.encis used to project the value in which cases are stored from the object representation'ofor encoding to JSON. It can be omitted if the result is only used for decoding.enc_casedetermines the actual case value from the value returned byenc.enc_omitis used on the tag of the case returned byenc_caseto determine if the case member can be ommited in the encoded JSON objectcasesenumerates 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.
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.
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) mapkeep_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
as_string_map t maps object to key-value maps of type t. See also Mems.string_map and Jsont.json_mems.