bincode icon indicating copy to clipboard operation
bincode copied to clipboard

Problem deserializing tagged enum (serde)

Open jbrosi opened this issue 2 years ago • 5 comments

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 DeserializeAnyNotSupportedwhen 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.

jbrosi avatar Jun 03 '22 07:06 jbrosi

Looks like we have a new entry for the known issue list with serde, where this will just not work with bincode.

VictorKoenders avatar Jun 03 '22 07:06 VictorKoenders

Any explanation on why tags cannot work with bincode? Thanks.

huntc avatar Jun 09 '22 08:06 huntc

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.

ZoeyR avatar Jun 09 '22 14:06 ZoeyR

any solutions to this?

yottacto avatar Sep 05 '22 05:09 yottacto

No, this is a fundamental incompatibility between serde and data formats that do not have metadata

VictorKoenders avatar Sep 05 '22 05:09 VictorKoenders