You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a relatively simple XML to serialize and deserialize (a subset of https://en.wikipedia.org/wiki/XML-RPC):
The following code does properly serialize an Object into XML, but the deserialization of the serialized object itself doesn't work.
I'm using:
[[package]]
name = "quick-xml"version = "0.30.0"
Any hint?
use serde::{Serialize,Deserialize};#[derive(Serialize,Deserialize)]pubstructMethodResponse{pubparams:Params,}#[derive(Serialize,Deserialize)]pubstructParams{pubparam:Vec<Param>,}#[derive(Serialize,Deserialize,Clone)]pubstructParam{pubvalue:ValueElement,}#[derive(Serialize,Deserialize,Clone)]pubstructValueElement{pubv:Value,}#[derive(Serialize,Deserialize,Clone)]pubenumValue{#[serde(rename = "int")]Int(i32),#[serde(rename = "double")]Double(f64),#[serde(rename = "bool")]Boolean(bool),#[serde(rename = "string")]String(String)}
let obj = MethodResponse{params:Params{param:{vec![Param{ value:ValueElement{ v:Value::String("MyStringValue".to_string())}},Param{ value:ValueElement{ v:Value::Boolean(true)}},Param{ value:ValueElement{ v:Value::Int(66)}},Param{ value:ValueElement{ v:Value::Double(1.2)}},]}}};let xml = quick_xml::se::to_string(&obj).unwrap();println!("{}", xml);let object:MethodResponse = quick_xml::de::from_str(&xml).unwrap();
The serialized XML looks ok to me, and it's correctly printed:
But the deserialization on the other hand doesn't work and the program panics:
thread 'xmlrpc::response::tests::deserialize_should_succeed' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("missing field `v`")', src\xmlrpc\response.rs:35:68
stack backtrace:
The text was updated successfully, but these errors were encountered:
You should add #[serde(rename = "$value")] to pub v: Value, because deserializer should known to which field it should redirect different tags (<string>, <bool>, etc.) and that field is a $value field. I think, we can emphasise that in the documentation something here.
Also, you should add #[serde(default)] to pub param: Vec<Param>, otherwise you'll get an error in attempt to deserialize a response with no parameters.
Also, note, that examples in https://docs.rs/quick-xml/latest/quick_xml/de/index.html#value are not correct. They are not tested and I've found that while working on fix for #630. Currently serialization without renames didn't produce <field/>, but actual enum variant tags, but deserialization didn't work.
You should add #[serde(rename = "$value")] to pub v: Value, because deserializer should known to which field it should redirect different tags (<string>, <bool>, etc.) and that field is a $value field. I think, we can emphasise that in the documentation something here.
Also, you should add #[serde(default)] to pub param: Vec<Param>, otherwise you'll get an error in attempt to deserialize a response with no parameters.
Also, note, that examples in https://docs.rs/quick-xml/latest/quick_xml/de/index.html#value are not correct. They are not tested and I've found that while working on fix for #630. Currently serialization without renames didn't produce <field/>, but actual enum variant tags, but deserialization didn't work.
Hey @Mingun, many thanks for the fast response! Indeed your suggestions helped 🙏
Indeed the $value feature is not very simple to understand from the documentation, on the other hand probably you kind of assume some "serde" knowledge, which unfortunately I don't yet(?) have.
I have a relatively simple XML to serialize and deserialize (a subset of https://en.wikipedia.org/wiki/XML-RPC):
The following code does properly serialize an Object into XML, but the deserialization of the serialized object itself doesn't work.
I'm using:
Any hint?
The serialized XML looks ok to me, and it's correctly printed:
But the deserialization on the other hand doesn't work and the program panics:
The text was updated successfully, but these errors were encountered: