251 breaking releases

Uses new Rust 2024

new 0.254.0 Apr 30, 2025
0.252.0 Apr 8, 2025
0.250.0 Mar 20, 2025
0.236.0 Dec 20, 2024
0.1.0 Mar 26, 2021

#105 in Encoding

Download history 178061/week @ 2025-01-12 124741/week @ 2025-01-19 150595/week @ 2025-01-26 154789/week @ 2025-02-02 167007/week @ 2025-02-09 150803/week @ 2025-02-16 201743/week @ 2025-02-23 193898/week @ 2025-03-02 200177/week @ 2025-03-09 181101/week @ 2025-03-16 199938/week @ 2025-03-23 188248/week @ 2025-03-30 199067/week @ 2025-04-06 181478/week @ 2025-04-13 185087/week @ 2025-04-20 137364/week @ 2025-04-27

704,897 downloads per month
Used in 163 crates (12 directly)

MIT license

88KB
2.5K SLoC

serde_v8

Author: Aaron O'Mullan aaron.omullan@gmail.com

Serde support for encoding/decoding (rusty_)v8 values.

Broadly serde_v8 aims to provide an expressive but ~maximally efficient encoding layer to biject rust & v8/js values. It's a core component of deno's op-layer and is used to encode/decode all non-buffer values.

Original issue: denoland/deno#9540

Quickstart

serde_v8 fits naturally into the serde ecosystem, so if you've already used serde or serde_json, serde_v8's API should be very familiar.

serde_v8 exposes two key-functions:

  • to_v8: maps rust->v8, similar to serde_json::to_string, ...
  • from_v8: maps v8->rust, similar to serde_json::from_str, ...

Best practices

Whilst serde_v8 is compatible with serde_json::Value it's important to keep in mind that serde_json::Value is essentially a loosely-typed value (think nested HashMaps), so when writing ops we recommend directly using rust structs/tuples or primitives, since mapping to serde_json::Value will add extra overhead and result in slower ops.

I also recommend avoiding unnecessary "wrappers", if your op takes a single-keyed struct, consider unwrapping that as a plain value unless you plan to add fields in the near-future.

Instead of returning "nothing" via Ok(json!({})), change your return type to rust's unit type () and returning Ok(()), serde_v8 will efficiently encode that as a JS null.

TODO

  • Experiment with KeyCache to optimize struct keys
  • Experiment with external v8 strings
  • Explore using json-stringifier.cc's fast-paths for arrays
  • Improve tests to test parity with serde_json (should be mostly interchangeable)
  • Consider a Payload type that's deserializable by itself (holds scope & value)
  • Ensure we return errors instead of panicking on .unwrap()s

Dependencies

~86MB
~1.5M SLoC