serde_versions_derive
exports an attribute macro that adds versioning support for structs.
When serializing a named field struct it will automatically add a new field containing the version. It also allows deserializing the versioned type directly back to the unversioned one.
Under the hood this works by creating a new struct that wraps the original struct plus adds a version byte field.
Internally this new struct uses #[serde(flatten)]
to serialize as expected.
The original struct uses #[serde(to, from)]
to add the version field when serializing and remove it when deserializing.
```rust
struct S { i: i32, } ```
This produces the following ```rust
struct S { i: i32, }
struct _Sv3 { version: u8, #[serde(flatten)] inner: S }
// plus implementations of To, From and to_versioned() for S ```
and will Serialize to:
json
{
"version": 3,
"i": 0
}
Note due to limitations of #[serde(to, from)]
this does not support structs with type parameters.