Cannot deserialize with double quotes in the header

Hi all,

I'm trying to deserialize a csv with double quotes in the header.

 #[derive(Clone, Debug, Deserialize)]
     pub struct TestRecord {
         #[serde(rename = "a")]
         pub code: String,
         #[serde(rename = "b")]
         pub name: String,
         //#[serde(rename = "\x22type\x22 of stuff")]
         #[serde(rename = "\"type\" of stuff")]
         pub type_of_stuff: String,
         #[serde(rename = "c")]
         pub c: String,
     pub fn test() {
         let data = r#"a;b;"type" of stuff;c
         let mut rdr = csv::ReaderBuilder::new()
         let v = rdr
             .collect::<Result<Vec<TestRecord>, csv::Error>>();
         println!("{:#?}", v);

I tried \" and \x22, same error :slight_smile:

        Deserialize {
            pos: Some(
                Position {
                    byte: 22,
                    line: 2,
                    record: 1,
            err: DeserializeError {
                field: None,
                kind: Message(
                    "missing field `\"type\" of stuff`",

I tried with csv::ReaderBuilder::new().escape(Some(b'"')).double_quote(false).has_headers(true).delimiter(b';').flexible(true).double_quote(false).from_reader(data.as_bytes()); (or only escape or only double_quote). Same error.

How to do that ?

Please wrap your code blocks in ``` to get appropriate formatting:

better ?

1 Like

I'm fairly certain the serde team didn't have cases like these in mind when they were designing their procedural macros, so you might want to raise this question directly on their repo.

For now, one possible workaround might be to split of the header, replace the double quotes with something more a bit more expected (single quotes or something else) and put it back together again.

1 Like

The double quote " is by default used to quote field names. This seems to confuse the deserializer here. You can disable quoting completely by calling .quoting(false).
Using a different quoting character also seems to do the trick.



This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.