#serde #serde-json #deserialize-trim #deserialize-with

serde_trim

serde deserialize_with String trim.Supports multiple std::collections types

6 releases (1 stable)

1.0.0 Jul 23, 2023
0.4.0 Sep 1, 2022
0.3.1 Jun 17, 2022
0.2.0 Jun 17, 2022
0.1.0 Jun 17, 2022

#179 in Encoding

Download history 90/week @ 2023-08-09 95/week @ 2023-08-16 32/week @ 2023-08-23 63/week @ 2023-08-30 627/week @ 2023-09-06 1619/week @ 2023-09-13 2605/week @ 2023-09-20 2025/week @ 2023-09-27 1972/week @ 2023-10-04 1839/week @ 2023-10-11 1975/week @ 2023-10-18 2253/week @ 2023-10-25 2877/week @ 2023-11-01 3614/week @ 2023-11-08 2239/week @ 2023-11-15 704/week @ 2023-11-22

9,961 downloads per month
Used in gogo

MIT AND Apache-2.0

13KB
179 lines

serde_trim:: serde deserialize_with string trim

GitHub Actions Crates.io Docs.rs Download DepStatus Coverage Status

Support trim

  • String
  • Option<String>
  • Vec<String>
  • BTreeSet<String>
  • HashSet<String>
  • VecDeque<String>
  • LinkedList<String>
  • BinaryHeap<String>

Supports multiple std::collections types

how to use

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"]
    );
}

Dependencies

~0.5–1MB
~25K SLoC