So I have my own struct Daten
for which I can derive Serialize/Deserialize
, and there's target struct Dict
from typst (which also implements Serialize/Deserialize
). I need to convert my struct into the target struct. I can do so manually, but I'm investigating if using serde
is a good idea here. First, here's the (pretty trivial) code:
let d: Daten = ... // elided;
let s = serde_json::to_string(&d).unwrap();
let dd = serde_json::from_str(&s).unwrap();
Note that this works, no problem here.
- Do I need to go through a JSON string here? It seems pretty unneeded, an I tried understanding the documentation of
Serialize/Deserialize
to see how I could directly connect the two structs, but I could not figure this out. I've notedserde_transcode
which seems similar to what I want to do, but backwards. - There's 2 performance considerations I'd like to ask about:
2a . First, I have an ownedDaten
here, and I need an ownedDict
out of it. The manual conversion simply moves the contained values, which seems pretty efficient. Can I do something like this while running through serde?
2b. If not, all "leaf values" ofDaten
are pretty cheap to clone. Could I makeserde
do that? Or does this all neccessarily go through a display representation or something? - The main thing I'm interested here is avoiding implementation errors. The target
Dict
is pretty much aHashMap
, and when I manually stick in the fields of myDaten
struct, I might forget a field, or mistype the key. Usingderive
would make the much less error-prone. Is there another way to get this property, short of writing my own proc macro?
Thanks for any pointers.