Serde support for encoding/decoding rusty_v8 values

Overview

serde_v8

Author: Aaron O'Mullan [email protected]

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
You might also like...
A wav encoding and decoding library in Rust

Hound A wav encoding and decoding library in Rust. Hound can read and write the WAVE audio format, an ubiquitous format for raw, uncompressed audio. T

Encoding and decoding images in Rust
Encoding and decoding images in Rust

Image Maintainers: @HeroicKatora, @fintelia How to contribute An Image Processing Library This crate provides basic image processing functions and met

corncobs: Corny COBS encoding/decoding in Rust

corncobs: Corny COBS encoding/decoding in Rust This crate provides Consistent Overhead Byte Stuffing (COBS) support for Rust programs, with a particul

A library for decoding and encoding DirectDraw Surface files

A library for decoding and encoding DirectDraw Surface files. Currently handles decoding some uncompressed DX9 formats, as well as DXT1-5. Supports encoding in the A8R8G8B8 format. Support for cubemaps and volumes, as well as DX10 is planned.

A small command-line utility for encoding and decoding bech32 strings

A small command-line utility for encoding and decoding bech32 strings.

Derive macro for encoding/decoding instructions and operands as bytecode

bytecoding Derive macro for encoding and decoding instructions and operands as bytecode. Documentation License Licensed under either of Apache License

Astro Format is a library for efficiently encoding and decoding a set of bytes into a single buffer format.

Astro Format is a library for efficiently transcoding arrays into a single buffer and native rust types into strings

Substreams development kit for Ethereum chains, contains Firehose Block model and helpers as well as utilities for Ethereum ABI encoding/decoding.

Substreams Ethereum Substreams development kit for Ethereum chains, contains Rust Firehose Block model and helpers as well as utilities for Ethereum A

Huffman Encoding/Decoding

Huffman Encoding/Decoding This project is inspired by lab_huffman of CS225(2022fall) @ UIUC Team:    -- Chaoqi LIU ([email protected])    -- Jiahu

Zero-copy, no-std proquint encoding and decoding

proqnt A pronounceable quintuplet, or proquint, is a pronounceable 5-letter string encoding a unique 16-bit integer. Proquints may be used to encode b

Base 32 + 64 encoding and decoding identifiers + bytes in rust, quickly

fast32 Base32 and base64 encoding in Rust. Primarily for integer (u64, u128) and UUID identifiers (behind feature uuid), as well as arbitrary byte arr

A tool to deserialize data from an input encoding, transform it and serialize it back into an output encoding.

dts A simple tool to deserialize data from an input encoding, transform it and serialize it back into an output encoding. Requires rust = 1.56.0. Ins

Databento Binary Encoding (DBZ) - Fast message encoding and storage format for market data

dbz A library (dbz-lib) and CLI tool (dbz-cli) for working with Databento Binary Encoding (DBZ) files. Python bindings for dbz-lib are provided in the

A CSV parser for Rust, with Serde support.

csv A fast and flexible CSV reader and writer for Rust, with support for Serde. Dual-licensed under MIT or the UNLICENSE. Documentation https://docs.r

Serde support for (rusty_)v8

serde_v8 Serde support for (rusty_)v8 WIP: see denoland/deno#9540 TODO Experiment with KeyCache to optimize struct keys Experiment with external v8 st

Serde support for n-dimensional arrays from self-describing formats

serde-ndim Overview This crate provides a way to serialize and deserialize arrays of arbitrary dimensionality from self-described formats such as JSON

serde support for http crate types Request, Response, Uri, StatusCode, HeaderMap

serde extensions for the http crate types Allows serializing and deserializing the following types from http: Response Request HeaderMap StatusCode Ur

Character encoding support for Rust

Encoding 0.3.0-dev Character encoding support for Rust. (also known as rust-encoding) It is based on WHATWG Encoding Standard, and also provides an ad

TLV-C encoding support.

TLV-C: Tag - Length - Value - Checksum TLV-C is a variant on the traditional [TLV] format that adds a whole mess of checksums and whatnot. Why, you as

