4 releases

✓ Uses Rust 2018 edition

0.1.3 May 16, 2019
0.1.2 May 16, 2019
0.1.1 May 16, 2019
0.1.0 May 16, 2019

#19 in WebAssembly

Download history 720/week @ 2019-12-02 1197/week @ 2019-12-09 1283/week @ 2019-12-16 394/week @ 2019-12-23 709/week @ 2019-12-30 1016/week @ 2020-01-06 1923/week @ 2020-01-13 2209/week @ 2020-01-20 3325/week @ 2020-01-27 2155/week @ 2020-02-03 1918/week @ 2020-02-10 1591/week @ 2020-02-17 3143/week @ 2020-02-24 3242/week @ 2020-03-02 2492/week @ 2020-03-09 2282/week @ 2020-03-16

4,430 downloads per month
Used in 2 crates

MIT license

787 lines

This is an alternative native integration of Serde with wasm-bindgen.


The primary difference with the built-in implementation is that it leverages direct APIs for JavaScript value manipulation instead of passing data in a JSON format. This allows it to support more types while producing a much leaner Wasm binary. In particular, it saved 26.6KB when comparing size-optimised and Brotli-compressed benchmarks with a stripped debug information.

Performance-wise the library is currently comparable with the original. Specific numbers vary a lot between the engines and used data types and, according to benchmarks, range from 1.6x regression in worst cases to 3.3x improvement in best cases. Your mileage might vary.

These numbers are currently mostly saturated by the overhead of frequent JavaScript <-> Wasm and JavaScript <-> C++ calls. These calls are used for sharing JavaScript values with the Rust side as well as encoding/decoding UTF-8 strings, and will go away in the future when reference types proposal lands natively in Wasm.


To pass a Rust value to JavaScript, use:

pub fn pass_value_to_js() -> Result<JsValue, JsValue> {
	// ...

To retrieve a value from JavaScript:

pub fn get_value_from_js(value: JsValue) -> Result<(), JsValue> {
	let value: SomeSupportedRustType = serde_wasm_bindgen::from_value(value)?;
	// ...

Supported types

Note that, even though it might often be the case, this library doesn't attempt to be strictly compatible with either serde_json or, correspondingly, JsValue::from_serde / JsValue::into_serde, instead prioritising better compatibility with common JavaScript idioms and representations.

Supported types and values for the deserialization:

  • () from undefined and null.
  • Option<_> from any value will map undefined or null to None and any other value to Some(...).
  • bool from a JavaScript boolean (false and true).
  • Rust integer (u8/i8/.../u128/i128) from a safe JavaScript integer (as matched by Number.isSafeInteger).
  • Rust floating number (f32/f64) from any JavaScript number.
  • char from a JavaScript string containing a single codepoint.
  • String from any JavaScript string.
  • Rust map (HashMap, BTreeMap, ...) from any JavaScript iterable producing [key, value] pairs (including but not limited to ES2015 Map).
  • HashMap<String, _> from any plain JavaScript object ({ key1: value1, ... }).
  • Rust sequence (tuple, Vec<_>, HashSet<_>, ...) from any JavaScript iterable (including but not limited to Array, ES2015 Set, etc.).
  • Rust byte buffer (see serde_bytes) from JavaScript ArrayBuffer or Uint8Array.
  • Typed Rust structure from any plain JavaScript object ({ key1: value1, ... }).
  • Rust enum from either a string ("Variant") or a plain object. Specific representation is controlled by #[serde(...)] attributes and should be compatible with serde-json.

Serialization is compatible with the deserialization, but it's limited to a single representation, so it chooses:

  • undefined for () or None.
  • ES2015 Map for Rust maps.
  • Array for any Rust sequences.
  • Uint8Array for byte buffers.
  • Plain JavaScript object for typed Rust structures.


Licensed under the MIT license. See the LICENSE file for details.


~36K SLoC