serde-wasm-bindgen

Native integration of Serde with wasm-bindgen for fast Rust-to-JavaScript value conversion

Library
Cargo
v0.6.5
615stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
35/100Needs Attention
Development Activity0
Maintenance0
Community52
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
86/100Excellent
Architecture90
Code Quality88
Innovation87
Learning Curve80

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_value and from_value helpers that serialize and deserialize any Serde-compatible Rust type against a JsValue
  • A configurable Serializer with presets such as json_compatible() and options for maps-as-objects, null handling, bigint number types, and byte arrays
  • A preserve module that passes native JavaScript values (Date, RegExp, arbitrary JsValue) through unchanged via serde(with)
  • Errors surfaced as real JavaScript exceptions, so ? works seamlessly in wasm-bindgen exports returning Result<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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search