Skip to content

Preserve shared S7 classes through JSON round trips - #120

Merged
nbenn merged 8 commits into
nbenn:mainfrom
t-kalinowski:fix/s7-compatibility-20261012
Nov 20, 2026
Merged

nbenn merged 8 commits into
nbenn:mainfrom
t-kalinowski:fix/s7-compatibility-20261012

Conversation

@t-kalinowski

Copy link
Copy Markdown
Contributor

With updated S7, JSON round trips rebuild nested constructor environments, lose shared class definitions, and skip property validation because the reader only recognizes the older class attribute.

Record S7 class references through the existing S7 state tag, preserve repeated class definitions in reference bookkeeping, and omit the constructor environment that new_class() recreates. Resolve the decoded class through S7_class() and validate the current instance representation. Objects read from one document retain their shared class, and re-encoding that document stays stable.

Corpus checks compare reconstructed environments by structure and sharing topology, matching the format's existing equivalence contract. The S3-property fixture is defined outside the test runner namespace. Constructor bindings resolved by newer S7 are checked as data, while their promise-based refusal cases remain covered with S7 0.2.2.

@t-kalinowski

Copy link
Copy Markdown
Contributor Author

Thanks for maintaining typedjson! I'm planning to submit the next version of S7 to CRAN on November 16 (see RConsortium/S7#786).

Would you be able to merge these compatibility changes and submit an updated version of typedjson to CRAN before then? Please let me know if you need more time or if there’s anything I can help with.

The test from main expects the class at `attr(x, "S7_class")`, but S7
1.0.0 writes `_S7_class`. The round-trip helper now takes its comparison
from the caller, since lintr flags a call from one helper file into
another.
In typedjson a reference is already a class recorded by its name, so a
`reference` key gave the word a second meaning. A class that an older S7
built now reads back as the class itself, the way S7 1.0.0 stores it,
and an older S7 refuses a class reference by the version it needs.
A read with `code = FALSE` named only `S7_class`, though S7 1.0.0 writes
`_S7_class`.
The round-trip tests fell back to comparing every corpus value by
structure once `identical()` failed, which would let a regression in
any other value pass as an equivalent one.
The docs named only `S7_class`, which S7 1.0.0 renamed `_S7_class` and
fills with a class reference. The NEWS entry now says what changed for
a user rather than that something did.
@nbenn

nbenn commented Nov 20, 2026

Copy link
Copy Markdown
Owner

I merged main into this branch and pushed a few changes on top, so that the fix can go out with typedjson 0.1.2. Thanks for the PR. Some tweaks:

  • The ~s7 payload of a class reference is now {"class_ref": …} rather than {"reference": …}. In typedjson, "reference" already means a class recorded by its name.
  • Reading a class reference no longer stops at stopifnot(). With an S7 older than 1.0.0 installed, the read fails with an error naming the version it needs. With S7 1.0.0 and a class that an older S7 built, which binds no .S7_class_ref, the object comes back holding the class itself, the way S7 1.0.0 stores it.
  • With code = FALSE, the refusal now names the attribute the value carries, _S7_class or S7_class. A test that main gained in the meantime expected the old path and failed with S7 1.0.0.
  • The round-trip tests keep identical() for every value that holds no S7 class, and compare by structure only where the reader rebuilds an S7 class.
  • The README, the design vignette and the json_write() docs now describe the _S7_class attribute.

The fix reads three S7 internals: the S7_class_ref class, its class binding, and the .S7_class_ref binding in the environment of a constructor. I opened RConsortium/S7#806 to ask whether S7 would export S7_class_storage() for the lookup the reader does.

@nbenn
nbenn merged commit aaeccf1 into nbenn:main Nov 20, 2026
22 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.

2 participants