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

元气小坏坏 提交于 2019-12-06 17:14:26

问题


I have a JSON object that contains a few metadata keys and a large data payload. My service cares about the metadata for the purposes of logging and routing, but does not care about the payload, other than to pass the payload off to another service. I will never need to look inside the payload for any reason.

Right now, the payload is represented in my struct as aserde_json::Value. Through profiling, I've seen the (de)serialization of the Value takes a non-trivial amount of time.

Is there a mechanism in Serde where I can bundle around the payload without having to pay the cost of deserializing it into component values only to be required to re-serialize them later?

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

#[derive(Serialize, Deserialize)]
struct DataBlob<'a> {
    id: &'a str,
    priority: u8,
    // payload: OpaqueValue,
}

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"));
}

回答1:


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.79
extern 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"));
}


来源:https://stackoverflow.com/questions/49079533/is-there-a-way-to-deserialize-arbitrary-json-using-serde-without-creating-fine-g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!