quick-xml: Option doesn't (de)serialize properly with serde
Using the following code:
use serde;
use quick_xml;
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Player {
spawn_forced: Option<bool>
}
fn main() {
let data = Player {
spawn_forced: None,
};
let mut deserialize_buffer = String::new();
quick_xml::se::to_writer(&mut deserialize_buffer, &data).unwrap();
println!("{}", &deserialize_buffer);
quick_xml::de::from_reader::<&[u8], Player>(&deserialize_buffer.as_bytes())
.unwrap();
}
The code crashes during the deserialize step
[dalley@thinkpad playground]$ cargo run
Compiling playground v0.1.0 (/home/dalley/Devel/practice/playground)
Finished dev [unoptimized + debuginfo] target(s) in 0.28s
Running `target/debug/playground`
<Player><spawn_forced/></Player>
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidBoolean("")', src/main.rs:98:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
If instead spawn_forced is set to Some(true) or Some(false), it works fine. Only None, which serializes to <spawn_forced/>, panics upon deserialization.
[dalley@thinkpad playground]$ cargo run
Compiling playground v0.1.0 (/home/dalley/Devel/practice/playground)
Finished dev [unoptimized + debuginfo] target(s) in 0.44s
Running `target/debug/playground`
<Player><spawn_forced>true</spawn_forced></Player>
[dalley@thinkpad playground]$ cargo run
Compiling playground v0.1.0 (/home/dalley/Devel/practice/playground)
Finished dev [unoptimized + debuginfo] target(s) in 0.28s
Running `target/debug/playground`
<Player><spawn_forced>false</spawn_forced></Player>
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16
Commits related to this issue
- Properly use `xsi:nil` to deserialize null values via serde This commit fixes an issue that causes `quick-xml` trying to deserialize empty tags via the serde interface even if these tags were explici... — committed to GiGainfosystems/quick-xml by weiznich 2 years ago
- Properly use `xsi:nil` to deserialize null values via serde This commit fixes an issue that causes `quick-xml` trying to deserialize empty tags via the serde interface even if these tags were explici... — committed to GiGainfosystems/quick-xml by weiznich 2 years ago
- Properly use `xsi:nil` to deserialize null values via serde This commit fixes an issue that causes `quick-xml` trying to deserialize empty tags via the serde interface even if these tags were explici... — committed to GiGainfosystems/quick-xml by weiznich 2 years ago
- Add regression tests for #497 — committed to Mingun/quick-xml by Mingun 2 years ago
- Properly use `xsi:nil` to deserialize null values via serde This commit fixes an issue that causes `quick-xml` trying to deserialize empty tags via the serde interface even if these tags were explici... — committed to Mingun/quick-xml by weiznich 2 years ago
- Properly use `xsi:nil` to deserialize null values via serde This commit fixes an issue that causes `quick-xml` trying to deserialize empty tags via the serde interface even if these tags were explici... — committed to Mingun/quick-xml by weiznich 2 years ago
I agree with the round-trips
rust -> xml -> rust. This is also precisely the use-case of why I started to usequick-xmlbut also why I stopped when I found out that this behaviour is not supported by default. The solution to annotate with serde options was also not good for me since I want to support multiple serialization formats for my users and these annotations might interfere with other working approaches.However this is just my feedback. My current solution has been to deprecate xml for my application but I would like to introduce it again later if possible.
Hello, I had a similar issue and would like to share my solution if it is of help to anyone in the future.
I wanted an element like
<some_boolean_flag/>to appear whentrueand not appear whenfalse. I assume something like this was the original poster was intent with usingOption<bool>.Example
Initial Content:
Deserialized Struct:
Reserialized XML:
Initial Content:
Deserialized Struct:
Reserialized XML:
Code
This bug is not specific to
bool– anyOption<T>fields that deserializing from empty element, are affecting. Let’s look at such XMLs:While result for (1) and (3) are pretty obvious, that is hard to say for (2). The JSON equivalent of that structs is
How we should map the case (2)?
serde_jsonmaps it toNonealways. That means, that, for example,Option<()>and anyOption<UnitStruct>are always deserialized asNone, while they are serialized into"value": null.quick-xml currently does the opposite: it always deserializes to
Some(...). It is a bit tricky to handle this similar to JSON, because ourDeserializer::deserialize_optiondoes not advance event pointer, so the type underOptionhas access to anEvent::Start. We only lookahead to one event, butDeserializer::deserialize_optionrequires lookahead for 2 events: we need to look atEvent::Textin order to see that it is empty and we need to returnNone. TheEvent::Textgenerated, because<element/>is represented as<element></element>for a deserializer and we want no difference in handling between that two representations.Fixing that likely will have a consequence, that
Option<&str>andOption<String>will never be deserialized toSome("").