Is there a way to deserialize arbitrary JSON using Serde without creating fine-grained objects? Is there a way to deserialize arbitrary JSON using Serde without creating fine-grained objects? json json

Is there a way to deserialize arbitrary JSON using Serde without creating fine-grained objects?


This was added in serde_json 1.0.29 as the type RawValue. It must be enabled using the raw_value feature and then placed behind a reference:

extern crate serde; // 1.0.79#[macro_use]extern crate serde_derive; // 1.0.79extern crate serde_json; // 1.0.30, features = ["raw_value"]#[derive(Serialize, Deserialize)]struct DataBlob<'a> {    id: &'a str,    priority: u8,    payload: &'a serde_json::value::RawValue,}fn main() {    let input = r#"{        "id": "cat",        "priority": 42,        "payload": [1, 2, 3, 4]    }"#;    let parsed = serde_json::from_str::<DataBlob>(input).expect("Could not deserialize");    let output = serde_json::to_string(&parsed).expect("Could not serialize");    assert!(output.contains("payload"));}