In Rust, why can serde_json fail to deserialize an `OrderEvent<'a>` with a `customer: &'a str` field, and why won't `from_reader` accept that type?
answer
- borrow straight from the input
- escapes force a rewritten string
- transient, borrowed, owned
- for<'de> means owns everything
basics
~20 sA &'a str field must point into the input, so serde_json fails with "expected a borrowed string" whenever the JSON string contains an escape and has to be rewritten. from_reader requires DeserializeOwned, which a borrowing type cannot satisfy.
solid answer
~50 sZero-copy deserialization means a field like `customer: &'a str` points straight into the input text, with `'de`, the input's lifetime, outliving `'a`. That works only when the bytes in the input are exactly the string's value. A JSON string with an escape such as `\"` or `\u00e9` must be unescaped into a scratch buffer first, so serde_json can only offer a temporary string, and the `&str` impl rejects it: `invalid type: string "Ana \"Ace\" Silva", expected a borrowed string`. Any field that may contain escapes should be a `String`. The bound follows from the same rule: `from_str` and `from_slice` take `T: Deserialize<'a>` tied to the input, but `from_reader` and `from_value` require `T: DeserializeOwned`, meaning `for<'de> Deserialize<'de>`, because the input is gone when they return; a borrowing type fails with ``implementation of `Deserialize` is not general enough``.
code
rust · 19 linesuse serde::Deserialize;
#[derive(Deserialize)]
struct OrderEvent<'a> {
order_id: u64,
customer: &'a str,
}
fn main() {
let body = String::from(r#"{"order_id": 42, "customer": "Ana Silva"}"#);
let event: OrderEvent = serde_json::from_str(&body).unwrap();
println!("{} borrowed from body: {}", event.order_id, event.customer);
let escaped = r#"{"order_id": 42, "customer": "Ana \"Ace\" Silva"}"#;
match serde_json::from_str::<OrderEvent>(escaped) {
Ok(e) => println!("{}", e.customer),
Err(err) => println!("{err}"),
}
}go deeper
Recall that a &str field borrows from the input while a String field owns a copy, and that the input must outlive the parsed struct.
Explain what 'de is, why an escape forces a scratch buffer, and which serde_json functions take Deserialize<'de> versus DeserializeOwned.
Show the production judgment: the escape failure is data-dependent and slips past tests, so borrow only escape-free fields and bound helpers by who owns the input.
Weigh zero-copy against risk: the saved allocations matter only on measured hot paths, and a borrowing type spreads lifetime parameters through every API it touches.
## What zero-copy deserialization is Deserializing `customer: String` allocates a new string for each field and copies the text out of the input. **Zero-copy deserialization** skips that: the field is a `&'a str` that points into the input buffer itself. Serde's documentation describes it as deserializing into a structure that borrows string or byte data from the input, avoiding an allocation and a copy per field, with the borrow checker guaranteeing the input outlives the result. For a high-volume webhook consumer that parses large bodies and discards them, it removes most of the allocations. ```rust #[derive(Deserialize)] struct OrderEvent<'a> { order_id: u64, customer: &'a str, } ``` Why a struct holding a reference needs a lifetime parameter at all is covered with lifetimes in structs; here the question is what Serde does with it. ## The `'de` lifetime `Deserialize` has a lifetime parameter: `trait Deserialize<'de>`. **`'de` is the lifetime of the data being deserialized from.** For `OrderEvent<'a>`, the derive generates an impl in which `'de` must outlive `'a`, the pattern Serde's documentation writes as `impl<'de: 'a, 'a> Deserialize<'de> for OrderEvent<'a>`: the input must outlive every borrow in the result. A type that borrows nothing has no constraint on `'de` at all, so it implements `Deserialize<'de>` for every `'de`. ## Why an escaped string cannot borrow Serde's data model hands strings to a visitor in **three flavours**: - **transient**: `visit_str(&str)`, valid only during the call; - **borrowed**: `visit_borrowed_str(&'de str)`, valid for the input's lifetime; - **owned**: `visit_string(String)`. The `&'a str` impl accepts only the borrowed flavour. serde_json can offer a borrowed string only when the characters between the quotes are exactly the value. Any escape sequence, `\"`, `\\`, `\n` or `\u00e9`, has to be decoded into a scratch buffer, and a slice of that scratch buffer is transient. The result is a runtime error, not a compile error, and it depends on the **data**: ```plaintext invalid type: string "Ana \"Ace\" Silva", expected a borrowed string at line 1 column 48 ``` That makes `&str` a trap for free text such as customer names, addresses or notes, which will eventually contain a quote or a non-ASCII escape. Tests written with tidy sample data pass, and the first payload with an escaped character fails in production. `&'a [u8]` behaves alike: a JSON string borrows its raw bytes, but a JSON array of numbers cannot be borrowed and fails with `invalid type: sequence, expected a borrowed byte array`. A field that borrows when it can and allocates when it must is a `Cow` with `#[serde(borrow)]`, which the material on `Cow` covers. ## `Deserialize<'de>` versus `DeserializeOwned` There are two ways to write the bound: | Bound | Meaning | serde_json functions | |---|---|---| | `T: Deserialize<'de>` | `T` can be deserialized from input of one lifetime the **caller** chooses | `from_str(&'a str)`, `from_slice(&'a [u8])` | | `T: DeserializeOwned` | `T` can be deserialized from input of **any** lifetime, so it borrows nothing | `from_reader(R)`, `from_value(Value)` | `DeserializeOwned` is defined as a trait with the supertrait `for<'de> Deserialize<'de>` and a blanket impl, so it is exactly that higher-ranked bound with a readable name. A `serde_json::Value` owns all of its data, which is why `from_value` asks for `DeserializeOwned` as well. `from_reader` needs it because data read from an I/O stream passes through buffers that do not outlive the call, so nothing can borrow from them. `OrderEvent<'a>` implements `Deserialize<'de>` only for `'de` that outlive `'a`, never for every `'de`, so rustc reports ``implementation of `Deserialize` is not general enough``, with no error code. How higher-ranked bounds work in general belongs to the lesson on them. The same mistake appears in your own helpers. `fn parse<T: DeserializeOwned>(body: &str)` cannot return a borrowing type; `fn parse<'de, T: Deserialize<'de>>(body: &'de str)` can. Serde's documentation adds that `T: Deserialize<'static>` is never what you want: it demands input that lives for the whole program. ## Choosing field types 1. Default to `String` and owned types; measure before borrowing. 2. Borrow `&'a str` only for values that cannot contain escapes, such as IDs, enum-like codes or hashes you control, and keep the input buffer alive as long as the struct. 3. Read whole bodies into memory and use `from_slice` or `from_str` when you want borrowing; `from_reader` never borrows. 4. Bound generic helpers with `DeserializeOwned` when they own the input, and with `Deserialize<'de>` tied to a parameter when the caller supplies it. Zero-copy layouts as a format design question belong to the material on serialisation performance.
- How do you write a generic helper that parses a request body into any Serde type, borrowing types included?Tie the type to the input: `fn parse<'de, T: Deserialize<'de>>(body: &'de str) -> serde_json::Result<T>`. The caller chooses `'de`, and `T` may borrow from `body` for as long as `body` lives. Use `T: DeserializeOwned` only when the helper owns the input and drops it before returning, as with a reader or a decoded buffer.
- Does a &'a [u8] field behave the same way with serde_json?Much the same. A JSON string borrows its raw bytes, so `"abc"` becomes `[97, 98, 99]` pointing into the input. A JSON array of numbers cannot be borrowed and fails with `invalid type: sequence, expected a borrowed byte array`, because serde_json would have to build a new buffer for it.
- Why is T: Deserialize<'static> the wrong bound for a helper?It demands that borrowed data live for the whole program, so it only accepts input that is itself `'static`, such as a literal or a leaked buffer. Serde's documentation says writing `'static` near `Deserialize` is a sign of being on the wrong track; use `Deserialize<'de>` tied to the input, or `DeserializeOwned`.
- A &str field still copies the text, just without the String type.
- Borrowed &str fields work for every JSON string, escapes included.
- DeserializeOwned is just another name for Deserialize<'static>.
- from_reader can lend out its internal buffer, so &str fields are fine.
- The borrowing failure on escaped strings is caught at compile time.