RReverser / serde-xml-rs

xml-rs based deserializer for Serde (compatible with 1.0+)
https://crates.io/crates/serde-xml-rs
MIT License
270 stars 90 forks source link

Structure for optionally-empty lists #23

Closed indiv0 closed 7 years ago

indiv0 commented 7 years ago

I have a use case which involves parsing optionally-empty lists, like so:

<mylist>
    <item name="foo"></item>
    <item name="bar"></item>
</mylist>

or:

<mylist>
</mylist>

What would be the equivalent rust struct to represent this with serde-xml-rs?

RReverser commented 7 years ago

This one is exactly covered in readme:

#[derive(Debug, Deserialize)]
struct Project {
    pub name: String,

    #[serde(rename = "Item", default)]
    pub items: Vec<Item>
}

Here items maps to optional list of <Item ... /> tags.

TL;DR - always put #[serde(default)] on potentially-missing things.

indiv0 commented 7 years ago

Oh. My bad, I was missing the #[serde(default)]. Thanks!