Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot deserialize the serde_json::RawValue #329

Open
sargarass opened this issue May 23, 2023 · 1 comment
Open

Cannot deserialize the serde_json::RawValue #329

sargarass opened this issue May 23, 2023 · 1 comment

Comments

@sargarass
Copy link

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
Copy link

monoid commented Dec 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants