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

Ignore container when deserializing #74

Open
steven-joruk opened this issue Apr 26, 2020 · 3 comments
Open

Ignore container when deserializing #74

steven-joruk opened this issue Apr 26, 2020 · 3 comments

Comments

@steven-joruk
Copy link

Hi, I'd like to handle this kind of deserialization:

<root>
    <ignored>
        <item id="1"/>
        <item id="2"/>
    </ignored>
</root>
#[derive(YaDeserialize)]
#[yaserde(rename = "item")]
struct Item {
    id: String,
}

#[derive(YaDeserialize)]
#[yaserde(rename = "root")]
struct Root {
    /// Some yaserde attribute or custom deserializer to get rid of the `ignored` container.
    items: Vec<Item>,
}

Adding a custom deserializer for a newtype doesn't feel much better than just declaring the container type, and there doesn't seem to be an equivalent of serde's deserialize_with attribute.

Presuming there isn't a good way of handling this already, would you be interested in a PR to allow users to ignore containers like this?

Thanks.

@MarcAntoine-Arnaud
Copy link
Contributor

Hello @steven-joruk

Humm yes you have to declare all levels.

#[derive(YaDeserialize)]
#[yaserde(rename = "item")]
struct Item {
    #[yaserde(attribute)]
    id: String,
}

#[derive(YaDeserialize)]
#[yaserde(rename = "ignored")]
struct Ignored {
    items: Vec<Item>,
}

#[derive(YaDeserialize)]
#[yaserde(rename = "root")]
struct Root {
    ignored: Ignored,
}

It can be very complex to allow to fill with sub-childs.
The invert example is: an XML like

<root id="0">
    <ignored id="5">
        <item id="1"/>
        <item id="2"/>
    </ignored>
    <other>
        <something id="3" />
    </other>
</root>

What can be expected in values from that ...
Which means Serialiser can't reproduce it !

@steven-joruk
Copy link
Author

steven-joruk commented Apr 29, 2020

How about like adding an attribute to a field though, like this:

...
struct Root {
    #[yaserde(container = "ignored")]
    items: Vec<Item>,

    #[yaserde(container = "something")]
    somethings: Vec<Something>,
    ...

@MarcAntoine-Arnaud
Copy link
Contributor

Hi again;
container will be rename in YaSerDe.
It can works.

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