问题
I have a tree of serde-annotated structs and it succeeds in parsing the sample XML, including this fragment:
<bmsg>
<cmsg>
<!-- ... -->
<cmsg>
<bmsg>
Now I am testing with a large sample XML file and the following structs fail because sometimes <cmsg>..</cmsg>
is missing. I was deserializing this using:
#[derive(Serialize,Deserialize, Debug)]
struct A {
#[serde(rename="bmsg")]
messages: B, // <====
}
#[derive(Serialize,Deserialize, Debug)]
struct B { // bmsg
#[serde(rename="cmsg")]
list: Vec<C>,
}
Which resulted in an error in the second struct:
panicked at 'called `Result::unwrap()` on an `Err` value: missing field `cmsg`
I changed the first struct to have a Vec<>
so it can deal with an optional element:
#[derive(Serialize,Deserialize, Debug)]
struct A {
#[serde(rename="bmsg")]
messages: Vec<B>, // <====
}
#[derive(Serialize,Deserialize, Debug)]
struct B { // bmsg
#[serde(rename="cmsg")]
list: Vec<C>,
}
But serde continues to give the same error. I tried Option<>
too, but didn't get anywhere.
What baffles me the most is that I use Vec<>
all over the place and never ran into this problem.
回答1:
It would appear Option<T>
means that the item does exist, it just is void of content.
The documentation seems to suggest using the default
attribute, to tell the deserializer to use the implementation of the Default trait for the type if it cannot be found.
With that in mind, perhaps this would work for you:
#[derive(Serialize,Deserialize, Debug)]
struct A {
#[serde(rename = "bmsg")]
messages: B,
}
#[derive(Serialize,Deserialize, Debug)]
struct B { // bmsg
#[serde(rename = "cmsg", default)] // <----- use default to call `Default::default()` against this vector
list: Vec<C>,
}
You can find the code I used to check this in the Playground. It won't run in the Playground, but it produces your expected results running locally.
来源:https://stackoverflow.com/questions/46585332/failed-to-parse-xml-with-an-optional-element-with-serde-xml-rs