Hello There,
I am currently parsing JSON from a SingalR WebSocket with serde and serde_json.
But I am unable to correctly parse the Update Messages.
Here is a minimal playground with two types up update/messages.
I know of the tagging and representation of enums.
But when I use untagged, I get miss matches of the variant. Because the data structure starts the same as another topic but doesn't end the same and serde takes the wrong one.
I think I need something like "Adjacently tagged enums", but in an vec or tuple. Also I need the timestamp, it can't be ignored.
It's possible to use untagged if you make the Message itself an enum, and use one-variant enums as markers. Check out this playground. Unfortunately, this way the timestamp is not "extracted to the top level" of the message, but perhaps that's not a big deal.
You can make a simple manual deserialize impl to rewrap to the nicer enum. Playground.
Serde untagged is kinda slow (ok, in this case there's max one token to "rewind" and only two variants to check so it won't cause slowdown I guess. Although the quality of errors with untagged are still a reason to avoid untagged :)), so an alternative could be to stash the "yet unknown" update into RawValue and manually dispatch on the first element of the tuple. Playground. (Ugh, the idea seemed simple, but the error convertion logic around RawValue turned out ugly. Anyway).
Or you could write a fully manual impl with a visitor (which I guess you wanted to avoid :)) Playground (with a bit of help of Chat GPT – didn't know about the SeqAccess and next_elem)
Thank you for your response! The first variant might be the best way, will this have any negative impact on performance when I have about 10 Topics more?
And yes the fully manual implementation is what I wanted to avoid, as I though it should be possible without it somehow.
As you can see both Updates have the same Hashmap with driver numbers but the content of those fields have different content. This is still a simple example the real data structures are more nested.