quick-xml icon indicating copy to clipboard operation
quick-xml copied to clipboard

Document how to deserialize and serialize attributes using serde

Open eirnym opened this issue 1 year ago • 7 comments

There's a few examples here and there, I'd like to gather them to documentation, how idiomatically define an attribute using serde and this library as a parser and serializer.

eirnym avatar Sep 23 '24 22:09 eirnym

That documentation already exists. It may be not so visible, I'm open on suggestions how to improve that.

Mingun avatar Sep 24 '24 05:09 Mingun

To be honest this documented for Deserialization, which is also exists in an a quite obscure example. From documentation itself I'm not sure from documentation that this will work for Serialization. One doesn't strictly mean the other.

eirnym avatar Sep 24 '24 08:09 eirnym

It is located in de just because serde support is optional and we need to put that documentation somewhere under the feature flag. The main page does not say that it is only for deserialization:

Read more about mapping Rust types to XML in the documentation of de module.

se module does not have link to this documentation, however, this can be improved. PR is welcome. Some mention on attributes page also could be useful, because this page is what will be found by "attribute" query in rustdoc.

Mingun avatar Sep 24 '24 08:09 Mingun

When I open documentation, I see a quite small example for Serde and no other links whatsoever. I'd prefer to have documentation separation into specific documents, like for plain reader/writer you have and integration with Serde. Inside Serde probably there would be good to have separate documentation for (de)serialization if there's some specific details worth a separate page. Similar structure you can find in clap documentation.

eirnym avatar Sep 24 '24 15:09 eirnym

When I open documentation, I see a quite small example for Serde and no other links whatsoever.

This is readme file, the documentation starts here. Usually readme is not overloaded with links, but as I already said: I'm open for suggestions and PRs for improving documentation and making quick-xml better!

Mingun avatar Sep 24 '24 18:09 Mingun

Is it possible to deserialize a vector of attributes without having to explicitly name them? Example if I have this:

<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" another="bob">

Could I have a type like so?

#[derive(Default, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct A {
#[serde(rename = "@all-my-attrs")]
attrs: Vec<Attributes>,
}

tiagolobocastro avatar Mar 25 '25 17:03 tiagolobocastro

Yes, it is possible, just use any Map, flatten to your struct. Keys captured with @ in start is attributes:

#[test]
fn issue804() {
    #[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
    #[serde(rename_all = "PascalCase")]
    struct A {
        #[serde(flatten)]
        attrs: BTreeMap<String, String>,
    }

    let xml = r#"<A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" another="bob"/>"#;
    let result = quick_xml::de::from_str::<A>(xml);
    dbg!(&result);
    assert_eq!(
        result.unwrap(),
        A {
            attrs: [
                ("@another".to_string(), "bob".to_string()),
                ("@xmlns:xsi".to_string(), "http://www.w3.org/2001/XMLSchema-instance".to_string()),
            ].into_iter().collect()
        }
    );
}

Mingun avatar Mar 25 '25 19:03 Mingun