Struct serde_with::rust::StringWithSeparator [−][src]
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());
Implementations
impl<Sep> StringWithSeparator<Sep> where
Sep: Separator,
[src][−]
Sep: Separator,
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][−]
S: Serializer,
T: IntoIterator<Item = V>,
V: Display,
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][−]
D: Deserializer<'de>,
T: FromIterator<V>,
V: FromStr,
V::Err: Display,
Deserialize a collection from a string with separator symbol
Trait Implementations
impl<Sep: Clone> Clone for StringWithSeparator<Sep>
[src][+]
impl<Sep: Copy> Copy for StringWithSeparator<Sep>
[src]
impl<Sep: Debug> Debug for StringWithSeparator<Sep>
[src][+]
impl<Sep: Default> Default for StringWithSeparator<Sep>
[src][+]
impl<Sep: Eq> Eq for StringWithSeparator<Sep>
[src]
impl<Sep: Hash> Hash for StringWithSeparator<Sep>
[src][+]
impl<Sep: Ord> Ord for StringWithSeparator<Sep>
[src][+]
impl<Sep: PartialEq> PartialEq<StringWithSeparator<Sep>> for StringWithSeparator<Sep>
[src][+]
impl<Sep: PartialOrd> PartialOrd<StringWithSeparator<Sep>> for StringWithSeparator<Sep>
[src][+]
impl<Sep> StructuralEq for StringWithSeparator<Sep>
[src]
impl<Sep> StructuralPartialEq for StringWithSeparator<Sep>
[src]
Auto Trait Implementations
impl<Sep> RefUnwindSafe for StringWithSeparator<Sep> where
Sep: RefUnwindSafe,
Sep: RefUnwindSafe,
impl<Sep> Send for StringWithSeparator<Sep> where
Sep: Send,
Sep: Send,
impl<Sep> Sync for StringWithSeparator<Sep> where
Sep: Sync,
Sep: Sync,
impl<Sep> Unpin for StringWithSeparator<Sep> where
Sep: Unpin,
Sep: Unpin,
impl<Sep> UnwindSafe for StringWithSeparator<Sep> where
Sep: UnwindSafe,
Sep: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src][+]
T: Clone,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,