RReverser / serde-xml-rs

xml-rs based deserializer for Serde (compatible with 1.0+)
https://crates.io/crates/serde-xml-rs
MIT License
270 stars 90 forks source link

Boolean fields specified with integers always parse as true #41

Closed csssuf closed 7 years ago

csssuf commented 7 years ago

Boolean fields specified as integers always seem to parse as true, even with contents of 0:

#[macro_use] extern crate serde_derive;
extern crate serde_xml_rs;

#[derive(Debug, Deserialize)]
struct Structure {
    boolean_field: bool,
}

fn main() {
    let s = r##"
        <Structure boolean_field="0">
        </Structure>
    "##;
    let test: Structure = serde_xml_rs::from_reader(s.as_bytes()).unwrap();
    println!("{:#?}", test);
}

This test program outputs:

Structure {
    boolean_field: true
}
RReverser commented 7 years ago

This seems to be a duplicate of https://github.com/RReverser/serde-xml-rs/pull/18, please see my reasoning there.

oli-obk commented 7 years ago

Note that you can always use #[serde(deserialize_with=foo)] to overwrite the behaviour to match your use case. (See https://serde.rs/field-attrs.html for details on how to do that)