media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
182 stars 63 forks source link

Is it possible to deserialize based on attributes? #14

Open bbigras opened 5 years ago

bbigras commented 5 years ago

I'm not sure if my title is right but is it possible to deserialize this XML:

<group>
    <something name='value1'>a</something>
    <something name='value2'>b</something>
</group>

into

struct Group {
    value1: String, // containing 'a'
    value2: String, // containing 'b''
}

I know I could do a Something struct and deserialize into a Vec<Something> but I'm wondering if it's possible the other way.

MarcAntoine-Arnaud commented 5 years ago

Hi Bruno,

You use case is interesting. It's not possible right now. It requires to support issue #8 and I think also #2 but not sure.

I don't have to much time to spend on the library right now. I hope next month it will be better to solve issues.

I think right now, if it's urgent, right your own deserialiser as describe in the README.

WesleyAC commented 8 months ago

What's the status of this? I have a XML protocol like this that I want to interact with from Rust. I see both #8 and #2 are implemented, does that mean that this is now possible?

MarcAntoine-Arnaud commented 3 months ago

Hi @WesleyAC, sorry for the delay. I think it's not possible yet, it may requires to implement a new field attribute like:

struct Group {
    [yaserde(field_as_attribute='name')]
    value1: String, // containing 'a'
    [yaserde(field_as_attribute='name')]
    value2: String, // containing 'b''
}

or on structure:

[yaserde(field_as_attribute='name')]
struct Group {
    value1: String, // containing 'a'
    value2: String, // containing 'b''
}