#string #display #serde #from-str #fromstr

serde-strz

A serde wrapper that stores type using Display and FromStr

3 stable releases

1.1.1 Oct 16, 2020
1.1.0 Jul 20, 2020
1.0.0 Jun 24, 2020

#1363 in Encoding

Download history 51/week @ 2023-11-27 20/week @ 2023-12-04 1/week @ 2023-12-11 31/week @ 2023-12-18 6/week @ 2023-12-25 16/week @ 2024-01-01 83/week @ 2024-01-08 112/week @ 2024-01-15 123/week @ 2024-01-22 18/week @ 2024-01-29 48/week @ 2024-02-05 135/week @ 2024-02-12 59/week @ 2024-02-19 80/week @ 2024-02-26 109/week @ 2024-03-04 14/week @ 2024-03-11

277 downloads per month

MIT/Apache

15KB
124 lines

Serde Str

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.

Examples

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.

License

Licensed under either of

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.

Dependencies

~110–355KB