Json_encoding Ppx deriver

ppx_deriving_encoding is a json-encoding ppx deriver.

    type x = {
    a : string;
    b : int list;
    c : Ezjsonm.value option;
}
[@@deriving json_encoding]

will produce the encoding:

  let x_enc : x encoding =
conv
  (fun {a; b; c} -> (a, b, c))
  (fun (a, b, c) -> {a; b; c})
  (obj3
    (req "a" string)
    (req "b" (list int))
    (opt "c" any_ezjson_value))

Most of regular json types are handled. GADT and variant inheritance are not handled yet.

Field Options

General options

Tuple options

Variant options

If it is not a string enumeration, any constructor or polymorphic variant will produce a union encoding. Any case of the union can receive [@title expr], [@description expr], [@kind "kind"] attributes.

[@kind "kind_name"] will add the encoding

(obj1 (req "kind" (constant "kind_name")))

to allow several constructor with the same type to be well desctructed.

type t =
| A of x [@kind "a"]
| B of y
| C of x [@kind "c"] [@@deriving json_encoding]

will produce :

  let enc =
union [
  case
    (conv (fun x -> (), x) (fun ((), x) -> x)
      (merge_objs (obj1 (req "kind" (constant "a"))) x_enc))
    (function A x -> Some x | _ -> None)
    (fun x -> A x);
  case
    y_enc
    (function B x -> Some x | _ -> None)
    (fun x -> B x);
  case
    (conv (fun x -> (), x) (fun ((), x) -> x)
      (merge_objs (obj1 (req "kind" (constant "c"))) x_enc))
    (function C x -> Some x | _ -> None)
    (fun x -> C x);
]

Top type options