serde-xml-rs
serde-xml-rs copied to clipboard
Deserializing fields with values
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!
Seems like
#[serde(rename="$value")] content:String,
is the solution for content, but they seem not to work when they are optional.
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>
Interesting. Does enum not work for these cases? (where all attributes and $value are fields of variants)
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.