An encoding between an OCaml data type (the parameter) and a JSON representation. To be built using the predefined combinators provided by this module.
For instance, here is an encoding, of type (int * string)
encoding
, mapping values of type int * string
to JSON objects with a field code
of whose value is a number and a field message
whose value is a string.
let enc = obj2 (req "code" int) (req "message" string)
This encoding serves three purposes:
1. Output an OCaml value of type 'a
to an intermediate JSON representation using construct
. To be printed to actual JSON using an external library. 2. Input a JSON intermediate structure (already parsed with an external library) to produce an OCaml value of type 'a
. 3. Describe this encoding in JSON-schema format for inter-operability: you describe the encoding of your internal types, and obtain machine-readable descriptions of the formats as a byproduct. Specific documentation combinators are provided for that purpose.
By default, this library provides functions that work on the Json_repr.ezjsonm
data type, compatible with Ezjsonm.value
. However, encodings are not tied with this representation. See functor Make
and module Json_repr
for using another format.