Traits and macros for deserializing data onto an existing instance of a
struct via serde. It is somewhat like a more featureful version of adding
#[serde::default(...)] annotations everywhere except that it is able to
use runtime data instead of hardcoded defaults.
Usage
The main trait for this crate is the [DeserializeOver] trait and its
corresponding derive macro. It works analogously to serde's Deserialize
trait except that struct fields that are not present within the data being
deserialized keep their values as is.
For a simple struct, this ends up looking something like this:
use DeserializeOver;
# use Deserializer;
# use StrRead;
let json = r#"{ "a": "test" }"#;
let mut inst = MyStruct ;
let mut de = new;
inst.deserialize_over
.expect;
assert_eq!;
assert_eq!;
Here, the serialized json only has a value for the a field so when it gets
deserialized over the existing instance the a field is updated while the
b field remains unchanged.
Nested Structs
By default, the fields of the struct are deserialized using serde's
Deserialize. This means that, by default, nested structs must be
deserialized in entirety and cannot be deserialized on top of existing data.
To mark that subfields should instead be deserialized via
[DeserializeOver] the derive macro supports the #[deserialize_over]
attribute.
use DeserializeOver;
# use Deserializer;
# use StrRead;
let json = r#"{ "inner": { "b": 5 } }"#;
let mut inst = default;
let mut de = new;
inst.deserialize_over
.expect;
assert_eq!;
assert_eq!;
assert_eq!;
Extras
This crate also provides the [DeserializeInto] extension trait on all
serde Deserializers which takes the operands in the other order.
use ;
# use Deserializer;
# use StrRead;
let json = r#"{ "a": "test" }"#;
let mut inst = default;
let mut de = new;
de.deserialize_into
.expect;
assert_eq!;
assert_eq!;