Skip to content

Serializing struct to JSON and then deserialing JSON back to struct fails. #1051

@jsdw

Description

@jsdw

In this example (playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=31652345ee158b7c7bbe31aa6c165c4f), I serialize some struct to JSON and then try to deserialize that JSON immediately back to the same struct. The serializing works as hoped, but I hit an error on deserializing. The code:

use serde_json::value::RawValue;

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Response {
    pub jsonrpc: String,
    pub id: String,
    #[serde(flatten)]
    pub body: ResponseBody
}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum ResponseBody {
    #[serde(rename = "result")]
    Result(Box<RawValue>)
}

fn main() {
    let res = Response {
        id: "123".into(),
        jsonrpc: "2.0".into(),
        body: ResponseBody::Result(RawValue::from_string("[1,2,3]".to_owned()).unwrap())
    };

    // This works as expected and looks good:
    let str =  serde_json::to_string_pretty(&res).unwrap();
    println!("{str}");

    // But trying to deserialize this straight back to the struct fails; bug?
    let _res: Response = serde_json::from_slice(
        str.as_bytes()
    ).expect("ok response");
}

The error I hit when I try to serde_json::from_slice is:

Error("invalid type: newtype struct, expected any valid JSON value", line: 5, column: 1)'

Quite possibly I'm just missing something, but I'd expect offhand that if I serialize some struct to JSON, it should be deserializable back into the struct again. Any pointers would be greatly appreciated :)

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