media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
174 stars 58 forks source link

Empty strings not correctly deserialized #178

Open JonathanBrouwer opened 5 months ago

JonathanBrouwer commented 5 months ago

The following example fails:

#[derive(YaSerialize, YaDeserialize)]
pub struct Test {
    v: String
}

#[test]
fn test() {
    let v = Test {
        v: "".to_string()
    };

    yaserde::de::from_str::<Test>(&dbg!(yaserde::ser::to_string(&v).unwrap())).unwrap();
}

The struct is correctly serialized to:

<?xml version=\"1.0\" encoding=\"utf-8\"?><Test><v></v></Test>

The error message is "v is a required field of Test". The problem occurs because the visit_str function is not called since there is no string.

MarcAntoine-Arnaud commented 2 months ago

In fact xml-rs does not handle event XmlEvent::Characters.

So only a StartElement and EndElement are emitted.

So specific code has to be done for that case, without breaking the rest :P