Skip to main content

Read & write JavaScript values from Python with the V8 serialization format.

Project description

The v8serialize logo. Monochome. Large "V8" and smaller "serialize" in a handwritten style, with the 8 stylized to look like a snake.

Imagine having postMessage() between JavaScript and Python.

pip install v8serialize

Documentation


v8serialize

A Python library to read & write JavaScript values in V8 serialization format with Python.

Examples

These examples demonstrate serializing and deserializing the same selection of JavaScript values in Python and JavaScript. JavaScript types supported by JavaScript's structuredClone() algorithm can be serialized.

Serialize with Python

from base64 import b64encode
from datetime import datetime, UTC
import re

from v8serialize import dumps
from v8serialize.jstypes import JSObject, JSArray, JSUndefined

serialized = dumps(
    [
        "strings 🧵🧶🪡",
        123,
        None,
        JSUndefined,
        JSArray({0: 'a', 1: 'b', 123456789: 'sparse'}),
        JSObject({"msg": "Hi"}),
        b"\xc0\xff\xee",
        2**128,
        {"maps": True},
        {"sets", "yes"},
        re.compile(r"^\w+$"),
        datetime(2024, 1, 1, tzinfo=UTC),
    ]
)

print(b64encode(serialized).decode())

Output

/w9BDFMUc3RyaW5ncyDwn6e18J+ntvCfqqFVezBfYZaa7zpVAFMBYVUBUwFiVZWa7zpTBnNwYXJzZUADlprvOm9TA21zZ1MCSGl7AUIDwP/uWiIAAAAAAAAAAAAAAAAAAAAAATtTBG1hcHNUOgInUwRzZXRzUwN5ZXMsAlJTBV5cdyskgAJEAABAHyXMeEIkAAw=

Deserialize with Python

from base64 import b64decode
from v8serialize import loads

# The output of the JavaScript example
serialized = b64decode(
    "/w9BDGMccwB0AHIAaQBuAGcAcwAgAD7Y9d0+2PbdPtih3kn2ATBfYZaa7zpJACIBYUkCIgFiSaq03nUiBnNwYXJzZUADlprvOm8iA21zZyICSGl7AUIDwP/uWjAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAA7IgRtYXBzVDoCJyIDeWVzIgRzZXRzLAJSIgVeXHcrJIACRAAAQB8lzHhCJAAM"
)
print(loads(serialized))

Output

JSArray([
  'strings 🧵🧶🪡',
  123,
  None,
  JSUndefined,
  JSArray({
    0: 'a',
    1: 'b',
    123456789: 'sparse',
  }),
  JSObject(msg='Hi'),
  JSArrayBuffer(b'\xc0\xff\xee'),
  340282366920938463463374607431768211456,
  JSMap({
    'maps': True,
  }),
  JSSet([
    'yes',
    'sets',
  ]),
  JSRegExp(source='^\\w+$', flags=<JSRegExpFlag.UnicodeSets: 256>),
  datetime.datetime(2024, 1, 1, 0, 0),
])

Serialize with Node.js / Deno

import * as v8 from "node:v8";

const sparseArray = ["a", "b"];
sparseArray[123456789] = "sparse";

const buffer = v8.serialize([
  "strings 🧵🧶🪡",
  123,
  null,
  undefined,
  sparseArray,
  { msg: "Hi" },
  Uint8Array.from([0xc0, 0xff, 0xee]).buffer,
  2n ** 128n,
  new Map([["maps", true]]),
  new Set(["yes", "sets"]),
  /^\w+$/v,
  new Date(Date.UTC(2024, 0, 1)),
]);

console.log(buffer.toString("base64"));

Output

/w9BDGMccwB0AHIAaQBuAGcAcwAgAD7Y9d0+2PbdPtih3kn2ATBfYZaa7zpJACIBYUkCIgFiSaq03nUiBnNwYXJzZUADlprvOm8iA21zZyICSGl7AUIDwP/uWjAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAA7IgRtYXBzVDoCJyIDeWVzIgRzZXRzLAJSIgVeXHcrJIACRAAAQB8lzHhCJAAM

Deserialize with Node.js / Deno

import * as v8 from "node:v8";

// The output of the Python example
const buffer = Buffer.from(
  "/w9BDFMUc3RyaW5ncyDwn6e18J+ntvCfqqFVezBfYZaa7zpVAFMBYVUBUwFiVZWa7zpTBnNwYXJzZUADlprvOm9TA21zZ1MCSGl7AUIDwP/uWiIAAAAAAAAAAAAAAAAAAAAAATtTBG1hcHNUOgInUwN5ZXNTBHNldHMsAlJTBV5cdyskgAJEAABAHyXMeEIkAAw=",
  "base64"
);
console.log(v8.deserialize(buffer));

Output

[
  'strings 🧵🧶🪡',
  123,
  null,
  undefined,
  [ 'a', 'b', <123456787 empty items>, 'sparse' ],
  { msg: 'Hi' },
  ArrayBuffer { [Uint8Contents]: <c0 ff ee>, byteLength: 3 },
  340282366920938463463374607431768211456n,
  Map(1) { 'maps' => true },
  Set(2) { 'yes', 'sets' },
  /^\w+$/v,
  2024-01-01T00:00:00.000Z
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

v8serialize-0.2.0a0.tar.gz (86.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

v8serialize-0.2.0a0-py3-none-any.whl (101.6 kB view details)

Uploaded Python 3

File details

Details for the file v8serialize-0.2.0a0.tar.gz.

File metadata

  • Download URL: v8serialize-0.2.0a0.tar.gz
  • Upload date:
  • Size: 86.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for v8serialize-0.2.0a0.tar.gz
Algorithm Hash digest
SHA256 57dc3262a089ba5917da4d53300fa392e50e19ab2476ef3e507d6061f70ee332
MD5 4015e978db4afb33e7b120c2e1c4a979
BLAKE2b-256 8ad7257d89aa907f7e4a26281108eea7acafb41fafb9ae452562003e0f6a7d48

See more details on using hashes here.

Provenance

The following attestation bundles were made for v8serialize-0.2.0a0.tar.gz:

Publisher: publish.yml on h4l/v8serialize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file v8serialize-0.2.0a0-py3-none-any.whl.

File metadata

  • Download URL: v8serialize-0.2.0a0-py3-none-any.whl
  • Upload date:
  • Size: 101.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for v8serialize-0.2.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 643557ed38757a5ddaac008469bc87c47e1e89df062941ce133323e60122f4f0
MD5 089b2d2d2ac52b6cee8553c99db7489e
BLAKE2b-256 b3fb01b7207a3b6d8034afbcee94834bd05ec36700f7d9180a7469425e11e8fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for v8serialize-0.2.0a0-py3-none-any.whl:

Publisher: publish.yml on h4l/v8serialize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page