Skip to content

add a way to use a variant to define a json string instead of a string list #67

@Khady

Description

@Khady

I want to encode three possible string 2025-06-18, 2025-03-26, and 2024-11-05 as a variant. Right now if I use a variant it will be represented in json as a string list. It is fine when transporting internal data, but it isn't great when consuming and interacting with external APIs.

Without adding a different representation for the variant, maybe we can just expose some helpers, a la enum of cmdliner. So instead of

  type protocol_version =
    | V2025_06_18 [@json.name "2025-06-18"]
    | V2025_03_26 [@json.name "2025-03-26"]
    | V2024_11_05 [@json.name "2024-11-05"]
  [@@deriving json]

We would have

type protocol_version =
  | V2025_06_18
  | V2025_03_26 
  | V2024_11_05

let protocol_version_of_json, protocol_version_to_json = Melange_json.Primitives.string_enum [ V2025_06_18, "2025-06-18"; V2025_03_26, "2025-03-26"; V2024_11_05, "2024-11-05" ]

It's not as good as just defining the variant, but maybe an acceptable middle ground.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions