gecko-dev/third_party/rust/serde_with
..
src
tests
.cargo-checksum.json
CHANGELOG.md
Cargo.toml
LICENSE-APACHE
LICENSE-MIT
README.md

README.md

Custom de/serialization functions for Rust's serde

docs.rs badge crates.io badge Build Status codecov


This crate provides custom de/serialization helpers to use in combination with serde's with-annotation.

Serde tracks a wishlist of similar helpers at serde#553.

Usage

Add this to your Cargo.toml:

[dependencies.serde_with]
version = "1.4.0"
features = [ "..." ]

The crate is divided into different modules. They contain helpers for external crates and must be enabled with the correspondig feature.

Annotate your struct or enum to enable the custom de/serializer.

#[derive(Deserialize, Serialize)]
struct Foo {
    #[serde(with = "serde_with::rust::display_fromstr")]
    bar: u8,
}

Most helpers implement both deserialize and serialize. If you do not want to derive both, you can simply derive only the necessary parts. If you want to mix different helpers, you can write your annotations like

#[derive(Deserialize, Serialize)]
struct Foo {
    #[serde(
        deserialize_with = "serde_with::rust::display_fromstr::deserialize",
        serialize_with = "serde_with::json::nested::serialize"
    )]
    bar: u8,
}

However, this will prohibit you from applying deserialize on the value returned by serializing a struct.

Attributes

The crate comes with custom attributes, which futher extend how serde serialization can be customized. They are enabled by default, but can be disabled, by removing the default features from this crate.

The serde_with crate re-exports all items from serde_with_macros. This means, if you want to use any proc_macros, import them like use serde_with::skip_serializing_none.

The documentation for the custom attributes can be found here.

License

Licensed under either of

at your option.

Contribution

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.