Implementation:Microsoft Playwright Protocol Serializers
| Knowledge Sources | |
|---|---|
| Domains | Protocol, Serialization |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for serializing and deserializing JavaScript values across the Playwright protocol boundary provided by the Playwright library.
Description
The Protocol Serializers module provides two core functions, `parseSerializedValue` and `serializeValue`, that handle the round-trip conversion of JavaScript values into a compact `SerializedValue` wire format. It supports primitives (numbers, strings, booleans), special values (undefined, null, NaN, Infinity, -0), as well as complex types such as Date, URL, BigInt, Error, RegExp, typed arrays, ArrayBuffer, Map, Set, and plain objects/arrays. It uses a reference-tracking mechanism with `ref`/`id` pairs to handle circular references and shared object identity.
Usage
Use Protocol Serializers when transmitting JavaScript values between the Playwright client and server processes over the protocol channel, including function arguments for `page.evaluate()`, return values, and event payloads.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/protocol/serializers.ts
Signature
export function parseSerializedValue(value: SerializedValue, handles: any[] | undefined): any;
export function serializeValue(value: any, handleSerializer: (value: any) => HandleOrValue, visitedValues: VisitedValues): SerializedValue;
Import
import { parseSerializedValue, serializeValue } from '../protocol/serializers';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | SerializedValue / any | Yes | The serialized wire-format value to parse, or the JavaScript value to serialize |
| handles | any[] or undefined | Yes (parse) | Array of object handles referenced by index in the serialized form |
| handleSerializer | (value: any) => HandleOrValue | Yes (serialize) | Callback to convert non-serializable objects into handle references |
| visitedValues | VisitedValues | Yes (serialize) | Map tracking visited objects to handle circular references |
Outputs
| Name | Type | Description |
|---|---|---|
| result | any / SerializedValue | The deserialized JavaScript value, or the serialized wire-format representation |
Usage Examples
import { parseSerializedValue, serializeValue } from '../protocol/serializers';
// Parse a serialized number
const num = parseSerializedValue({ n: 42 }, undefined); // 42
// Parse a serialized Date
const date = parseSerializedValue({ d: '2024-01-01T00:00:00Z' }, undefined); // Date object
// Serialize a value for protocol transmission
const serialized = serializeValue({ key: 'value' }, (v) => ({ value: v }), new Map());