Skip to content

atdml: add support for any JSON-like input formats via atd-jsonlike library#485

Merged
mjambon merged 12 commits intomasterfrom
martin/json-ast
Mar 30, 2026
Merged

atdml: add support for any JSON-like input formats via atd-jsonlike library#485
mjambon merged 12 commits intomasterfrom
martin/json-ast

Conversation

@mjambon
Copy link
Copy Markdown
Collaborator

@mjambon mjambon commented Mar 30, 2026

Summary

  • atd-jsonlike: new atd-jsonlike library providing a generic JSON-like AST (Atd_jsonlike.AST.t) with source location on every node, a Number.t type that stores int/float/literal representations simultaneously, and helpers for building error messages with file/line/column context.

  • atdml: generates foo_of_jsonlike readers (consuming Atd_jsonlike.AST.t) alongside the existing foo_of_yojson readers. Reader codegen is mode-parametrized to avoid duplication. The inlined Atdml_runtime is split into Atdml_runtime.Yojson and Atdml_runtime.Jsonlike submodules.

  • atdml --no-jsonlike / --no-yojson: opt-out flags to suppress either flavour of generated code, reducing output size and runtime dependencies.

  • atd-jsonlike tests and README: 15 unit tests for Number arithmetic and AST.loc_msg formatting; README documenting the module API and Number.t field semantics.

Test plan

  • make -C atdml test — all 46 atdml snapshot tests pass
  • make -C atd-jsonlike test — all 15 unit tests pass
  • Verify atdml --no-jsonlike foo.atd produces no of_jsonlike functions
  • Verify atdml --no-yojson foo.atd produces no of_yojson/yojson_of/of_json/to_json functions

🤖 Generated with Claude Code

Closes #453

mjambon and others added 6 commits March 29, 2026 16:53
New opam package with an empty library. The JSON-like AST type will be
defined next. The library will serve as a runtime dependency for code
generated by atdml that reads generic tree data (JSON, YAML, TOML, etc.).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Rename package, folder, library name
- Pos.ml, Loc.ml: record type definitions matching the mli
- Number.ml: of_int, of_float, of_string_opt with JSON number validation
- AST.ml: tree type + loc_msg for human-readable location strings

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Generate foo_of_jsonlike functions (consuming Atd_jsonlike.AST.t)
alongside the existing foo_of_yojson functions.  Error messages from
of_jsonlike include the source location carried by every AST node.

Implementation uses a mode record to parametrize the reader codegen,
sharing all logic between the two input tree types.  The inlined
Atdml_runtime module is split into Atdml_runtime.Yojson and
Atdml_runtime.Jsonlike submodules.

Two new CLI flags let users opt out of either flavour:
  --no-jsonlike  skip of_jsonlike readers (no atd-jsonlike dependency)
  --no-yojson    skip of_yojson readers, writers, and I/O functions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- README.md documents the AST type, Number.t field semantics (with a
  table), and Loc/Pos conventions
- tests/test.ml covers Number.of_int, Number.of_float,
  Number.of_string_opt (valid and invalid inputs), and AST.loc_msg
- Makefile and test script follow the same pattern as atdml/atdcat
- dune-project: add re and testo :with-test deps for atd-jsonlike;
  remove mistakenly added re dep from atdgen-codec-runtime
- root Makefile: run atd-jsonlike tests as part of test-common

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
mjambon and others added 5 commits March 30, 2026 00:03
…e tests

Each test_e2e test now also silently verifies that:
  yojson_in -> of_jsonlike -> yojson_of -> yojson_out2 == yojson_out

A yojson_to_jsonlike_ helper (with dummy locations) is injected into the
generated Main.ml. Tests for abstract types and JSON adapters opt out via
~test_jsonlike:false since those features are intentionally not supported
by the jsonlike path.

Also fixes Yojson.Safe.t compatibility: v3.0.0 removed `Floatlit, `Tuple,
and `Variant constructors, so the match is now exhaustive with 8 cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ames

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mjambon mjambon changed the title atdml: of_jsonlike readers; atd-jsonlike: README and tests atdml: add support for any JSON-like input formats via atd-jsonlike library Mar 30, 2026
When <json adapter.ocaml="M"> is used, of_jsonlike now calls
M.normalize_jsonlike before deserialization, mirroring the M.normalize
call in of_yojson. The function name is intentionally different to
avoid conflicts with the Yojson adapter interface.

Only module-based adapters are supported for jsonlike; inline adapters
(adapter.to_ocaml / adapter.from_ocaml) are not supported and continue
to be silently skipped for the jsonlike path.

Implementation: the mode record's supports_json_adapters:bool field is
replaced with normalize_of_adapter:(json_adapter -> string option), so
each mode knows how to derive its normalize expression from an adapter.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mjambon mjambon merged commit d301cee into master Mar 30, 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.

atdml: support one-way conversions from a generic JSON-like data tree with locations

1 participant