Skip to content

RawValues inside internally tagged enum #545

@hweom

Description

@hweom

This code

use serde::Deserialize;
use serde_json; // 1.0.39
use serde_json::value::RawValue;

#[derive(Deserialize, Debug)]
struct Struct<'a> {
    #[serde(borrow)]
    value: &'a RawValue,
}

#[derive(Deserialize, Debug)]
#[serde(tag = "a")]
enum Wrapper<'a> {
    #[serde(rename = "s")]
    S {
        #[serde(borrow)]
        data: Struct<'a>,
    },
}

fn main() {
    let message = r#"{
        "a": "s",
        "data": {
            "value": 123 
        }
    }"#;

    let s: Wrapper = serde_json::from_str(message).unwrap();
    println!("{:?}", s);
}

compiles and runs, but serde_json returns an error: invalid type: newtype struct, expected any valid JSON value

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=84bc58188c5625ce5bbabd6a44da205a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions