Skip to content

Commit

Permalink
add Option default example
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi committed Jun 17, 2022
1 parent 0c51e72 commit 693ded7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_trim"
version = "0.3.0"
version = "0.3.1"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "serde deserialize_with String trim"
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,16 @@ fn main() {
let json = r#"{"name":" "}"#;
let foo = serde_json::from_str::<OptionFoo>(json).unwrap();
assert_eq!(foo.name, None);

#[derive(Deserialize)]
struct OptionBar {
#[serde(default, deserialize_with = "option_string_trim")]
name: Option<String>,
addr: String,
}
let json = r#"{"addr":"ABC"}"#;
let foo = serde_json::from_str::<OptionBar>(json).unwrap();
assert_eq!(foo.name, None);
assert_eq!(foo.addr, "ABC");
}
```
11 changes: 11 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ fn test_option_string_trim() {
let json = r#"{"name":" "}"#;
let foo = serde_json::from_str::<OptionFoo>(json).unwrap();
assert_eq!(foo.name, None);

#[derive(Deserialize)]
struct OptionBar {
#[serde(default, deserialize_with = "option_string_trim")]
name: Option<String>,
addr: String,
}
let json = r#"{"addr":"ABC"}"#;
let foo = serde_json::from_str::<OptionBar>(json).unwrap();
assert_eq!(foo.name, None);
assert_eq!(foo.addr, "ABC");
}

0 comments on commit 693ded7

Please sign in to comment.