-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzero_copy_serde.rs
More file actions
38 lines (30 loc) · 1.05 KB
/
zero_copy_serde.rs
File metadata and controls
38 lines (30 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! Advanced integration: zero-copy nested JSON parsing with `serde_json::RawValue`.
use json_escape::unescape_quoted;
use serde::Deserialize;
use serde_json::value::RawValue;
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct InnerPayload {
user_id: u64,
items: Vec<String>,
}
#[derive(Deserialize)]
struct OuterPayload<'a> {
transaction_id: String,
#[serde(borrow)]
payload: &'a RawValue,
}
fn main() {
let response_body = r#"{
"transaction_id": "txn_123",
"payload": "{\"user_id\": 42, \"items\": [\"apple\", \"orange\"]}"
}"#;
// Parse outer structure first.
let outer: OuterPayload = serde_json::from_str(response_body).unwrap();
// Create a reader that streams the unescaped inner payload.
let reader = unescape_quoted(outer.payload.get());
// Deserialize inner payload directly, without an intermediate String.
let inner: InnerPayload = serde_json::from_reader(reader).unwrap();
println!("Outer transaction: {}", outer.transaction_id);
println!("Inner payload: {:?}", inner);
}