Simpler way to write serde custom deserialization?

I'm finding serde custom deserialization impls to be alot more error prone than I expect.

I'm wondering if there is a library that lets me do the following:

  1. Suppose we want to custom derive on type T

  2. I want to specify types U along with two functions

f :: T -> Result<U, String>
g :: U -> Result<T, String>
  1. U is serialized via standard derive macro.

  2. If serde comes across T during serialization, it calls f and uses that output.

  3. If serde comes across T during deserialization, it gets a U from the data, then calls g.

Is there a library that does this? The main here is that I'd rather write f and g rather than write all the visitor patterns.

You can do so within the body of Serialize::serialize and Deserialize::deserialize impl.

impl Serialize for T {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        f(self).map_err(S::Error::custom)?.serialize(s)
    }
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.