Using a different serialization method for a particular feild based on some condition


I am working on library. It was encoding an object

pub struct obj {
    pub appliance_id: u32,
    pub last: bool,
    pub path: PathBuf,

And, previously the encoding for invalid utf-8 was not handled. So, I have written a separate method to serialize path

pub fn serialize<S>(p: &Arc<PathBuf>, serializer: S) -> Result<S::Ok, S::Error>
        S: Serializer,
        let b = p.as_os_str().as_bytes();

What I want is that if the path is valid utf-8 than it should serilize with default serialiser for pathbuf, which is by converting it to str and than serialize it. And, In case of invalid utf-8 it should fallback to the new method written for serialize path.
And same should work for deserializer.

Thanks in Advance!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.