diff --git a/Cargo.toml b/Cargo.toml index a4382f8..eea4b9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_trim" -version = "0.3.0" +version = "0.3.1" authors = ["baoyachi "] edition = "2021" description = "serde deserialize_with String trim" diff --git a/README.md b/README.md index d881724..10471f3 100644 --- a/README.md +++ b/README.md @@ -29,5 +29,16 @@ fn main() { let json = r#"{"name":" "}"#; let foo = serde_json::from_str::(json).unwrap(); assert_eq!(foo.name, None); + + #[derive(Deserialize)] + struct OptionBar { + #[serde(default, deserialize_with = "option_string_trim")] + name: Option, + addr: String, + } + let json = r#"{"addr":"ABC"}"#; + let foo = serde_json::from_str::(json).unwrap(); + assert_eq!(foo.name, None); + assert_eq!(foo.addr, "ABC"); } ``` \ No newline at end of file diff --git a/lib.rs b/lib.rs index 9f1029d..003ad71 100644 --- a/lib.rs +++ b/lib.rs @@ -45,4 +45,15 @@ fn test_option_string_trim() { let json = r#"{"name":" "}"#; let foo = serde_json::from_str::(json).unwrap(); assert_eq!(foo.name, None); + + #[derive(Deserialize)] + struct OptionBar { + #[serde(default, deserialize_with = "option_string_trim")] + name: Option, + addr: String, + } + let json = r#"{"addr":"ABC"}"#; + let foo = serde_json::from_str::(json).unwrap(); + assert_eq!(foo.name, None); + assert_eq!(foo.addr, "ABC"); }