Skip to content

ppx: add [@json.catch_all] for forward-compatible string enums - #88

Merged
andreypopp merged 1 commit into
melange-community:mainfrom
ahrefs:raman/json-open-enum
May 20, 2026
Merged

ppx: add [@json.catch_all] for forward-compatible string enums#88
andreypopp merged 1 commit into
melange-community:mainfrom
ahrefs:raman/json-open-enum

Conversation

@ahrrv

@ahrrv ahrrv commented May 19, 2026

Copy link
Copy Markdown
Contributor

For polymorphic and classic variants under [@@deriving json], the [@json.catch_all] attribute marks the constructor that absorbs any unknown wire tag, lossless. The constructor's argument is Melange_json.unknown_variant_case, a record with fields { tag : string; payload : Melange_json.t list option }; payload distinguishes bare strings (None) from array forms (Some xs), so the decoder captures both wire shapes and the encoder re-emits them faithfully.

Pairs naturally with [@@json.compact_variants] so the known cases are bare strings.

Also ships unknown_variant_case_jsonschema on the native side, structurally compatible with ppx_deriving_jsonschema_runtime.t (no hard dependency).

For polymorphic and classic variants under [@@deriving json], the
[@json.catch_all] attribute marks the constructor that absorbs any
unknown wire tag, lossless. The constructor's argument is
Melange_json.unknown_variant_case, a record with fields
{ tag : string; payload : Melange_json.t list option }; payload
distinguishes bare strings (None) from array forms (Some xs), so the
decoder captures both wire shapes and the encoder re-emits them
faithfully.

Pairs naturally with [@@json.compact_variants] so the known cases are
bare strings.

Also ships unknown_variant_case_jsonschema on the native side,
structurally compatible with ppx_deriving_jsonschema_runtime.t (no
hard dependency).
@ahrrv
ahrrv force-pushed the raman/json-open-enum branch from a63a4e7 to 71e6704 Compare May 20, 2026 07:37
@ahrrv ahrrv changed the title ppx native: add [@json.open_enum] for forward-compatible string enums ppx: add [@json.catch_all] for forward-compatible string enums May 20, 2026
@andreypopp
andreypopp merged commit 6d7e257 into melange-community:main May 20, 2026
1 check passed
jchavarri added a commit that referenced this pull request Jun 29, 2026
* main: (40 commits)
  Respect nonrec type declarations in json derivers (#94)
  ppx/test: cover encode behavior for @drop_default variants (#84)
  allow @@compact_variants to parse ["Tag"] in addition to "Tag" for nullary variants (#93)
  Make extra JSON fields allowed by default (#92)
  Fix error messages for `[@@json.compact_variants]` (#91)
  Revert "Fix error message for @@compact_variants" (#90)
  Fix error message for @@compact_variants (#87)
  ppx: add [@json.catch_all] for forward-compatible string enums (#88)
  classify: filter undefined values from JS objects (#86)
  Add primitives semantics table to readme (#82)
  Support @json.compact_variants on polyvariant types (#79)
  ppx: allow dropping arbitrary values with `[@drop_default]` (#77)
  .gitignore: .claude
  upgrade ppxlib to 0.36 / handle Ptyp_open (#60)
  build with Melange 6 / OCaml 5.4 (#72)
  fmt: format using OCamlformat 0.28 (#75)
  fix: use `opam-check-npm-deps` compatible with OPAM 2.5 (#74)
  native: re_export yojson
  browser: remove dup of_json_error_to_string
  native: +of_json_error_to_string
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants