3Hren/msgpack-rust

Cannot deserialize the `serde_json::RawValue`

sargarass opened this issue · 2 comments

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);
}

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).

Is it planned / doable to make a RawValue type specifically for serde_rmp to deserialize only one part of a payload or to pre-serialize some data ?