Skip to main content

Module transcode

Module transcode 

Source
Expand description

Zero-Value MessagePack → JSON transcoding for the read path. Zero-Value MessagePack → JSON transcoding for the read path.

The storage layer persists records as MessagePack (see crate::encoding::encode) but the HTTP wire format is JSON. The historical read path paid for this twice: it deserialized the stored bytes into a serde_json::Value tree and then re-serialized that tree to JSON bytes. Both the tree allocation and the re-walk are pure overhead when the record needs no in-memory transformation.

This module streams the MessagePack deserializer directly into a serde_json::Serializer using the established serde “transcode” pattern: a serde::Serialize adapter that, when asked to serialize, pulls one value out of a serde::Deserializer and forwards it into the serializer event-by-event. No serde_json::Value is ever materialized.

§Byte-identity with the round-trip path

The write path serializes a serde_json::Value to MessagePack. A serde_json::Value’s object is a BTreeMap (the workspace does not enable serde_json’s preserve_order feature), so stored map keys are already in sorted order. serde_json::Serializer emits map entries in the order it receives them, so transcoding preserves that sorted order — byte-identical to serde_json::to_vec(&decode::<Value>(bytes)). Numbers and strings are formatted by the same serde_json serializer in both paths, so their byte representation matches as well.

§Top-level key filtering

[transcode_msgpack_to_json_filtered] reproduces the read path’s schema-strip (retain(|k| k == "id" || allowed.contains(k))) during the transcode itself, so the fast path stays correct for records that carry fields no longer in the current schema. Filtering happens only at the top-level object; nested objects are forwarded verbatim, exactly as the Value-based strip behaved (it only ever called as_object_mut on the root record).

Functions§

transcode_msgpack_to_json
Transcode MessagePack bytes directly to JSON bytes without materializing a serde_json::Value.
transcode_msgpack_to_json_filtered
Transcode MessagePack bytes to JSON bytes, retaining only top-level object keys that are "id" or present in allowed.