TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: V8serialize – Read/write V8-serialized JavaScript values from Python

3 pointsby h4l8 months ago
v8serialize is a Python library that reads and writes V8&#x27;s value serialization format[1][2], which is how V8 moves JS values between contexts when using postMessage(value), storing data in IndexedDB and Deno uses it to store values in its Deno KV database. Node.js and Deno have a v8.serialize() API to read&#x2F;write the format. The format supports most of the conventional JavaScript types, like Map, Set, BigInt, Date, ArrayBuffer, Error, undefined etc; but not object prototypes or functions. It&#x27;s a bit like JSON with more types and reference cycles.<p>The strength of the format is that it lets JavaScript programs serialize values without thinking about reducing types down to the subset that JSON supports. A weakness is that all the quirks of JavaScript&#x27;s types need to be exposed by another language deserializing it, so it&#x27;s certainly not ideal as a general-purpose format, just when ease of use from JavaScript is a priority.<p>This library lets Python programs work with JavaScript values by reading and writing this serialization format. As well as encoding&#x2F;decoding the format itself, the library implements Python types that replicate the behaviour of JavaScript&#x27;s Object, Array, Map, Set, etc, so that values can be round-tripped faithfully and quirks like JavaScript&#x27;s sparse arrays don&#x27;t cause Python to allocate massive contiguous lists.<p>I made this in an unplanned yak shave when setting out to write a client for Deno&#x27;s KV database in Python. I&#x27;d assumed it was just storing JSON-encoded values, but I quickly realised it could round-trip all the regular JavaScript types, like Date &amp; undefined, and realised it was using V8&#x27;s value serialization format to store values at rest.<p>I&#x27;m now back to implementing a Deno KV client using this library. It can imagine it could also be used for other things, such as having a Python program act like a JavaScript Web Worker, receiving JavaScript values from postMessage()-like calls. Or perhaps could be used to preserve full JavaScript types in something using Python in the browser (in conjunction with a pure-js v8.serialize() implementation, like <a href="https:&#x2F;&#x2F;github.com&#x2F;worker-tools&#x2F;v8-value-serializer">https:&#x2F;&#x2F;github.com&#x2F;worker-tools&#x2F;v8-value-serializer</a>).<p>1: <a href="https:&#x2F;&#x2F;h4l.github.io&#x2F;v8serialize&#x2F;en&#x2F;latest&#x2F;explanation&#x2F;v8_serialization_format.html" rel="nofollow">https:&#x2F;&#x2F;h4l.github.io&#x2F;v8serialize&#x2F;en&#x2F;latest&#x2F;explanation&#x2F;v8_s...</a> 2: <a href="https:&#x2F;&#x2F;github.com&#x2F;v8&#x2F;v8&#x2F;blob&#x2F;main&#x2F;src&#x2F;objects&#x2F;value-serializer.cc">https:&#x2F;&#x2F;github.com&#x2F;v8&#x2F;v8&#x2F;blob&#x2F;main&#x2F;src&#x2F;objects&#x2F;value-seriali...</a>

1 comment

ekzhang8 months ago
This is really neat! It looks like you implemented the parser and encoder entirely in Python, how does that compare to an approach where you load an extension module in C or Rust? (Possibly with the actual V8 source)
评论 #41638545 未加载