Custom de/serialization functions for Rust's serde
This crate provides custom de/serialization helpers to use in combination with serde's with-annotation and with the improved serde_as
-annotation.
Some common use cases are:
- De/Serializing a type using the
Display
andFromStr
traits, e.g., foru8
,url::Url
, ormime::Mime
. CheckDisplayFromStr
for details. - Support for arrays larger than 32 elements or using const generics.
With
serde_as
large arrays are supported, even if they are nested in other types.[bool; 64]
,Option<[u8; M]>
, andBox<[[u8; 64]; N]>
are all supported, as this examples shows. - Skip serializing all empty
Option
types with#[skip_serializing_none]
. - Apply a prefix to each field name of a struct, without changing the de/serialize implementations of the struct using
with_prefix!
. - Deserialize a comma separated list like
#hash,#tags,#are,#great
into aVec<String>
. Check the documentation forserde_with::StringWithSeparator::<CommaSeparator, T>
.
Getting Help
Check out the user guide to find out more tips and tricks about this crate.
For further help using this crate you can open a new discussion or ask on users.rust-lang.org. For bugs, please open a new issue on GitHub.
Use serde_with
in your Project
# Add the current version to your Cargo.toml
The crate contains different features for integration with other common crates. Check the feature flags section for information about all available features.
Examples
Annotate your struct or enum to enable the custom de/serializer.
The #[serde_as]
attribute must be placed before the #[derive]
.
The as
is analogous to the with
attribute of serde.
You mirror the type structure of the field you want to de/serialize.
You can specify converters for the inner types of a field, e.g., Vec<DisplayFromStr>
.
The default de/serialization behavior can be restored by using _
as a placeholder, e.g., BTreeMap<_, DisplayFromStr>
.
DisplayFromStr
// This will serialize
Foo
// into this JSON
Large and const-generic arrays
serde does not support arrays with more than 32 elements or using const-generics.
The serde_as
attribute allows circumventing this restriction, even for nested types and nested arrays.
On top of it, [u8; N]
(aka, bytes) can use the specialized "Bytes"
for efficiency much like the serde_bytes
crate.
// This allows us to serialize a struct like this
let arrays: = Arrays ;
assert!;
skip_serializing_none
This situation often occurs with JSON, but other formats also support optional fields.
If many fields are optional, putting the annotations on the structs can become tedious.
The #[skip_serializing_none]
attribute must be placed before the #[derive]
.
// This will serialize
Foo
// into this JSON
Advanced serde_as
usage
This example is mainly supposed to highlight the flexibility of the serde_as
-annotation compared to serde's with-annotation.
More details about serde_as
can be found in the user guide.
use Duration;
// This will serialize
Durations
// into this JSON
// and serializes
Bytes
// into this JSON
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
For detailed contribution instructions please read CONTRIBUTING.md
.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.