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

Struct namespaces with Serde

Open Richterrettich opened this issue 5 years ago • 16 comments

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.

Richterrettich avatar Jun 22 '20 12:06 Richterrettich

I do not know how to handle this here, but I've used yaserde crate for such @Richterrettich

mlevkov avatar Jun 28 '20 08:06 mlevkov

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.

tafia avatar Jul 17 '20 03:07 tafia

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?

WhyNotHugo avatar Feb 18 '23 18:02 WhyNotHugo

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.

meghfossa avatar May 26 '23 03:05 meghfossa

No workaround if you want serde. I wrote my own parser with https://docs.rs/quick-xml/latest/quick_xml/reader/struct.NsReader.html

WhyNotHugo avatar May 26 '23 06:05 WhyNotHugo

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.

Mingun avatar May 26 '23 07:05 Mingun

Whoever picks this up, consider starting from https://github.com/tafia/quick-xml/pull/466

dralley avatar Jul 26 '23 16:07 dralley