You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use serde_json::value::RawValue;#[derive(Debug,Clone, serde::Deserialize, serde::Serialize)]pubstructResponse{pubjsonrpc:String,pubid:String,#[serde(flatten)]pubbody:ResponseBody}#[derive(Debug,Clone, serde::Deserialize, serde::Serialize)]pubenumResponseBody{#[serde(rename = "result")]Result(Box<RawValue>)}fnmain(){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:
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 :)
The text was updated successfully, but these errors were encountered:
You are not going to be able to deserialize a RawValue inside of flatten. Flattened content is collected into intermediate data structures while the rest of the outer data structure is deserialized, so it's no longer associated with any of the original underlying JSON formatting for the RawValue's contents to mirror.
You can see why this happens by considering the following example:
The "correct" value for value would be a JSON object containing "a":1 and "b":2, and no "id". This is what you have if value were serde_json::Value. But such a raw object does not appear anywhere in the input, so there can't exist a RawValue with that value.
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:
The error I hit when I try to
serde_json::from_slice
is: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 :)
The text was updated successfully, but these errors were encountered: