Skip to content

Commit

Permalink
Fix Vec<Struct> seralization RReverser#186
Browse files Browse the repository at this point in the history
  • Loading branch information
TBurleigh committed Sep 29, 2022
1 parent 920e54d commit f1d9261
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ impl<'ser, W: Write> serde::ser::Serializer for &'ser mut Serializer<W> {
}

fn serialize_struct(self, name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
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))
Expand Down
29 changes: 29 additions & 0 deletions tests/round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ struct Item {
source: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Items {
#[serde(rename = "$value")]
items: Vec<Item>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
enum Node {
Boolean(bool),
Expand Down Expand Up @@ -35,6 +41,29 @@ fn basic_struct() {
assert_eq!(src, reserialized_item);
}

#[test]
fn round_trip_list_of_structs() {
let src = r#"<?xml version="1.0" encoding="UTF-8"?><Items><Item><name>Apple</name><source>Store</source></Item><Item><name>Orange</name><source>Store</source></Item></Items>"#;
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
Expand Down

0 comments on commit f1d9261

Please sign in to comment.