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

后端 未结 1 1391
小蘑菇
小蘑菇 2021-01-18 17:27

I have a tree of serde-annotated structs and it succeeds in parsing the sample XML, including this fragment:



        
相关标签:
1条回答
  • 2021-01-18 18:28

    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.

    0 讨论(0)
提交回复
热议问题