Crates.io | serde_v8 |
lib.rs | serde_v8 |
version | 0.234.0 |
source | src |
created_at | 2021-03-26 02:41:22.7751 |
updated_at | 2024-12-06 10:08:55.011839 |
description | Rust to V8 serialization and deserialization |
homepage | |
repository | https://github.com/denoland/deno_core |
max_upload_size | |
id | 373639 |
size | 133,815 |
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
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
, ...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
.
serde_json
(should be mostly
interchangeable)Payload
type that's deserializable by itself (holds scope &
value).unwrap()
s