3Hren / 3Hren/msgpack-rust

Struct with named fields can be deserialized from sequence

Open
#206 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
1.4k
Forks
162
PR merge metrics
No merged PRs in 30d

Description

This [issue in Serde](https://github.com/serde-rs/serde/issues/1587) affects Serde Msgpack.

Serde Msgpack unlike other format implementations has an option to the serializer to tell whether to serialize structs as a map with named fields or as a sequence without field names. The deserializer doesn't however have the corresponding option, but instead relies on this issue to accept both forms, which is usually not what is wanted, and comes with the unwanted consequences mentioned in the issue in Serde. Either the deserializer should have this option added, or even better, remove the option from the serializer and let the serialize implementation of the struct decide the format. The correct way to specify that a struct is going to be serialized without field names is in the serialize implementation of the struct. A container attribute can be added to Serde Derive for this purpose.

Example code adapted to Serde Msgpack:
```rust
use serde_derive::Deserialize;

#[derive(Debug, Deserialize)]
struct Person {
first_name: String,
last_name: String,
}

fn main() {
let data = rmp_serde::to_vec(&("John", "Doe")).unwrap();
eprintln!("{:?}", data);
eprintln!("{:#?}", rmp_serde::from_slice::(&data));
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.