3Hren / msgpack-rust

MessagePack implementation for Rust / msgpack.org[Rust]
MIT License
1.11k stars 123 forks source link

Cannot deserialize the `serde_json::RawValue` #329

Open sargarass opened 1 year ago

sargarass commented 1 year ago

Hello!

The following code fails with deserialization not working: Syntax("invalid type: newtype struct, expected any valid JSON value") on the rmp_serde:1.1.1

#[test]
fn test() {
    let expected_value = r#"{"asd": "asd"}"#;

    let raw_value = serde_json::value::RawValue::from_string(expected_value.into()).unwrap();
    let bytes = rmp_serde::to_vec(&raw_value).unwrap();
    let raw_value: Box<RawValue> = rmp_serde::from_slice(&bytes).expect("deserialization not working");

    assert_eq!(raw_value.get(), expected_value);
}

I tried: 1) wrap it in Newtype: struct NewType(Box<RawValue>); 2) use struct NewType { inner: Box<RawValue>} with #[serde(flatten)] or #[serde(transparent)].

Nothing seems to work.

If I use serde_json::to_ver/from_slice, the test passes correctly.

#[test]
fn test() {
    let expected_value = r#"{"asd": "asd"}"#;

    let raw_value = serde_json::value::RawValue::from_string(r#"{"asd": "asd"}"#.into()).unwrap();
    let bytes = serde_json::to_vec(&raw_value).unwrap();
    let raw_value: Box<RawValue> = serde_json::from_slice(&bytes).expect("deserialization not working");

    assert_eq!(raw_value.get(), expected_value);
}
monoid commented 7 months ago

serde_json::RawValue has a hack inside it to be serialized/deserialized with serde_json, and cannot be used with other implementation (even with alternative JSON implementations).