diff --git a/serde_with/src/formats.rs b/serde_with/src/formats.rs index 27228325..3171a2bc 100644 --- a/serde_with/src/formats.rs +++ b/serde_with/src/formats.rs @@ -138,3 +138,23 @@ impl Separator for ColonSeparator { ":" } } + +/// Predefined separator using a single linefeed. +pub struct UnixLineSeparator; + +impl Separator for UnixLineSeparator { + #[inline] + fn separator() -> &'static str { + "\n" + } +} + +/// Predefined separator using a DOS/Windows line ending. +pub struct DosLineSeparator; + +impl Separator for DosLineSeparator { + #[inline] + fn separator() -> &'static str { + "\r\n" + } +} diff --git a/serde_with/tests/serde_as/lib.rs b/serde_with/tests/serde_as/lib.rs index 74cf32b4..566f168b 100644 --- a/serde_with/tests/serde_as/lib.rs +++ b/serde_with/tests/serde_as/lib.rs @@ -519,7 +519,7 @@ fn test_bytes_or_string() { #[test] fn string_with_separator() { use serde_with::{ - formats::{CommaSeparator, SpaceSeparator}, + formats::{CommaSeparator, DosLineSeparator, SpaceSeparator, UnixLineSeparator}, StringWithSeparator, }; @@ -529,26 +529,44 @@ fn string_with_separator() { #[serde_as(as = "StringWithSeparator::")] tags: Vec, #[serde_as(as = "StringWithSeparator::")] - // more_tags: Vec, more_tags: BTreeSet, + #[serde_as(as = "StringWithSeparator::")] + lf_tags: BTreeSet, + #[serde_as(as = "StringWithSeparator::")] + crlf_tags: BTreeSet, } let v: A = serde_json::from_str( r##"{ "tags": "#hello #world", - "more_tags": "foo,bar,bar" + "more_tags": "foo,bar,bar", + "lf_tags": "foo\nbar\nbar", + "crlf_tags": "foo\r\nbar\r\nbar" }"##, ) .unwrap(); assert_eq!(vec!["#hello", "#world"], v.tags); - assert_eq!(2, v.more_tags.len()); + assert_eq!( + BTreeSet::from(["foo".to_string(), "bar".to_string()]), + v.more_tags + ); + assert_eq!( + BTreeSet::from(["foo".to_string(), "bar".to_string()]), + v.lf_tags + ); + assert_eq!( + BTreeSet::from(["foo".to_string(), "bar".to_string()]), + v.crlf_tags + ); let x = A { tags: vec!["1".to_string(), "2".to_string(), "3".to_string()], more_tags: BTreeSet::default(), + lf_tags: BTreeSet::default(), + crlf_tags: BTreeSet::default(), }; assert_eq!( - r#"{"tags":"1 2 3","more_tags":""}"#, + r#"{"tags":"1 2 3","more_tags":"","lf_tags":"","crlf_tags":""}"#, serde_json::to_string(&x).unwrap() ); }