1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 01:29:14 -05:00
denoland-deno/serde_v8
Bartek Iwańczuk 869acee8fb
chore: upgrade rusty_v8 to 0.54.0 (#16368)
<!--
Before submitting a PR, please read http://deno.land/manual/contributing

1. Give the PR a descriptive title.

  Examples of good title:
    - fix(std/http): Fix race condition in server
    - docs(console): Update docstrings
    - feat(doc): Handle nested reexports

  Examples of bad title:
    - fix #7123
    - update docs
    - fix bugs

2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
-->
2022-10-20 21:01:49 +02:00
..
benches perf(serde_v8): zero-copy StringOrBuffer (#14381) 2022-04-24 09:28:46 -03:00
examples build: require safety comments on unsafe code (#13870) 2022-06-26 00:13:24 +02:00
magic perf(serde_v8): serde_v8::StringOrBuffer return JS ArrayBuffer instead of Uint8Array (#16360) 2022-10-20 16:03:57 +05:30
tests chore(serde_v8): Use SeqAccess in MapObjectAccess to avoid intermediate allocation (#16137) 2022-10-03 12:37:18 +02:00
Cargo.toml chore: upgrade rusty_v8 to 0.54.0 (#16368) 2022-10-20 21:01:49 +02:00
de.rs fix(serde_v8): avoid creating unsound slice reference (#16189) 2022-10-08 19:34:00 +05:30
error.rs chore: fix clippy warnings (#15944) 2022-09-19 10:25:03 +02:00
keys.rs chore: drop src/ in bench_util & serde_v8 (#14097) 2022-03-24 11:23:40 +01:00
lib.rs feat(serde_v8): add serde_v8::Global (#14761) 2022-06-19 08:39:11 +05:30
payload.rs chore: drop src/ in bench_util & serde_v8 (#14097) 2022-03-24 11:23:40 +01:00
README.md chore: upgrade deno_ast to 0.5.0 (#12595) 2021-11-01 16:22:27 -04:00
ser.rs perf(napi): optimize primitive napi functions (#16163) 2022-10-07 16:24:01 +05:30
serializable.rs chore(serde_v8): take mutable reference in ToV8::to_v8 (#15707) 2022-09-01 15:54:40 +05:30
utils.rs build: require safety comments on unsafe code (#13870) 2022-06-26 00:13:24 +02: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 unecessary "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