Module serde_with::rust::display_fromstr [−][src]
De/Serialize using Display
and FromStr
implementation
This allows to deserialize a string as a number. It can be very useful for serialization formats like JSON, which do not support integer numbers and have to resort to strings to represent them.
Examples
#[derive(Deserialize, Serialize)] struct A { #[serde(with = "serde_with::rust::display_fromstr")] address: Ipv4Addr, #[serde(with = "serde_with::rust::display_fromstr")] b: bool, } let v: A = serde_json::from_str(r#"{ "address": "192.168.2.1", "b": "true" }"#).unwrap(); assert_eq!(Ipv4Addr::new(192, 168, 2, 1), v.address); assert!(v.b); let x = A { address: Ipv4Addr::new(127, 53, 0, 1), b: false, }; assert_eq!(r#"{"address":"127.53.0.1","b":"false"}"#, serde_json::to_string(&x).unwrap());
Functions
deserialize | Deserialize T using FromStr |
serialize | Serialize T using Display |