atamakahere
atamakahere

Reputation: 25

How can I deserialize a bincode serialised data into something like serde_json::Value?

I have a IPC implementation where a process serializes a struct using bincode

On the other end, I'm receiving it and lets say this process is not aware of the struct it is receiving, Here, I want to do something like

let parsed: Result<serde_json::Value, serde_json::Error> = serde_json::from_str(json_str);

but with bincode, is it possible?

I tried doing

let deser = bincode::deserialize::<serde_json::Value>(msg.bytes()))?;

But it throws error

Error: Bincode does not support the serde::Deserializer::deserialize_any method

Upvotes: 2

Views: 796

Answers (1)

cafce25
cafce25

Reputation: 27532

You can't because the bincode deserializer has to be informed about the type to expect, but serde_json::Value relies on the deserializer to tell it what type is next. Both requirements contradict each other. That's also expressed in the documentation of deserialize_any:

When implementing Deserialize, you should avoid relying on Deserializer::deserialize_any unless you need to be told by the Deserializer what type is in the input. Know that relying on Deserializer::deserialize_any means your data type will be able to deserialize from self-describing formats only, ruling out Postcard and many others.

bincode is one of those formats that do not describe itself.

Upvotes: 6

Related Questions