1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/serde_v8
Matt Mastracci fa935e553a
chore: Ensure copyright line is the first in the file (#19608)
The copyright checker was allowing files with code above the copyright
line in a few places, mainly as a result of IDEs ordering imports
improperly.

This makes the check more robust, and adds a whitelist of valid lines
that may appear before the copyright line.
2023-06-26 09:38:55 -06:00
..
benches chore: upgrade to Rust 1.67 (#17548) 2023-01-27 10:43:16 -05:00
examples chore: upgrade to Rust 1.67 (#17548) 2023-01-27 10:43:16 -05:00
magic chore: Ensure copyright line is the first in the file (#19608) 2023-06-26 09:38:55 -06:00
tests refactor: remove remaining references to ZeroCopyBuf (#19598) 2023-06-24 07:30:41 +00:00
Cargo.toml chore: forward v1.34.3 release commit to main (#19526) 2023-06-16 01:55:31 +02:00
de.rs fix(serde_v8): Do not coerce values in serde_v8 (#19569) 2023-06-23 12:52:48 +02:00
error.rs chore: upgrade rusty_v8 to 0.71.0 (#18868) 2023-04-27 20:50:46 +02:00
keys.rs chore: update copyright year to 2023 (#17247) 2023-01-02 21:00:42 +00:00
lib.rs refactor(serde_v8): split ZeroCopyBuf into JsBuffer and ToJsBuffer (#19566) 2023-06-22 23:37:56 +02:00
payload.rs feat(ext/kv): key-value store (#18232) 2023-03-22 12:13:24 +08:00
README.md chore: fix typos (#19572) 2023-06-26 09:10:27 -04:00
ser.rs refactor(serde_v8): split ZeroCopyBuf into JsBuffer and ToJsBuffer (#19566) 2023-06-22 23:37:56 +02:00
serializable.rs chore: fix typos (#19572) 2023-06-26 09:10:27 -04:00
utils.rs chore: update copyright year to 2023 (#17247) 2023-01-02 21:00:42 +00:00

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.

Advanced features

If you need to mix rust & v8 values in structs/tuples, you can use the special serde_v8::Value type, which will passthrough the original v8 value untouched when encoding/decoding.

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