I try to implement my own custom Serde deserializer. I made some progress with simple data types, but struggle to get deserialize_struct working. The signature looks like
which is deserialized as part of a HashMap<String, FakeA>. When hitting the above trait method all the parameters look good to me. V::Value is actually FakeA. But how do I create an instance? The Json-example in the docs delegates to deserialize_map which is not helpful for me. I tried
V::Value::deserialize(self)
but this does not work, because V::Value is not required to implement Deserialize.
Based on my Serde experiences so far, I expect that I miss some additional layer of indirection. Any hint in the right direction would be highly appreciated.
All you know from the signature is that V implements Visitor, so you might want to take a look at what that trait provides to you: Visitor in serde::de - Rust
The exact approach may vary based on what your data format looks like. But most likely you will have an auxiliary struct that implements MapAccess, which you pass to visitor.visit_map. It might be the same as you use in deserialize_map, but it doesn't have to be.
Maybe you check for the struct type name first, maybe you don't; depends on the format.
Maybe your auxiliary struct relies on the given field names, maybe it doesn't; depends on the format.
In the example,CommaSeparated is the auxiliary struct, and is the same as deserialize_map because the implementation doesn't care about the struct type name or the exact field names. It's up to the type being deserialized to error or ignore unknown fields and to error or have some fallback for missing fields.
When I managed to understand serde, I read the serde sourde code together with the code generated by the serde_json for several (10-15?) different types I defined.
You can use cargo expand, support by e.g. VS code to generate this code.
Then, you can swap the generated code in and use a debugger with break points to litteraly step through the code.