-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the representation of Enums #717
Comments
You can find answer in this chapter about mapping of Rust types to XML. TL;DR: your enum variant is not a unit variant and it cannot be serialized in arbitrary-named field of struct. |
Then why does it work for It forces me to use a feature flag in compile time - just to produce a little different code for Is there any way to show struct AnyName {
#[serde(rename = "$value")]
any_name: Choice,
} |
But still, even when using |
It is hard to answer to your questions because you do not provide your expectations. The mentioned piece of documentation shows how quick-xml performs mapping in a consistent manner. If you have concrete suggestions, please describe them and even better open a PR with them! |
My expectations:
Just like JSON:
|
Issue should be reopened - other crates support enum lists. |
You feel free to submit PR that would implement the desired behavior and make it in the consistent way. Probably this is possible. We also should keep the ability to use tag name as enum discriminator, because this is natural way how |
If we accept that quick-xml does not do this - is the only way to get around this to write a custom serializer/deserializer with kinda "hardcoding" the variants? Do you see any obstacles with that approach? |
@flotang-gtt I think you can get around it with this response on StackOverflow, I am working to try it out now: https://stackoverflow.com/questions/78444158/unsupportedcannot-serialize-enum-newtype-variant-exampledata 😊 |
@flotang-gtt i managed to get stuff working with struct wrappers like so: #[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct FooWrapper {
#[serde(rename = "$value")]
pub foo: Foo,
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum Foo {
#[serde(rename = "tag:Point", alias = "Point")]
Point(Point),
#[serde(rename = "tag:LineString", alias = "LineString")]
LineString(LineString),
} Where i then have #[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename = "tag:Baz")]
pub struct Baz {
#[serde(rename = "tag:Foo", alias = "Foo")]
pub foo: FooWrapper,
} So here the wrappers can handle the enum variant where we then use |
serde_json supports enums:
"field": "Unit",
"field": { "Newtype": 42 },
"field": { "Tuple": [42, "answer"] },
"field": { "Struct": {"q": 42, "a":"answer"} },
https://docs.rs/quick-xml/latest/quick_xml/de/index.html#normal-enum-variant
so quick-xml should too:
<field>Unit</field>
<field><Newtype>42</Newtype></field>
<field><Tuple>42</Tuple><Tuple>answer</Tuple></field>
<field><Struct><q>42</q><a>answer</a></Struct></field>
Currently the above XML won't be generated (gives an error) while JSON would even parse back into Rust enum.
Unsupported operation: cannot serialize enum newtype variant
Serialization of enum - crate comparison:
The text was updated successfully, but these errors were encountered: