-
Notifications
You must be signed in to change notification settings - Fork 236
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
Cannot Deserialize a Serializable Enum #808
Comments
Basically, serde features that requires internal bufferisation, will not work with XML deserializer, because only XML deserializer knows how to convert strings from XML into other types (such as Serde features that require internal bufferisation and therefore not working with quick-xml deserializer:
The problems with bufferisation would be fixed when serde-rs/serde#1183 would be solved. But even it that problem would be solved, your untagged enum specifies variants in the wrong order. The You should avoid bufferisation, for example, try to use #[derive(Deserialize)]
struct IntermediateItem {
#[serde(rename = "@id")]
id: Option<i32>,
#[serde(rename = "$value")]
value: i32,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct IntermediateStruct {
#[serde(rename = "some_enum")]
some_enum: Vec<IntermediateItem>,
}
impl TryFrom<Vec<IntermediateItem>> for SomeEnum {
type Error = ...;
fn try_from(raw: Vec<IntermediateItem>) -> Result<Self, Self::Error> {
if let [first] = raw.as_slice() {
if first.id.is_none() {
return SomeEnum::SingleElement(SingleElement { value: first.value });
}
}
let vec: Result<Vec<_>> = raw.into_iter().map(|e| ListElement { id: e.id?, value: e.value }).collect();
SomeEnum::ListElement(vec)
}
}
impl From<IntermediateStruct> for SomeStruct {
fn from(raw: IntermediateStruct) -> Self {
Self { some_enum: raw.some_enum.map(Into::into).collect() }
}
} |
Great explanation! Maybe these paragraphs should be added to the docs? Currently untagged enums are not mentioned but only internal tagged enums. |
@Chaoses-Ib, feel free to submit a PR which would add things that, in your opinion, will improve understanding of untagged enums. |
Hello, thank you for creating and maintaining this library.
I am unsure if this is a skill issue on my part or if there is a problem with this library.
Basically, I am unable to deserialize an enum (but I can serialize it).
Even when I try to deserialize the serialized output, I get an error.
I am trying to parse XML in one of the following two forms:
id
)I have modelled them as follows:
Can Serialize Single Element Variant
which is
Cannot Deserialize Single Element Variant
Can Serialize List Element Variant
which is
Cannot Deserialize List Element Variant
Thank you for your help and support!
The text was updated successfully, but these errors were encountered: