Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structure for optionally-empty lists #23

Closed
indiv0 opened this issue Jul 7, 2017 · 2 comments
Closed

Structure for optionally-empty lists #23

indiv0 opened this issue Jul 7, 2017 · 2 comments

Comments

@indiv0
Copy link

indiv0 commented Jul 7, 2017

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
Copy link
Owner

RReverser commented Jul 7, 2017

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
Copy link
Author

indiv0 commented Jul 7, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants