Crates.io | serde-strz |
lib.rs | serde-strz |
version | 1.1.1 |
source | src |
created_at | 2020-06-24 15:56:21.000495 |
updated_at | 2020-10-16 13:37:48.930784 |
description | A serde wrapper that stores type using Display and FromStr. |
homepage | https://github.com/ZaneHannanAU/serde-str |
repository | |
max_upload_size | |
id | 257611 |
size | 30,942 |
Documentation | Github | crates.io | libs.rs
A serde wrapper that simplifies (de)serializaton of data types using Display
(as ToString::to_string(&self)
) and FromStr
as intermediataries.
use serde::{Serialize, Deserialize};
use std::net::IpAddr;
#[derive(Serialize, Deserialize)]
struct Struct {
// By default IpAddr serializes the same in human-readable formats
// like json. This forces the impl even for binary formats.
//
// More imporantly this is useful for types which don't have serde impl.
#[serde(with = "serde_str")]
ip: IpAddr,
}
#[derive(Serialize, Deserialize)]
struct Optional {
// The above but handling null types
#[serde(with = "serde_str::opt")]
ip: Option<IpAddr>,
}
#[derive(Serialize, Deserialize)]
struct Empty {
// The above but an empty string is a none-value
#[serde(with = "serde_str::emp")]
ip: Option<IpAddr>,
}
#[derive(Serialize, Deserialize)]
struct EmptyOptional {
// The above but an empty string, null, or unspecified is a none-value.
#[serde(with = "serde_str::emp", default)]
ip: Option<IpAddr>,
}
See docs.rs for more examples and usage.
Licensed under either of
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.