3Hren / 3Hren/msgpack-rust

Can't handle #[serde(tag = "someKey")] for structure while Deserialization

Open
#318 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.4k
Forks
162
PR merge metrics
No merged PRs in 30d

Description

```
use rmp_serde::Serializer;
use serde::Serialize;

extern crate rmp_serde;
extern crate serde;
#[macro_use]
extern crate serde_derive;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
struct A {
b: B,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "kind", content = "data")]
#[serde(rename_all = "snake_case")]
pub enum B {
First(i32),
Second(Messagedata)
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "someKey")]
pub struct Messagedata {
status: String,
message: String,
}

fn main() {
let a = A {
b: B::Second (Messagedata { status: "Failure".to_string(), message: "You are not getting the things".to_string() }),
};
//SERDE
let string_data = serde_json::to_string(&a).unwrap();
println!("data: {:?}", string_data);
let data = serde_json::from_str::(&string_data).unwrap();
println!("A: {:?}", data);
println!("-------------------------------------------------------------------------------");

//MESSAGEPACK
let mut buf = Vec::new();
a.serialize(&mut Serializer::new(&mut buf)).unwrap();
// println!("a: {:?}", buf);
let a = rmp_serde::from_slice::
(&buf).unwrap();
println!("a: {:?}", a);
}

```

Here is error I am getting:

```
data: "{\"b\":{\"kind\":\"second\",\"data\":{\"someKey\":\"Messagedata\",\"status\":\"Failure\",\"message\":\"You are not getting the things\"}}}"
A: A { b: Second(Messagedata { status: "Failure", message: "You are not getting the things" }) }
-------------------------------------------------------------------------------
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: LengthMismatch(2)', src/main.rs:45:46
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

There is no problem with `serializing` and `deserializing` with `serde`. With `rmp_serde` there is no problem in `serializing` a `struct` with a given tag but while deserializing it is not able to take `tag` provided as key and giving length error.

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.