RReverser / serde-xml-rs

xml-rs based deserializer for Serde (compatible with 1.0+)
https://crates.io/crates/serde-xml-rs
MIT License
270 stars 90 forks source link

Deserialize "$value" as String or Vec<SomeStruct> based on attributes #69

Open futile opened 6 years ago

futile commented 6 years ago

I have data which can look like either:

<data encoding="XML">
  <tile gid="0"/>
  <tile gid="1"/>
</data>

or

<data encoding="base64">
  aaabbbbbbbcc
</data>

And, as far as I can see, this is not (yet) possible to deserialize. With the old serde-xml I was able to get a serde_xml::Value, which was an enum that could be, e.g., Value::Element(...) or Value::Content(...), which made it possible to handle them individually. However, I have no Idea how I would do that currently.

The regular Deserializer/Visitor pattern seems to fail here, as I need to dispatch based on what is actually inside the XML, and don't known that beforehand.

Edit: Something that looked like it should work, but didn't. It worked for the string case, but not for the XML-case (cant match any variant-error):

#[derive(Debug, Deserialize)]
#[serde(rename="tile")]
struct Tile {
  gid: u32
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum DataContent {
  Str(String),
  Tiles(Vec<Tile>)
}

#[derive(Debug, Deserialize)]
struct DataImpl {
  encoding: Option<String>,
  compression: Option<String>,
  #[serde(rename="$value")]
  value: DataContent,
};

Inlining either of the variants (String or Vec) into DataImpl actually makes it work for that case (but obviously not for the other).

All help is appreciated!

NuSkooler commented 8 months ago

@futile did you find an answer to this?

futile commented 8 months ago

@futile did you find an answer to this?

Nope, don't think I ever found a solution. But, given that the last commit to this repo was ~2 years ago, I would assume that there exist better options for XML-parsing (maybe without serde) for Rust by now. Good luck! :)

NuSkooler commented 8 months ago

@futile Thanks for your response on this! Sort of what I was afraid of... think I'll try quick_xml.