Skip to main content

7  JSON, equality, and hashes

7.1 Documents

r8to_json() writes a value as a document: its type, the versions of the types it contains, and its fields. r8from_json() reads a document into a value of the type:

json <- r8to_json(cv)
cat(json)
{"$type":"demo::Resampler","$types":{"demo::Resampler":1},"$declarations":{"demo::Resampler":"sha256:ec3630433934a50cecdf2a6a44119986959c396811f709c5337602b22e3a7f53"},"n_resamples":10,"seed":2026,"type":"KFold"}
r8from_json(Resampler, json) == cv
[1] TRUE

A document is read with the same validation as a construction:

r8from_json(
  Resampler,
  '{"$type":"demo::Resampler","$types":{"demo::Resampler":1},"type":"KFold","n_resamples":1,"seed":null}'
)
Error:
! Cannot read `demo::Resampler`: 1 problem
- `n_resamples` must be at least 2; found 1. [below_min]

A document is RFC 8785 text in declaration order, so a value has one document whichever language wrote it; the Rust crate rtemis-r8 writes and reads the same documents.

A document records the hash of what each declaration says about a value, in $declarations. After the declaration changes, a document that was written before is refused, since its fields may mean something else:

Dose <- new_struct("Dose", fields = list(
  amount = field(type_double(min = 0), unit = "mg")
))
short <- r8to_json(Dose(amount = 500))
full <- r8to_json(Dose(amount = 500), declarations = TRUE)
Dose <- new_struct("Dose", fields = list(
  amount = field(type_double(min = 0), unit = "mg/kg")
))
r8from_json(Dose, short)
Error:
! Cannot read `demo::Dose`: 1 problem
- the document was written under another declaration of `demo::Dose` than the registered, and does not hold it; `r8to_json(x, declarations = TRUE)` writes one that does, and `r8from_json(declaration = "any")` reads it as one of the registered [declaration_changed]

With declarations = TRUE the document holds the declarations too, so that the error names what differs, and a document whose fields mean the same, as after a new optional field, is read:

r8from_json(Dose, full)
Error:
! Cannot read `demo::Dose`: 1 problem
- `amount` was in `mg`, and is in `mg/kg` [declaration_changed]

declaration = "any" reads a document as one of the registered declaration, on the caller’s responsibility. A document without $declarations, as one written by hand, claims none and is read:

r8from_json(Dose, '{"$type": "demo::Dose", "$types": {"demo::Dose": 1}, "amount": 2}')
<demo::Dose>
  amount: 2

7.2 Equality

== compares the values’ contents, as r8equal() does. A value equals a value of the same type with the same fields, defaults included:

cv == Resampler@KFold(n_resamples = 10L, seed = 2026L)
[1] TRUE
cv == Resampler@KFold(n_resamples = 5L, seed = 2026L)
[1] FALSE

7.3 Hashes

r8hash() is a SHA-256 content hash of the type, its declaration, and the value. Values that differ only in defaults written out have the same hash:

r8hash(cv)
[1] "sha256:7eb5904c366fbf3e3bdd493b42c7015546bb3ba4627855f72470d6871bd9a42a"
r8hash(cv) == r8hash(Resampler@KFold(n_resamples = 10L, seed = 2026L))
[1] TRUE

A change to the type’s declaration, other than to its descriptions, changes the hash of its values, so a hash identifies a value and the declaration it was checked against.

© 2026 E.D. Gennatas