Read & write JavaScript values from Python with the V8 serialization format.
Project description
Imagine having postMessage() between JavaScript and Python.
pip install v8serialize |
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file v8serialize-0.3.0.tar.gz.
File metadata
- Download URL: v8serialize-0.3.0.tar.gz
- Upload date:
- Size: 85.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09172274b65e4985445d9162953f125a6804f34ffde0e231710f3f431f7e19af
|
|
| MD5 |
59006758a15ecd2046e05b94ae649580
|
|
| BLAKE2b-256 |
b75ce5cb1daa3e024ed0d2ab73034047d7a4a143cd3961110a26077be951aba5
|
Provenance
The following attestation bundles were made for v8serialize-0.3.0.tar.gz:
Publisher:
publish.yml on h4l/v8serialize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
v8serialize-0.3.0.tar.gz -
Subject digest:
09172274b65e4985445d9162953f125a6804f34ffde0e231710f3f431f7e19af - Sigstore transparency entry: 563053427
- Sigstore integration time:
-
Permalink:
h4l/v8serialize@09ae8ce41a3cb9c2714a498a6dce1494bb018969 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/h4l
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@09ae8ce41a3cb9c2714a498a6dce1494bb018969 -
Trigger Event:
push
-
Statement type:
File details
Details for the file v8serialize-0.3.0-py3-none-any.whl.
File metadata
- Download URL: v8serialize-0.3.0-py3-none-any.whl
- Upload date:
- Size: 101.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a46d0f46f90e934ac7fa067d3af5df1a2a08d8cb6dd5ae2950c1c58a29d9605
|
|
| MD5 |
b1d4482390b885930a9bdc850e24a82e
|
|
| BLAKE2b-256 |
93f21fdde8388cae678c06fee19851d915831874c5cb61aa08f3eaf8d904e2bf
|
Provenance
The following attestation bundles were made for v8serialize-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on h4l/v8serialize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
v8serialize-0.3.0-py3-none-any.whl -
Subject digest:
5a46d0f46f90e934ac7fa067d3af5df1a2a08d8cb6dd5ae2950c1c58a29d9605 - Sigstore transparency entry: 563053438
- Sigstore integration time:
-
Permalink:
h4l/v8serialize@09ae8ce41a3cb9c2714a498a6dce1494bb018969 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/h4l
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@09ae8ce41a3cb9c2714a498a6dce1494bb018969 -
Trigger Event:
push
-
Statement type: