From f1d9261f0b76bc6efa2d81d1e9d2d330818b7b09 Mon Sep 17 00:00:00 2001 From: Tomas Burleigh Date: Fri, 30 Sep 2022 10:37:39 +1300 Subject: [PATCH] Fix Vec seralization #186 --- src/ser/mod.rs | 6 +++++- tests/round_trip.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 8130a8d..efee4d4 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -374,7 +374,11 @@ impl<'ser, W: Write> serde::ser::Serializer for &'ser mut Serializer { } fn serialize_struct(self, name: &'static str, _len: usize) -> Result { - self.open_root_tag(name)?; + if self.root { + self.open_root_tag(name)?; + } else { + self.open_tag(name)?; + } debug!("Struct {}", name); Ok(StructSerializer::new(self, false)) diff --git a/tests/round_trip.rs b/tests/round_trip.rs index 65c84ae..835a7c5 100644 --- a/tests/round_trip.rs +++ b/tests/round_trip.rs @@ -7,6 +7,12 @@ struct Item { source: String, } +#[derive(Debug, Serialize, Deserialize, PartialEq)] +struct Items { + #[serde(rename = "$value")] + items: Vec, +} + #[derive(Debug, Serialize, Deserialize, PartialEq)] enum Node { Boolean(bool), @@ -35,6 +41,29 @@ fn basic_struct() { assert_eq!(src, reserialized_item); } +#[test] +fn round_trip_list_of_structs() { + let src = r#"AppleStoreOrangeStore"#; + let should_be = Items { + items: vec![ + Item { + name: "Apple".to_string(), + source: "Store".to_string(), + }, + Item { + name: "Orange".to_string(), + source: "Store".to_string(), + } + ] + }; + + let items: Items = from_str(src).unwrap(); + assert_eq!(items, should_be); + + let reserialized_items = to_string(&items).unwrap(); + assert_eq!(src, reserialized_items); +} + #[test] fn round_trip_list_of_enums() { // Construct some inputs