json icon indicating copy to clipboard operation
json copied to clipboard

RawValue does not work inside an internally tagged enum

Open rklaehn opened this issue 4 years ago • 0 comments

This is similar, but not quite the same as https://github.com/serde-rs/json/issues/497. In this case it is a normally tagged enum.

Rust playground example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4d9015b013535f143aa5b8ad829be43c

Code:

use serde_json::value::RawValue;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct RequestBody {
    pub payload: Box<RawValue>,
}

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
enum Incoming {
    Request {
        payload: Box<RawValue>,
    },
}

fn main() {
    let foo = Incoming::Request {
        payload: serde_json::value::to_raw_value(&42).unwrap()
    };
    let txt = serde_json::to_string(&foo).unwrap();
    let roundtrip: Incoming = serde_json::from_str(&txt).unwrap();
}

rklaehn avatar May 21 '21 09:05 rklaehn