serde_trim:: serde deserialize_with string trim
String
Option<String>
Vec<String>
BTreeSet<String>
HashSet<String>
VecDeque<String>
LinkedList<String>
BinaryHeap<String>
Supports multiple std::collections types
The current also supports non-empty trim
- binaryheap_non_empty_string_trim
- binaryheap_string_trim
- btreeset_non_empty_string_trim
- btreeset_string_trim
- hashset_non_empty_string_trim
- hashset_string_trim
- inkedlist_non_empty_string_trim
- linkedlist_string_trim
- option_string_trim
- string_trim
- vec_non_empty_string_trim
- vec_string_trim
- vecdeque_non_empty_string_trim
- vecdeque_string_trim
use serde_derive::Deserialize;
use serde_trim::*;
use std::collections::*;
fn main() {
#[derive(Deserialize)]
struct Foo {
#[serde(deserialize_with = "string_trim")]
name: String,
}
let json = r#"{"name":" "}"#;
let foo = serde_json::from_str::<Foo>(json).unwrap();
assert_eq!(foo.name, "");
#[derive(Deserialize)]
struct OptionFoo {
#[serde(deserialize_with = "option_string_trim")]
name: Option<String>,
}
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");
#[derive(Deserialize)]
struct VecFoo {
#[serde(deserialize_with = "vec_string_trim")]
name: Vec<String>,
}
let json = r#"{"name":[" ","foo","b ar","hello "," rust"]}"#;
let foo = serde_json::from_str::<VecFoo>(json).unwrap();
assert_eq!(foo.name, vec!["", "foo", "b ar", "hello", "rust"]);
#[derive(Deserialize)]
struct BTreeSetFoo {
#[serde(deserialize_with = "btreeset_string_trim")]
name: BTreeSet<String>,
}
let json = r#"{"name":[" ","foo","b ar","hello "," rust"]}"#;
let foo = serde_json::from_str::<BTreeSetFoo>(json).unwrap();
let expected: BTreeSet<String> = BTreeSet::from_iter([
"".into(),
"foo".into(),
"b ar".into(),
"hello".into(),
"rust".into(),
]);
assert_eq!(foo.name, expected);
#[derive(Deserialize)]
struct HashSetFoo {
#[serde(deserialize_with = "hashset_string_trim")]
name: HashSet<String>,
}
let json = r#"{"name":[" ","foo","b ar","hello "," rust"]}"#;
let foo = serde_json::from_str::<HashSetFoo>(json).unwrap();
let expected: HashSet<String> = HashSet::from_iter([
"".into(),
"foo".into(),
"b ar".into(),
"hello".into(),
"rust".into(),
]);
assert_eq!(foo.name, expected);
#[derive(Deserialize)]
struct VecDequeFoo {
#[serde(deserialize_with = "vecdeque_string_trim")]
name: VecDeque<String>,
}
let json = r#"{"name":[" ","foo","b ar","hello "," rust"]}"#;
let foo = serde_json::from_str::<VecDequeFoo>(json).unwrap();
assert_eq!(foo.name, vec!["", "foo", "b ar", "hello", "rust"]);
#[derive(Deserialize)]
struct LinkedListFoo {
#[serde(deserialize_with = "linkedlist_string_trim")]
name: LinkedList<String>,
}
let json = r#"{"name":[" ","foo","b ar","hello "," rust"]}"#;
let foo = serde_json::from_str::<LinkedListFoo>(json).unwrap();
assert_eq!(
foo.name,
LinkedList::from_iter([
"".into(),
"foo".into(),
"b ar".into(),
"hello".into(),
"rust".into(),
])
);
#[derive(Deserialize)]
struct BinaryHeapFoo {
#[serde(deserialize_with = "binaryheap_string_trim")]
name: BinaryHeap<String>,
}
let json = r#"{"name":[" ","foo","b ar","hello "," rust"]}"#;
let foo = serde_json::from_str::<BinaryHeapFoo>(json).unwrap();
assert_eq!(
foo.name.into_vec(),
vec!["rust", "hello", "b ar", "", "foo"]
);
}