Skip to content

[Question]: Deserialize optional vector #510

@sandersantema

Description

@sandersantema

Afaik the following line in Cargo.toml should mean that I'm using the latest git version of quick-xml:

quick-xml = { git = "https://github.com/tafia/quick-xml", features = ['serialize']}

Although I've put question in the title this issue might be a bug, but I can't tell whether I'm doing something wrong or if it is indeed a bug.

I've got the following xml:

<ENTRY>
  <CUE_V2 NAME="foo"></CUE_V2>
  <CUE_V2 NAME="bar"></CUE_V2>
</ENTRY>

And I've got the following struct to deserialize:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "ENTRY")]
struct Entry {
    #[serde(rename = "CUE_V2")]
    cues: Vec<Cue>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde_with::serde_as]
struct Cue {
    #[serde(rename = "@NAME")]
    name: String,
}

This works but is incorrect since the cue vector should be optional like so:

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename = "ENTRY")]
struct Entry {
    #[serde(rename = "CUE_V2")]
    cues: Option<Vec<Cue>>,
}

But this gives the error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: UnexpectedEnd([69, 78, 84, 82, 89])', src/main.rs:330:56

There might be a trivial solution, but after two days of using rust in total I can't seem to find it :)

If I may take the opportunity to ask another unrelated question, how do I build the documentation for the git version of the package locally? I've been looking through #490 but I think having the actual documentation would be a bit better :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraysIssues related to mapping XML content onto arrays using serdebugserdeIssues related to mapping from Rust types to XML

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions