With_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:
- The case encodings must be objects.
- The case encoding objects must not include a "kind" field.
- The case encoding objects must not have duplicate field names.
- The JSON discriminants must all be distinct.
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 case
case 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
s. Specifically, if
e is an object encoding, then it adds the field
(req "kind" (constant s)) to
matching now check that there are no duplicate
"kind" discriminating values. If there is, they raises
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