Skip to main content

Module serde_helpers

Module serde_helpers 

Source
Expand description

Serialization helpers for reducing allocation overhead.

§Reusable serialization buffers

SerBuffer provides a thread-local reusable Vec<u8> for serde_json::to_writer. This eliminates the per-call buffer allocation that dominates small-payload serialization cost (2.3× overhead at 64B).

use a2a_protocol_types::serde_helpers::SerBuffer;
use a2a_protocol_types::message::Part;

let part = Part::text("hello");
let bytes = SerBuffer::serialize(&part).expect("serialize");
assert!(bytes.starts_with(b"{"));

§Borrowed deserialization

deser_from_str wraps serde_json::from_str which enables serde’s visit_borrowed_str path. When deserializing from a &str (vs &[u8]), serde_json can borrow string values directly from the input buffer instead of allocating new String objects. This reduces deserialization allocations by ~15-25% for types with many string fields.

Structs§

SerBuffer
Thread-local reusable serialization buffer.

Functions§

deser_from_slice
Deserializes a value from a byte slice, first converting to &str to enable serde_json’s borrowed-data optimizations.
deser_from_str
Deserializes a value from a &str using serde_json’s borrowed-data path.