Skip to content

Commit

Permalink
Merge pull request #1 from julienr/fix-nested-vec-serialization
Browse files Browse the repository at this point in the history
WIP fix nested vec serialization: RReverser#186
  • Loading branch information
duckfromdiscord authored Apr 27, 2023
2 parents f1d9261 + 058e164 commit 3a02735
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
118 changes: 118 additions & 0 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ impl<'ser, W: Write> serde::ser::Serializer for &'ser mut Serializer<W> {
mod tests {
use super::*;
use serde::Serialize;
use simple_logger::SimpleLogger;

fn init_logger() {
SimpleLogger::new().with_utc_timestamps().init().unwrap();
}

#[test]
fn test_serialize_struct() {
Expand Down Expand Up @@ -451,4 +456,117 @@ mod tests {
let got = String::from_utf8(buffer).unwrap();
assert_eq!(got, should_be);
}

#[test]
fn test_serialize_tuple() {
#[derive(Serialize)]
struct A {
a: (f64, f64),
}

let should_be = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><A><a>0.2 0.3</a></A>";
let a = A { a: (0.2, 0.3) };
let got = to_string(&a).unwrap();
assert_eq!(got, should_be);
}

#[test]
fn test_serialize_attrs() {
#[derive(Serialize)]
struct A {
#[serde(rename = "@b", default)]
b: String,
}

let should_be = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><A b=\"42\" />";
let a = A { b: "42".to_owned() };
let got = to_string(&a).unwrap();
assert_eq!(got, should_be);
}
#[test]
fn test_serialize_nested_structs() {
#[derive(Debug, Serialize, PartialEq)]
struct Outer {
a: Inner,
}

#[derive(Debug, Serialize, PartialEq)]
struct Inner {
name: String,
}

let coll = Outer {
a: Inner {
name: "42".to_owned(),
},
};

let should_be =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><Outer><a><name>42</name></a></Outer>";
let got = to_string(&coll).unwrap();
assert_eq!(got, should_be);
}

#[test]
fn test_serialize_nested_collection() {
init_logger();

#[derive(Debug, Serialize, PartialEq)]
struct OuterCollection {
a: Vec<A>,
b: Vec<B>,
}

#[derive(Debug, Serialize, PartialEq)]
struct A {
// TODO: This makes it fail with yet another error
// #[serde(rename = "@name", default)]
name: String,
}
#[derive(Debug, Serialize, PartialEq)]
struct B {
name: String,
}

let coll = OuterCollection {
a: vec![
A {
name: "42".to_owned(),
},
A {
name: "43".to_owned(),
},
],
b: vec![B {
name: "44".to_owned(),
}],
};

let should_be = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<OuterCollection>
<a name=\"42\" />
<a name=\"42\" />
<b>
<name>44</name>
</b>
</OuterCollection>";
//let should_be = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><OuterCollection><a><name>42</name></a><a><name>43</name></a></OuterCollection>";
let got = to_string(&coll).unwrap();
assert_eq!(got, should_be);
}

#[test]
fn test_serialize_vector() {
#[derive(Debug, Serialize, PartialEq)]
struct OuterCollection {
a: Vec<String>,
}

let coll = OuterCollection {
a: vec!["42".to_owned()],
};

let str = to_string(&coll).unwrap();
println!("str={:?}", str);
}
}
3 changes: 2 additions & 1 deletion src/ser/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ impl<'ser, W: 'ser + Write> serde::ser::SerializeSeq for SeqSeralizer<'ser, W> {
}

fn end(self) -> Result<()> {
self.ser.abandon_tag()?;
// TODO: Commenting this out fixes it, but unsure why
// self.ser.abandon_tag()?;
Ok(())
}
}

0 comments on commit 3a02735

Please sign in to comment.