Skip to content

atdj: add <json repr="object"> for sum types (externally-tagged encoding)#491

Merged
mjambon merged 1 commit intoahrefs:masterfrom
mjambon:atdj-sum-repr-object
Apr 11, 2026
Merged

atdj: add <json repr="object"> for sum types (externally-tagged encoding)#491
mjambon merged 1 commit intoahrefs:masterfrom
mjambon:atdj-sum-repr-object

Conversation

@mjambon
Copy link
Copy Markdown
Collaborator

@mjambon mjambon commented Apr 10, 2026

Summary

  • Sum types annotated with <json repr="object"> now encode/decode tagged variants as single-key JSON objects {"Constructor": payload} instead of the default two-element array ["Constructor", payload].
  • Unit variants (no payload) remain plain strings regardless of the repr annotation.
  • Updates Util.extractTag() to also handle JSONObject values (in addition to String and JSONArray).
  • Adds a shape type to the test suite with Circle, Square, and Point variants.

Motivation

This encoding matches the Rust/Serde default externally-tagged representation and was already implemented for OCaml in atdml (commit 0fc67a5). It also maps naturally to YAML as a single-key mapping:

# array encoding (default):
- - Circle
  - 3.14
# object encoding (<json repr="object">):
- Circle: 3.14

Decoding uses ((JSONObject)o).getDouble("Circle") (key-based access) instead of ((JSONArray)o).getDouble(1) (index-based). Encoding wraps the payload with {\"Circle\":} instead of [\"Circle\",].

Test plan

  • dune runtest atdj/test — codegen diff test passes (Java runtime tests require javac, not available in CI)

…ing)

Sum types annotated with <json repr="object"> now encode tagged variants
as single-key JSON objects {"Constructor": payload} instead of the default
two-element array ["Constructor", payload].

Unit variants (no payload) are always encoded as plain strings regardless
of the repr annotation, consistent with all other backends.

This matches the Rust/Serde default externally-tagged encoding and also
maps naturally to YAML as a single-key mapping:

  # array encoding (default):
  - - Circle
    - 3.14
  # object encoding (<json repr="object">):
  - Circle: 3.14

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mjambon mjambon merged commit 88e9272 into ahrefs:master Apr 11, 2026
2 of 3 checks passed
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.

1 participant