Skip to content

fix(kv): serialize maps as objects in KvStore::put - #1094

Merged
guybedford merged 2 commits into
cloudflare:mainfrom
rosalbito:fix/kv-put-json-maps
Nov 5, 2026
Merged

guybedford merged 2 commits into
cloudflare:mainfrom
rosalbito:fix/kv-put-json-maps

Conversation

@rosalbito

Copy link
Copy Markdown
Contributor

Problem

KvStore::put serializes any non-string value with serde_wasm_bindgen::to_value and then JSON.stringify. The default serializer writes Rust maps (HashMap, BTreeMap, serde_json::Value::Object) as a JS Map and byte buffers as a Uint8Array; JSON.stringify turns a Map into {} and a Uint8Array into an object keyed by index. Every map-typed field is silently stored as {} and cannot be read back.

Same root cause as the queue producer fix in the sibling PR for Queue::send.

Fix

Serialize with serialize_maps_as_objects(true) and serialize_bytes_as_arrays(true), which is what the builder options in the same module already do. Strings, numbers and booleans keep their existing short-circuits.

Tests

Puts a struct with a HashMap and a serde_json::Value field, then checks both the Rust round-trip and the raw stored JSON text. Fails on main, passes with this change; full suite green.

Tested on macOS (Apple Silicon) only.

KvStore::put serializes any non-string value with
serde_wasm_bindgen::to_value and then JSON.stringify, but the default
serializer writes Rust maps (HashMap, BTreeMap, serde_json::Value::Object)
as JS Map and byte buffers as Uint8Array. JSON.stringify encodes a Map as
{} and a Uint8Array as an object keyed by index, so every map-typed field
is silently stored as {} and cannot be read back.

Serialize the value with serialize_maps_as_objects and
serialize_bytes_as_arrays, as the builder options already do. Strings,
numbers and booleans take the same short-circuits as before. Add a test
that puts a struct with a HashMap and a serde_json::Value field and checks
both the Rust round-trip and the raw stored JSON.
@rosalbito
rosalbito requested a review from guybedford as a code owner October 18, 2026 23:44
guybedford
guybedford previously approved these changes Oct 30, 2026
serde_wasm_bindgen serializes None and () as undefined, which
JSON.stringify drops from objects, so null map entries were lost. Encode
with serde_json directly, matching the ts-gen-worker KV put, and cover a
null entry in the test.
@guybedford
guybedford merged commit d4c2fb2 into cloudflare:main Nov 5, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants