msgpack-rust: Error when deserializing internally-tagged enum
Given an internally-tagged enum with NewType variants, serializing produces a valid value but deserializing fails. For example:
#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
enum TestEnum {
Foo(TestType),
Bar(TestType),
}
#[derive(Serialize, Deserialize)]
struct TestType {
inner: u32,
}
let test_enum1 = TestEnum::Foo(TestType{inner: 123});
let serialized = rmp_serde::to_vec(&test_enum1).unwrap();
let test_enum2 = rmp_serde::from_slice(&serialized).unwrap();
The serializer produces [146, 163, 70, 111, 111, 123]
, equivalent to the JSON value ["Foo", 123]
. That’s the value I was expecting.
The deserializer fails with the error Syntax("invalid type: sequence, expected any value")
.
Everything works if I remove the line #[serde(tag = "type")]
, but then the serializer produces [146, 0, 145, 145, 123]
, equivalent to the JSON value [0, [[123]]]
. If possible I’d prefer to use the internally tagged representation.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 25 (16 by maintainers)
Commits related to this issue
- Add tests for #130 adjacently tagged enums serialization/deserialization works with sequence deserialization. internally tagged enums deserialization fails with array serialization (it should fail as... — committed to SX91/msgpack-rust by SX91 7 years ago
- chore: add tests for #130 (#145) Adjacently tagged enums serialization/deserialization works with sequence deserialization. Internally tagged enums deserialization fails with array serialization (it... — committed to 3Hren/msgpack-rust by SX91 7 years ago
I’m closing this issue because it appears to be fixed by #146
It’s a link. https://play.rust-lang.org/?gist=3c0891feff3526480b78943dd6dda8da&version=stable
The error does not appear when using serde_json.