Name | v8serialize JSON |
Version |
0.1.0
JSON |
| download |
home_page | None |
Summary | Read & write JavaScript values from Python with the V8 serialization format. |
upload_time | 2024-09-24 14:28:26 |
maintainer | None |
docs_url | None |
author | Hal Blackburn |
requires_python | <4.0,>=3.9 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<p align="center">
<img src="docs/v8serialize_logo_auto.svg" width="300" alt="The v8serialize logo. Monochome. Large "V8" and smaller "serialize" in a handwritten style, with the 8 stylized to look like a snake.">
</p>
<p align="center"><em>Imagine having <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage"><code>postMessage()</code></a> between JavaScript and Python.</em></p>
<table align="center">
<tr>
<td><pre lang="shell">pip install v8serialize</pre></td>
</tr>
</table>
<p align="center"><a href="https://h4l.github.io/v8serialize/en/latest/">Documentation</a></p>
---
# `v8serialize`
A Python library to read & write JavaScript values in [V8 serialization format]
with Python.
[V8 serialization format]:
https://h4l.github.io/v8serialize/en/latest/explanation/v8_serialization_format.html
## 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.
[`structuredClone()` algorithm]:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
### Serialize with Python
```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
```python
from base64 import b64decode
from v8serialize import loads
# The output of the JavaScript example
serialized = b64decode(
"/w9BDGMccwB0AHIAaQBuAGcAcwAgAD7Y9d0+2PbdPtih3kn2ATBfYZaa7zpJACIBYUkCIgFiSaq03nUiBnNwYXJzZUADlprvOm8iA21zZyICSGl7AUIDwP/uWjAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAA7IgRtYXBzVDoCJyIDeWVzIgRzZXRzLAJSIgVeXHcrJIACRAAAQB8lzHhCJAAM"
)
print(loads(serialized))
```
**Output**
```python
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
```javascript
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
```javascript
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**
```javascript
[
'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
]
```
Raw data
{
"_id": null,
"home_page": null,
"name": "v8serialize",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Hal Blackburn",
"author_email": "hwtb2@cam.ac.uk",
"download_url": "https://files.pythonhosted.org/packages/45/f7/b04a38e2f37194581090f16d71626fe34953022eb98f1ec4259ecc3a55e2/v8serialize-0.1.0.tar.gz",
"platform": null,
"description": "<p align=\"center\">\n <img src=\"docs/v8serialize_logo_auto.svg\" width=\"300\" alt=\"The v8serialize logo. Monochome. Large "V8" and smaller "serialize" in a handwritten style, with the 8 stylized to look like a snake.\">\n</p>\n\n<p align=\"center\"><em>Imagine having <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage\"><code>postMessage()</code></a> between JavaScript and Python.</em></p>\n\n<table align=\"center\">\n <tr>\n <td><pre lang=\"shell\">pip install v8serialize</pre></td>\n </tr>\n</table>\n\n<p align=\"center\"><a href=\"https://h4l.github.io/v8serialize/en/latest/\">Documentation</a></p>\n\n---\n\n# `v8serialize`\n\nA Python library to read & write JavaScript values in [V8 serialization format]\nwith Python.\n\n[V8 serialization format]:\n https://h4l.github.io/v8serialize/en/latest/explanation/v8_serialization_format.html\n\n## Examples\n\nThese examples demonstrate serializing and deserializing the same selection of\nJavaScript values in Python and JavaScript. JavaScript types supported by\nJavaScript's [`structuredClone()` algorithm] can be serialized.\n\n[`structuredClone()` algorithm]:\n https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm\n\n### Serialize with Python\n\n```python\nfrom base64 import b64encode\nfrom datetime import datetime, UTC\nimport re\n\nfrom v8serialize import dumps\nfrom v8serialize.jstypes import JSObject, JSArray, JSUndefined\n\nserialized = dumps(\n [\n \"strings \ud83e\uddf5\ud83e\uddf6\ud83e\udea1\",\n 123,\n None,\n JSUndefined,\n JSArray({0: 'a', 1: 'b', 123456789: 'sparse'}),\n JSObject({\"msg\": \"Hi\"}),\n b\"\\xc0\\xff\\xee\",\n 2**128,\n {\"maps\": True},\n {\"sets\", \"yes\"},\n re.compile(r\"^\\w+$\"),\n datetime(2024, 1, 1, tzinfo=UTC),\n ]\n)\n\nprint(b64encode(serialized).decode())\n```\n\n**Output**\n\n```\n/w9BDFMUc3RyaW5ncyDwn6e18J+ntvCfqqFVezBfYZaa7zpVAFMBYVUBUwFiVZWa7zpTBnNwYXJzZUADlprvOm9TA21zZ1MCSGl7AUIDwP/uWiIAAAAAAAAAAAAAAAAAAAAAATtTBG1hcHNUOgInUwRzZXRzUwN5ZXMsAlJTBV5cdyskgAJEAABAHyXMeEIkAAw=\n```\n\n### Deserialize with Python\n\n```python\nfrom base64 import b64decode\nfrom v8serialize import loads\n\n# The output of the JavaScript example\nserialized = b64decode(\n \"/w9BDGMccwB0AHIAaQBuAGcAcwAgAD7Y9d0+2PbdPtih3kn2ATBfYZaa7zpJACIBYUkCIgFiSaq03nUiBnNwYXJzZUADlprvOm8iA21zZyICSGl7AUIDwP/uWjAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAA7IgRtYXBzVDoCJyIDeWVzIgRzZXRzLAJSIgVeXHcrJIACRAAAQB8lzHhCJAAM\"\n)\nprint(loads(serialized))\n```\n\n**Output**\n\n```python\nJSArray([\n 'strings \ud83e\uddf5\ud83e\uddf6\ud83e\udea1',\n 123,\n None,\n JSUndefined,\n JSArray({\n 0: 'a',\n 1: 'b',\n 123456789: 'sparse',\n }),\n JSObject(msg='Hi'),\n JSArrayBuffer(b'\\xc0\\xff\\xee'),\n 340282366920938463463374607431768211456,\n JSMap({\n 'maps': True,\n }),\n JSSet([\n 'yes',\n 'sets',\n ]),\n JSRegExp(source='^\\\\w+$', flags=<JSRegExpFlag.UnicodeSets: 256>),\n datetime.datetime(2024, 1, 1, 0, 0),\n])\n```\n\n### Serialize with Node.js / Deno\n\n```javascript\nimport * as v8 from \"node:v8\";\n\nconst sparseArray = [\"a\", \"b\"];\nsparseArray[123456789] = \"sparse\";\n\nconst buffer = v8.serialize([\n \"strings \ud83e\uddf5\ud83e\uddf6\ud83e\udea1\",\n 123,\n null,\n undefined,\n sparseArray,\n { msg: \"Hi\" },\n Uint8Array.from([0xc0, 0xff, 0xee]).buffer,\n 2n ** 128n,\n new Map([[\"maps\", true]]),\n new Set([\"yes\", \"sets\"]),\n /^\\w+$/v,\n new Date(Date.UTC(2024, 0, 1)),\n]);\n\nconsole.log(buffer.toString(\"base64\"));\n```\n\n**Output**\n\n```\n/w9BDGMccwB0AHIAaQBuAGcAcwAgAD7Y9d0+2PbdPtih3kn2ATBfYZaa7zpJACIBYUkCIgFiSaq03nUiBnNwYXJzZUADlprvOm8iA21zZyICSGl7AUIDwP/uWjAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAA7IgRtYXBzVDoCJyIDeWVzIgRzZXRzLAJSIgVeXHcrJIACRAAAQB8lzHhCJAAM\n```\n\n## Deserialize with Node.js / Deno\n\n```javascript\nimport * as v8 from \"node:v8\";\n\n// The output of the Python example\nconst buffer = Buffer.from(\n \"/w9BDFMUc3RyaW5ncyDwn6e18J+ntvCfqqFVezBfYZaa7zpVAFMBYVUBUwFiVZWa7zpTBnNwYXJzZUADlprvOm9TA21zZ1MCSGl7AUIDwP/uWiIAAAAAAAAAAAAAAAAAAAAAATtTBG1hcHNUOgInUwN5ZXNTBHNldHMsAlJTBV5cdyskgAJEAABAHyXMeEIkAAw=\",\n \"base64\"\n);\nconsole.log(v8.deserialize(buffer));\n```\n\n**Output**\n\n```javascript\n[\n 'strings \ud83e\uddf5\ud83e\uddf6\ud83e\udea1',\n 123,\n null,\n undefined,\n [ 'a', 'b', <123456787 empty items>, 'sparse' ],\n { msg: 'Hi' },\n ArrayBuffer { [Uint8Contents]: <c0 ff ee>, byteLength: 3 },\n 340282366920938463463374607431768211456n,\n Map(1) { 'maps' => true },\n Set(2) { 'yes', 'sets' },\n /^\\w+$/v,\n 2024-01-01T00:00:00.000Z\n]\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Read & write JavaScript values from Python with the V8 serialization format.",
"version": "0.1.0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "23915d45845efba587dd6a7634e11e329eabc5978f0780dadcced605d57ea999",
"md5": "6c0ace820298bb50227ab72623ac2194",
"sha256": "5136e50c24308f9ddc7b8083ca34e7c65f57cd321dd703b9667708a8552eebed"
},
"downloads": -1,
"filename": "v8serialize-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6c0ace820298bb50227ab72623ac2194",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 98168,
"upload_time": "2024-09-24T14:28:25",
"upload_time_iso_8601": "2024-09-24T14:28:25.220714Z",
"url": "https://files.pythonhosted.org/packages/23/91/5d45845efba587dd6a7634e11e329eabc5978f0780dadcced605d57ea999/v8serialize-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "45f7b04a38e2f37194581090f16d71626fe34953022eb98f1ec4259ecc3a55e2",
"md5": "b2c5f3e801aa3e89b67c71d546d837ff",
"sha256": "bd330fb925be9c395d82ed4f048b78f0d560d4358b3061d149665d8a8cc60d86"
},
"downloads": -1,
"filename": "v8serialize-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "b2c5f3e801aa3e89b67c71d546d837ff",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 83518,
"upload_time": "2024-09-24T14:28:26",
"upload_time_iso_8601": "2024-09-24T14:28:26.721079Z",
"url": "https://files.pythonhosted.org/packages/45/f7/b04a38e2f37194581090f16d71626fe34953022eb98f1ec4259ecc3a55e2/v8serialize-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-24 14:28:26",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "v8serialize"
}