serde-deprecated / xml

No longer maintained
35 stars 9 forks source link

Out of order list of tags deserialization error #33

Closed indiv0 closed 7 years ago

indiv0 commented 7 years ago

So I basically have some XML in the form:

<states>
  <state />
  <statelist>
    <state />
    <state />
  </statelist>
  <state />
</states>

which fails to deserialize because the tags go state, statelist, state.

However it deserializes fine if the two state tags come first:

<states>
  <state />
  <state />
  <statelist>
    <state />
    <state />
  </statelist>
</states>
oli-obk commented 7 years ago

To what rust code do you expect it to deserialize? I have no clue how to represent interleaved sequences.

oli-obk commented 7 years ago

See #26 for a similar/the same case

indiv0 commented 7 years ago

I expect/prefer for it to deserialize to:

struct States {
    state: Vec<State>,
    statelist: Statelist,
}

struct Statelist {
    state: Vec<State>,
}

i.e., merge the sequences into a single Vec, as if all the elements came in order.

oli-obk commented 7 years ago

There can't be multiple state lists?

This is very hard to do, I'll think about it, but it'll require some intermediate allocations.

The easiest would be to manually implement deserialize for this special case

indiv0 commented 7 years ago

There can be multiple statelists, but I simplified it for the purposes of illustrating the example. Here's the full version of what I want.

Let me know what you've decided when you've finished thinking about it. If it's indeed too hard to handle then I guess I can write a custom deserializer.

oli-obk commented 7 years ago

The serde deserialization framework does not fit your use case with autogenerated deserialize impls. There's no need for a custom deserializer, since that won't help. You need to implement Deserialize manually for States.

I'd still say that the solution from #26 is cleaner, but I'm not sure on the usability of the struct and enum

indiv0 commented 7 years ago

Yeah that's what I meant by "custom deserializer". Ok I'll look into it, thanks.

indiv0 commented 7 years ago

Actually, I don't think I can use the solution in #26 because my elements don't specify an xsi:type. Is there a way to deserialize those elements to an enum just on their name alone?

oli-obk commented 7 years ago

Yea, you need to write a MapVisitor to get access to the names

oli-obk commented 7 years ago

Similar to the generic one here you can simply call a KeyDeserializer to get the name

indiv0 commented 7 years ago

Well for what's its worth I ended up implementing Deserialize manually and leaving the struct itself as-is. It seems to work. Thank you for your help. I guess this issue can be closed now.