Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum encoding discrepancy between rmp-serde and rmpv serde implementations #327

Open
wuggen opened this issue Mar 28, 2023 · 0 comments
Open

Comments

@wuggen
Copy link

wuggen commented Mar 28, 2023

The serde implementation in rmp-serde encodes enum variants as a single-element mapping with the variant's name as a key; in contrast, rmpv's serde implementation encodes enum variants as a two-element array with the variant index as the first element.

Example:

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub enum Foo {
    Bar(String),
}

fn main() {
    let bar = Foo::Bar(String::from("Heya lol"));
    let encoded_bin = rmp_serde::encode::to_vec(&bar).unwrap();
    for b in &encoded_bin {
        print!("{b:02x}");
    }
    println!();

    let decoded_val = rmpv::decode::read_value::<&[u8]>(&mut encoded_bin.as_ref()).unwrap();
    println!("{decoded_val:?}");

    let encoded_val = rmpv::ext::to_value(&bar).unwrap();
    println!("{encoded_val:?}");
}

The above code outputs the following:

81a3426172a848657961206c6f6c
Map([(String(Utf8String { s: Ok("Bar") }), String(Utf8String { s: Ok("Heya lol") }))])
Array([Integer(PosInt(0)), Array([String(Utf8String { s: Ok("Heya lol") })])])

Either of these encodings is reasonable on its own, but it would seem that the two implementations should agree by default.

Relatedly, as noted in #323, the documentation incorrectly states that the default configuration in rmp-serde serializes enum variants as integer indices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant