Skip to content

Commit

Permalink
WIP fix nested vec serialization: RReverser#186
Browse files Browse the repository at this point in the history
  • Loading branch information
julienr committed Mar 4, 2023
1 parent 920e54d commit 67112f8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,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 @@ -447,4 +452,64 @@ mod tests {
let got = String::from_utf8(buffer).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 str = to_string(&coll).unwrap();
println!("str={:?}", str);
}
#[test]
fn test_serialize_nested_collection() {
init_logger();

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

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

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

let str = to_string(&coll).unwrap();
println!("str={:?}", str);
}

#[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);
}
}
7 changes: 6 additions & 1 deletion src/ser/seq.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Serializer;
use crate::error::{Error, Result};
use log::debug;
use serde::ser::Serialize;
use std::io::Write;

Expand All @@ -22,16 +23,20 @@ impl<'ser, W: 'ser + Write> serde::ser::SerializeSeq for SeqSeralizer<'ser, W> {
T: ?Sized + Serialize,
{
let must_close_tag = self.ser.build_start_tag()?;
debug!("start, must_close_tag={:?}", must_close_tag);
value.serialize(&mut *self.ser)?;
if must_close_tag {
debug!("end");
self.ser.end_tag()?;
self.ser.reopen_tag()?;
}
Ok(())
}

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

0 comments on commit 67112f8

Please sign in to comment.