serde-xml-rs icon indicating copy to clipboard operation
serde-xml-rs copied to clipboard

Deserializing fields with values

Open ifeherva opened this issue 7 years ago • 4 comments

What rust structure shall I go for the following situation? <MyStruct> <Foo a="351"> <deDE>Flugzeug</deDE> <enUS>Airplane</enUS> </Foo> <Foo a="342" b="bar">Triangle</Foo> </MyStruct>

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

Thanks!

ifeherva avatar Aug 30 '17 21:08 ifeherva

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

ifeherva avatar Aug 30 '17 21:08 ifeherva

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>

adnanademovic avatar Dec 10 '17 14:12 adnanademovic

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

RReverser avatar Dec 10 '17 16:12 RReverser

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.

adnanademovic avatar Dec 10 '17 17:12 adnanademovic