RReverser / serde-xml-rs

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

Deserializing fields with values #42

Open ifeherva opened 7 years ago

ifeherva commented 7 years ago

What rust structure shall I go for the following situation? ``

Flugzeug Airplane Triangle

``

I could not find a suitable example for describing a field with a value like <a b="c">k</a>

Thanks!

ifeherva commented 7 years ago

Seems like #[serde(rename="$value")] content:String, is the solution for content, but they seem not to work when they are optional.

adnanademovic commented 6 years ago

I'm having the same issue to solve, in a simpler version. I don't know how I'd represent a structure that can accept either of these 3:

<foo>bar</foo>
<foo><a>case_a</a></foo>
<foo><b>case_b</b></foo>
RReverser commented 6 years ago

Interesting. Does enum not work for these cases? (where all attributes and $value are fields of variants)

adnanademovic commented 6 years ago

I worked with an enum of approximately this design:

enum foo {
#[serde(rename="a")]
A(String),
#[serde(rename="b")]
B(String),
#[serde(rename="$value")] // added this
V(String), // added this
}

And it didn't work. I tried to refactor it as a struct, with fields A: Option<String>, B: Option<String>, and $value renamed V: Option<String>. In both cases tests pass for tagged cases, but as soon as I add the value case all tests fail.