[][src]Struct serde_with::rust::StringWithSeparator

pub struct StringWithSeparator<Sep>(_);

De/Serialize a delimited collection using Display and FromStr implementation

You can define an arbitrary separator, by specifying a type which implements Separator. Some common ones, like space and comma are already predefined and you can find them here.

An empty string deserializes as an empty collection.

Examples

use serde_with::{CommaSeparator, SpaceSeparator};
use std::collections::BTreeSet;

#[derive(Deserialize, Serialize)]
struct A {
    #[serde(with = "serde_with::rust::StringWithSeparator::<SpaceSeparator>")]
    tags: Vec<String>,
    #[serde(with = "serde_with::rust::StringWithSeparator::<CommaSeparator>")]
    more_tags: BTreeSet<String>,
}

let v: A = serde_json::from_str(r##"{
    "tags": "#hello #world",
    "more_tags": "foo,bar,bar"
}"##).unwrap();
assert_eq!(vec!["#hello", "#world"], v.tags);
assert_eq!(2, v.more_tags.len());

let x = A {
    tags: vec!["1".to_string(), "2".to_string(), "3".to_string()],
    more_tags: BTreeSet::new(),
};
assert_eq!(r#"{"tags":"1 2 3","more_tags":""}"#, serde_json::to_string(&x).unwrap());

Methods

impl<Sep> StringWithSeparator<Sep> where
    Sep: Separator
[src]

pub fn serialize<S, T, V>(values: T, serializer: S) -> Result<S::Ok, S::Error> where
    S: Serializer,
    T: IntoIterator<Item = V>,
    V: Display
[src]

Serialize collection into a string with separator symbol

pub fn deserialize<'de, D, T, V>(deserializer: D) -> Result<T, D::Error> where
    D: Deserializer<'de>,
    T: FromIterator<V>,
    V: FromStr,
    V::Err: Display
[src]

Deserialize a collection from a string with separator symbol

Trait Implementations

impl<Sep: Eq> Eq for StringWithSeparator<Sep>
[src]

impl<Sep: PartialOrd> PartialOrd<StringWithSeparator<Sep>> for StringWithSeparator<Sep>
[src]

impl<Sep: Copy> Copy for StringWithSeparator<Sep>
[src]

impl<Sep: Default> Default for StringWithSeparator<Sep>
[src]

impl<Sep: PartialEq> PartialEq<StringWithSeparator<Sep>> for StringWithSeparator<Sep>
[src]

impl<Sep: Clone> Clone for StringWithSeparator<Sep>
[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<Sep: Ord> Ord for StringWithSeparator<Sep>
[src]

fn max(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl<Sep: Debug> Debug for StringWithSeparator<Sep>
[src]

impl<Sep: Hash> Hash for StringWithSeparator<Sep>
[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

Auto Trait Implementations

impl<Sep> Send for StringWithSeparator<Sep> where
    Sep: Send

impl<Sep> Sync for StringWithSeparator<Sep> where
    Sep: Sync

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]