Proper way to build a serde Deserializer wrapper

I have a set of JSON files with keys obfuscated, and could obtain a mapping to deobfuscate at runtime. Since serde's derive(deserialize) supports only compile-time rename = "...", the Deserialize trait is stateless, and DeserializeSeed is not quite composable, I decide to write a generic deserializer wrapper to match and deobfuscate mapping keys at runtime.

This is harder than I initially thought. After spending hours with ownership, lifetime, Visitor and MapAccess, etc, I still could not achieve my goal. Currently, I took a simpler approach, by modifying upon serde_transcode (which implement Serialize and Deserialize on the Transcode struct in a smart way), to deobfuscate the keys first, produce an new JSON string, then deserialize into structs.

I wonder, is there a minimal example of wrapping a Deserializer (and also Visitor, MapAccess). I saw serde_with did something similar, but not dig into the details yet.

Yes, there are some in serde_with. For example the one used by the implementation of with_prefix!. Other crate I know are based on a Deserializer adapter include clang-ast, serde_ignored, serde_path_to_error, and serde_stacker.

1 Like