Serialize
, Deserialize
for wrapper typesThis crate provides several custom derives that provide implementations of
serde's Serialize
and Deserialize
traits for wrapper types, as well as
Deserialize
implementations that perform some validation.
Sometimes you have a single-field type
```rust
struct Contact { email: String, } ```
which you want to serialize and deserialize as a string instead of a struct, e.g. you want its JSON
representation to just be ""user@domain.com"
" instead of "{ "email": "user@domain.com" }
". The
above derive attribute creates Serialize
and Deserialize
implementations for that purpose, as
well as Into
and From
implementations to convert between String
and Contact
.
Another example is a validated wrapper type like
```rust
struct Email(String); ```
that should never be instantianted with a string that doesn't represent a valid email address.
If you implement Email: TryFrom<String>
using the try_from
crate, such that conversion fails for invalid addresses, the derived Deserialize
will also fail if
the string is in the wrong format.