Comments
  • serde_v8 is vulnerable to prototype pollution

    serde_v8 is vulnerable to prototype pollution

    use rusty_v8 as v8;
    use serde::{Deserialize, Serialize};
    
    #[derive(Serialize, Deserialize)]
    struct TestStruct {
        a: String,
        b: String,
    }
    
    fn run_script(scope: &mut v8::HandleScope, code: &str) {
        let code_string = v8::String::new(scope, code).unwrap();
        let script = v8::Script::compile(scope, code_string, None).unwrap();
        script.run(scope).unwrap();
    }
    
    fn main() {
        let platform = v8::new_default_platform(0, false).make_shared();
        v8::V8::initialize_platform(platform);
        v8::V8::initialize();
    
        let isolate = &mut v8::Isolate::new(Default::default());
        let scope = &mut v8::HandleScope::new(isolate);
        let context = v8::Context::new(scope);
        let scope = &mut v8::ContextScope::new(scope, context);
    
        run_script(
            scope,
            r#"
                Object.defineProperty(Object.prototype, "a", {
                    set: (v) => {
                        throw new Error("WTF");
                    }
                });
            "#,
        );
    
        let test = TestStruct {
            a: "a".into(),
            b: "b".into(),
        };
        let try_catch = &mut v8::TryCatch::new(scope);
        let _result = serde_v8::to_v8(try_catch, test);
    
        match try_catch.message() {
            Some(message) => println!(
                "Thrown exception: {}",
                message.get(try_catch).to_rust_string_lossy(try_catch)
            ),
            None => println!("No exception thrown."),
        }
    }
    
    Thrown exception: Uncaught Error: WTF
    
    opened by andreubotella 0
Owner
Deno Land
Deno Land
A rust bencode encoding/decoding implementation backed by serde.

Bende A rust bencode encoding/decoding implementation backed by serde. About This is one of a few bencode implementations available for rust. Though t

Du Toit 3 Dec 4, 2022
Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

null 294 Dec 23, 2022
Encoding and decoding support for BSON in Rust

bson-rs Encoding and decoding support for BSON in Rust Index Overview of BSON Format Usage BSON Values BSON Documents Modeling BSON with strongly type

mongodb 304 Dec 30, 2022
axum-serde is a library that provides multiple serde-based extractors and responders for the Axum web framework.

axum-serde ?? Overview axum-serde is a library that provides multiple serde-based extractors / responses for the Axum web framework. It also offers a

GengTeng 3 Dec 12, 2023
A TOML encoding/decoding library for Rust

toml-rs A TOML decoder and encoder for Rust. This library is currently compliant with the v0.5.0 version of TOML. This library will also likely contin

Alex Crichton 1k Dec 30, 2022
Encoding and decoding images in Rust

Image Maintainers: @HeroicKatora, @fintelia How to contribute An Image Processing Library This crate provides basic image processing functions and met

image-rs 3.5k Jan 9, 2023
Google Encoded Polyline encoding & decoding in Rust.

polyline Google Encoded Polyline encoding & decoding in Rust. A Note on Coordinate Order This crate uses Coordinate and LineString types from the geo-

GeoRust 14 Dec 11, 2022
TIFF decoding and encoding library in pure Rust

image-tiff TIFF decoding and encoding library in pure Rust Supported Features Baseline spec (other than formats and tags listed below as not supported

image-rs 66 Dec 30, 2022
PNG decoding and encoding library in pure Rust

PNG Decoder/Encoder PNG decoder/encoder in pure Rust. It contains all features required to handle the entirety of the PngSuite by Willem van Schack. p

image-rs 247 Dec 25, 2022
CBOR (binary JSON) for Rust with automatic type based decoding and encoding.

THIS PROJECT IS UNMAINTAINED. USE serde_cbor INSTEAD. This crate provides an implementation of RFC 7049, which specifies Concise Binary Object Represe

Andrew Gallant 121 Dec 27, 2022