Failed to parse XML with an optional element with serde-xml-rs

╄→гoц情女王★ 提交于 2019-12-01 18:18:06

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!