quick-xml
quick-xml copied to clipboard
Struct namespaces with Serde
Hi, it would be really cool to have a feature to define common namespaces for structs using serde. Something like this:
#[derive(Serialize, Deserialize)]
#[namespace(F,foourn)]
struct Foo {
id: String,
#[serde(flatten)]
#[namespace(B,barurn)]
bar: Bar
}
#[derive(Serialize, Deserialize)]
struct Bar {
name: String,
desc: String
}
resulting in:
<F:foo xmlns:B="foourn" xmlns:F="barurn">
<F:id>123</F:id>
<B:name>asdf</B:name>
<B:desc>foobar </B:desc>
</F:foo>
Background
I sometimes face (old) API's that requrie XML to be structured in a way similar to the given example. They use namespaces to distinguish between entities to form some sort of inheritance tree.
A feature like this would make interacting with these kind of API's very easy.
I do not know how to handle this here, but I've used yaserde crate for such @Richterrettich
This would be a nice feature indeed. Unfortunately I don't have much time but I'd be happy to integrate it if someone finds the time to do it.
I'd love to see this happen, but I've no idea where to even start. What kind of macro would namespace be in order for its data to somehow be accessible in the serde (de)serialization stage?
Is there any workaround for this, or : is not supported at all. I don't mind if the workaround approach is verbose or unintuitive. Right now, I can't parse any field with : in it's name.
No workaround if you want serde. I wrote my own parser with https://docs.rs/quick-xml/latest/quick_xml/reader/struct.NsReader.html
If you don't want to write deserialization code manually, you could look at xmlserde. Support for namespaces for serde is not an easy task, unfortunately.
Whoever picks this up, consider starting from https://github.com/tafia/quick-xml/pull/466