rust-csv icon indicating copy to clipboard operation
rust-csv copied to clipboard

Question about (de)serializing vectors

Open corvusrabus opened this issue 3 years ago • 2 comments

Hi, thanks for providing this great crate. Currently, when I am using the crate to serialise a vector inside a struct it will be flattened, so say I have:

struct A { a : String, b : Vec<i64> }

and I have a flexible writer, then two As {"Hello",[1,2,3]}, {"Bye", [4,5,6,7]} would be serialized to "Hello", 1,2,3, "Bye", 4,5,6,7

Is there any way I can tell the CSV writer to serialize it to "Hello", [1,2,3] "Bye", [4,5,6,7] ?

corvusrabus avatar Nov 25 '21 14:11 corvusrabus

Please provide a complete Rust program, any relevant inputs, actual output and desired output.

BurntSushi avatar Nov 25 '21 15:11 BurntSushi

Program

use std::io; use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug)] struct A { a: String, b: Vec, }

fn main() { let x = A { a: "Hello".to_string(), b: vec![1, 2, 3] }; let y = A { a: "Bye".to_string(), b: vec![4, 5, 6, 7] }; let mut w = csv::WriterBuilder::new() .flexible(true).has_headers(false).from_writer(io::stdout()); w.serialize(x); w.serialize(y); }

Actual output Hello,1,2,3 Bye,4,5,6,7 Desired output Hello,[1,2,3] Bye,[4,5,6,7]

corvusrabus avatar Nov 26 '21 11:11 corvusrabus