Versioning a Serialized Record
A version or a compatible format contract helps a reader interpret records produced by different editions of a program.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace an optional addition
An earlier settings record may contain only size. A later record also contains color. A later reader can choose a documented default when reading older data. See optional fields.
Test two directions
An older reader can accept the newer record only if its rules permit unknown fields, and it cannot necessarily preserve them when rewriting. Test reading both old and new examples. Changing a contract explains the corresponding compatibility problem at a function boundary.[1]
Translate prior field meanings into today's internal model
Version one stores duration in integer seconds under seconds. Version two stores integer milliseconds under duration_ms. An older value of three becomes three thousand milliseconds after inspecting its version. Treating the numeric field as though all versions used the current unit would shrink the duration. See changing a function contract.
v1 {seconds:3} → internal duration 3000 ms
v2 {duration_ms:3000} → same internal duration
Unknown version → defined incompatible-version result
Keep accepted versions and tests visible
A reader can normalize old data at its boundary while exposing a single internal representation. Preserve historical input examples and test them against current readers. Decide which loss of precision is permitted when writing back to an older format.