-
Notifications
You must be signed in to change notification settings - Fork 606
Closed
Description
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
Labels
No labels