1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00
denoland-deno/serde_v8
denobot e7ea4edc8a
1.23.0 (#14878)
* 1.23.0

* docs(Releases.md): update a few items for 1.23

* docs(Releases.md): revert bad formatting

Co-authored-by: aslilac <aslilac@users.noreply.github.com>
Co-authored-by: McKayla Washburn <mckayla@hey.com>
2022-06-15 17:02:18 -06:00
..
benches perf(serde_v8): zero-copy StringOrBuffer (#14381) 2022-04-24 09:28:46 -03:00
examples chore: update rusty_v8 to 0.41.0 (#13909) 2022-03-11 19:59:01 +05:30
magic chore(serde_v8): throw error when string buffer exceeds v8 max length (#14588) 2022-05-26 17:15:44 +02:00
tests feat(serde_v8): bytes::Bytes support (#14412) 2022-05-13 12:53:13 +02:00
Cargo.toml 1.23.0 (#14878) 2022-06-15 17:02:18 -06:00
de.rs perf(serde_v8): fast path for large strings (#14450) 2022-05-15 17:16:09 +02:00
error.rs fix(serde_v8): more robust number deserialization (#14216) 2022-04-12 14:36:21 +02:00
keys.rs chore: drop src/ in bench_util & serde_v8 (#14097) 2022-03-24 11:23:40 +01:00
lib.rs perf(core): optimize encode on large strings (#14619) 2022-05-15 17:52:32 +02:00
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 cleanup(serde_v8): disambiguate ZeroCopyBuf (#14380) 2022-04-25 16:56:47 +02:00
serializable.rs cleanup(serde_v8): disambiguate ZeroCopyBuf (#14380) 2022-04-25 16:56:47 +02:00
utils.rs chore: drop src/ in bench_util & serde_v8 (#14097) 2022-03-24 11:23:40 +01: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