serde-wasm-bindgen
Native integration of Serde with wasm-bindgen for fast Rust-to-JavaScript value conversion
Repository Health
Technical Analysis
serde-wasm-bindgen is a native adapter that plugs Serde directly into wasm-bindgen, letting Rust code compiled to WebAssembly convert its data structures into idiomatic JavaScript values and back without going through JSON. Any type that derives Serde’s Serialize and Deserialize gains a to_value/from_value bridge to the JavaScript world.
Originally built at Cloudflare as an alternative to wasm-bindgen’s built-in JSON-based serde support, it is now the officially preferred approach. Because it maps Rust types onto native JS types (Map, Array, Uint8Array, bigint) instead of stringifying to JSON, it produces smaller code-size overhead and, in most common cases, noticeably faster serialization and deserialization.
What You Get
to_valueandfrom_valuehelpers that serialize and deserialize any Serde-compatible Rust type against aJsValue- A configurable
Serializerwith presets such asjson_compatible()and options for maps-as-objects, null handling, bigint number types, and byte arrays - A
preservemodule that passes native JavaScript values (Date, RegExp, arbitrary JsValue) through unchanged viaserde(with) - Errors surfaced as real JavaScript exceptions, so
?works seamlessly in wasm-bindgen exports returningResult<T, JsValue>
Common Use Cases
- Passing complex Rust data structures to and from JavaScript in a wasm-bindgen web app
- Replacing JSON-based serde bridging to shrink Wasm bundle size and speed up hot serialization paths
- Preserving JavaScript-only values like Date or RegExp inside otherwise Serde-serialized structs
- Generating TypeScript typings by combining it with tsify for structural type generation
Under The Hood
Architecture - The crate is small and tightly focused: lib.rs exposes the public to_value/from_value entry points plus a pointer-keyed thread-local cache that interns &'static str field names into JsStrings, while ser.rs implements a Serde Serializer that emits native js_sys values and de.rs implements the matching Deserializer that reads them back. A preserve module uses an intentionally asymmetric magic-string wrapper to pass opaque JsValues through the Serde data model unchanged.
Tech Stack - Written in Rust (edition 2024) with only three runtime dependencies: serde (with derive), js-sys, and wasm-bindgen. It targets wasm32-unknown-unknown, builds with LTO and a single codegen unit in release, and relies on custom #[wasm_bindgen] extern "C" bindings (an ObjectExt with indexing getter/setter) to avoid the overhead of fallible Reflect calls on plain objects.
Code Quality - The code enforces #![warn(missing_docs)] and clippy’s missing_const_for_fn, and ships a substantial test suite (tests/node.rs, tests/browser.rs, shared tests/common) run via wasm-bindgen-test, backed by proptest and a dedicated benchmarks/ workspace member. Errors are modeled as a single Error newtype wrapping JsValue with clean From/Display impls, and unsafe is confined to the preserve ABI round-trip with an explicit safety rationale.
API Design - The surface area is deliberately minimal: two free functions cover the common case, and a builder-style Serializer exposes discoverable toggles (serialize_maps_as_objects, serialize_large_number_types_as_bigints, json_compatible()) for the rest. Because it hooks into Serde’s derive macros, existing #[derive(Serialize, Deserialize)] types work with zero boilerplate, and the README documents the full Rust-to-JS type mapping table.
Used by 3 apps in this directory
Bramble
Password Manager · Security · Authentication
Local-first, end-to-end encrypted password manager that syncs your vault directly between your own devices over a private peer-to-peer mesh — no server, no account, no cloud in the middle.
hoodik
File Storage · Security
Self-hosted, end-to-end encrypted cloud storage with browser-based encryption and S3-compatible storage support
liteparse
Developer Tools
A fast, lightweight, open-source document parser that extracts spatial text, bounding boxes, and Markdown from PDFs and Office files — entirely on your machine.