Encoding.With_JSON_discriminantWith_JSON_discriminant is a subset of Encoding where the union/matching combinators (and associated functions) add discriminant for the JSON backend.
The following restrictions apply:
let e =
let open Data_encoding in
let open Data_encoding.With_JSON_discriminant in
…case_tag's only variant Tag includes both a numeric tag for the binary encoding and a string tag for the JSON encoding.
val case :
title:string ->
?description:string ->
case_tag ->
'a encoding ->
('t -> 'a option) ->
('a -> 't) ->
't casecase is similar to Encoding.case but it takes a SaferEncoding.case_tag parameter. This includes both a numeric tag and a string tag.
In Binary: This has no impact. The case_tag argument of Encoding already has a numeric tag.
In JSON: The SaferEncoding adds a field for discriminating the different cases, making these encodings less likely to include accidental bugs. More specifically, when you use case (Tag (_, s)) e _ _ then the underlying union uses an encoding based on e and s. Specifically, if e is an object encoding, then it adds the field (req "kind" (constant s)) to e.
union and matching now check that there are no duplicate "kind" discriminating values. If there is, they raises Invalid_argument.
val matched :
?tag_size:[ `Uint8 | `Uint16 ] ->
(int * string) ->
'a encoding ->
'a ->
match_resultSimilarly to case_tag, matched also takes an additional string parameter. This parameter is used in the same way as case (to add a "kind" field to the JSON encoding) and it fails in the same way as case.
val matching :
?tag_size:[ `Uint8 | `Uint16 ] ->
't matching_function ->
't case list ->
't encoding