package atdgen-runtime

  1. Overview
  2. Docs

Support for the retired tag_field feature. This converts the following

{
  "id": "abc123",
  "type": "User",
  "payload": 17
}

into

{
  "id": "abc123",
  "type": "User",
  "payload": [ "User", 17 ]
}

As illustrated, two parameters need to be specified: the name of the field that holds the tag of the variant ("type" in the example) and the name of the field that holds the value associated with that tag ("payload" in the example).

If the value is missing, we'll use the representation of an enum, i.e. a json string:

{
  "id": "abc124",
  "type": "Event",
}

becomes

{
  "id": "abc124",
  "type": "Event",
  "payload": "Event"
}

Additionally, a catch-all case is supported if known_tags is specified. Given the following ATD type definitions:

type t = {
  id: string;
  payload: payload;
} <json adapter.ocaml="My_adapter">

type payload = [
  | User of int
  | Event

  (* catch-all *)
  | Unknown of (string * json nullable)
]

and the module My_adapter defined as follows:

module My_adapter = Atdgen_runtime.Json_adapter.Type_and_value_fields.Make(
  struct
    let type_field_name = "type"
    let value_field_name = "payload"
    let known_tags = Some ["User"; "Event"]
  end
)

and given the following json input:

{
  "id": "abc124",
  "type": "Group",
  "payload": {}
}

we obtain this normalized json, compatible with the type definitions:

{
  "id": "abc124",
  "payload": ["Unknown", ["Group", {}]]
}

If there's no payload, it is treated as if it were null. Given the following:

{
  "id": "abc124",
  "type": "Thing"
}

we get this normalized json:

{
  "id": "abc124",
  "payload": ["Unknown", ["Thing", null]]
}
module type Param = sig ... end
module Make (Param : Param) : S

Functor needed to produce a module using the correct parameters.