bincode
bincode copied to clipboard
Problem deserializing tagged enum (serde)
Hey there, I have the following code:
use serde::{Deserialize, Serialize};
fn main() {
#[derive(Debug, Serialize, Deserialize)]
struct Struct {
a: u8,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "version")]
enum Test {
Variant(Struct),
}
let data = Test::Variant(Struct { a: 7 });
let serialized = bincode2::serialize(&data).unwrap();
let deserialized: Test = bincode2::deserialize(&serialized).unwrap();
}
The deserialize fails with DeserializeAnyNotSupported
when I have the #[serde(tag="version")]
enabled like above. If i remove it, it does work. Serialize always works and seems to put additional info when tagging is enabled. Couldn't find any hints on how to solve this and this does not seem to be known issue (https://docs.rs/bincode/2.0.0-alpha.1/bincode/serde/index.html#known-issues).
The behavior is the same for bincode 2.0.1 and 1.3.3.
Looks like we have a new entry for the known issue list with serde, where this will just not work with bincode.
Any explanation on why tags cannot work with bincode? Thanks.
Its because the serde codegen when using tags calls unsupported methods in bincode. Specifically the deserialize_any
function. Bincode is not a self-describing format and it can't figure out the shape of data from the data itself.
any solutions to this?
No, this is a fundamental incompatibility between serde and data formats that do not have metadata