media-io / yaserde

Yet Another Serializer/Deserializer
MIT License
179 stars 60 forks source link

Custom serializer with fields from derive #98

Open kaedwen opened 3 years ago

kaedwen commented 3 years ago

I want to write a custom serializer for a wrapped chrono::NaiveDate, but I want to serialize with the rename given in the derive.

impl YaSerialize for NaiveDate {
    fn serialize<W: Write>(&self, writer: &mut yaserde::ser::Serializer<W>) -> Result<(), String> {
        let _ret = writer.write(xml::writer::XmlEvent::characters(self.to_string().as_str()));
        Ok(())
    }
    fn serialize_attributes(
        &self,
        attributes: Vec<xml::attribute::OwnedAttribute>,
        namespace: xml::namespace::Namespace,
    ) -> Result<
        (
            Vec<xml::attribute::OwnedAttribute>,
            xml::namespace::Namespace,
        ),
        String,
    > {
        Ok((attributes, namespace))
    }
}

Is there any chance to write the default start_element given in a derive?