Expand description
§Serde Str
Documentation | Github | crates.io | lib.rs
A (de)serializer for anything that has implemented FromStr / Display
but does not have Serialize/Deserialize.
§Example
use std::net::IpAddr;
/// A demonstration structure that holds a lonesome IP address.
#[derive(Serialize, Deserialize)]
struct WithIp {
#[serde(with = "serde_strz")]
ip: IpAddr,
}
use serde_json::{
from_str,
to_string,
};
let with_ip: WithIp = from_str(r#"{"ip": "127.0.0.1"}"#)?;
assert_eq!(WithIp { ip: [127, 0, 0, 1].into() }, with_ip);
assert_eq!(to_string(&with_ip)?, r#"{"ip":"127.0.0.1"}"#);
let with_ip: WithIp = from_str(r#"{"ip": "::"}"#)?;
assert_eq!(WithIp { ip: [0u16; 8].into() }, with_ip);
assert_eq!(to_string(&with_ip)?, r#"{"ip":"::"}"#);Modules§
- emp
- A (de)serializer for anything that has implemented
FromStr/Display(asToString) but does not haveSerialize/Deserialize, and is wrapped in anOptiontype, and may be represented as an empty string. - opt
- A (de)serializer for anything that has implemented
FromStr/Display(asToString) but does not haveSerialize/Deserialize, and is wrapped in anOptiontype.
Functions§
- deserialize
- Deserialize function, see crate docs examples to see how to use it
- serialize
- Serialize function, see crate docs examples to see how to use it