When implementing a custom Rust serde Serialize implementation, is there any way to make the serialization depend on the capabilities of the format to which the structure is being serialized?
My specific use case is that I have a structure with file checksums, represented in memory as u8 arrays. When serializing to a text format like JSON, TOML, or YAML, I would like to serialize (and deserialize) them as hex-encoded strings. But when serializing to a binary format such as bincode or msgpack, I would like to serialize the u8 bytes directly.
Is this possible with Serialize, or do I need separate structures for text-encoded and binary-encoded digests and have client code convert types based on how they're serializing the structure?
I am aware of this question, but mine is slightly different (but may have the same ultimate answer): that question is concerned with overriding the derived serialize implementation for a specific serializer, and mine is concerned with serializer-specific behavior in a custom Serialize implementation.