Implementation:Microsoft Playwright UtilityScriptSerializers
| Knowledge Sources | |
|---|---|
| Domains | Serialization, Cross-Context Communication |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for serializing and deserializing JavaScript values across execution contexts provided by the Playwright library.
Description
The `utilityScriptSerializers.ts` module provides functions for serializing JavaScript values into a portable `SerializedValue` format and deserializing them back. This enables passing complex values between the Node.js process and browser execution contexts. The `SerializedValue` union type supports primitives (boolean, number, string), special values (null, undefined, NaN, Infinity, -0), Date objects, URL objects, BigInt, Error objects (with name, message, stack), RegExp, arrays and objects (with circular reference tracking via `ref`/`id`), JSHandle references (`h`), TypedArrays (Int8Array, Uint8Array, Float64Array, etc.), and ArrayBuffers. The `serializeAsCallArgument` function converts values for browser-side evaluation, while `parseEvaluationResultValue` reconstructs values from browser responses. The module is isomorphic and works in both Node.js and browser environments.
Usage
Use this module when passing arguments to `page.evaluate()` or receiving results from browser-side JavaScript evaluation. It is the serialization layer between the Playwright server and the injected utility script.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/utils/isomorphic/utilityScriptSerializers.ts
Signature
export type SerializedValue =
undefined | boolean | number | string |
{ v: 'null' | 'undefined' | 'NaN' | 'Infinity' | '-Infinity' | '-0' } |
{ d: string } | // Date
{ u: string } | // URL
{ bi: string } | // BigInt
{ e: { n: string, m: string, s: string } } | // Error
{ r: { p: string, f: string } } | // RegExp
{ a: SerializedValue[], id: number } | // Array
{ o: { k: string, v: SerializedValue }[], id: number } | // Object
{ ref: number } | // Circular reference
{ h: number } | // JSHandle
{ ta: { b: string, k: TypedArrayKind } } | // TypedArray
{ ab: { b: string } }; // ArrayBuffer
export function serializeAsCallArgument(value: any, handleSerializer: (value: any) => HandleOrValue): SerializedValue;
export function parseEvaluationResultValue(value: SerializedValue, handles?: any[]): any;
Import
import { serializeAsCallArgument, parseEvaluationResultValue, SerializedValue } from '../utils/isomorphic/utilityScriptSerializers';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | any | Yes | JavaScript value to serialize or deserialized result to parse |
| handleSerializer | (value: any) => HandleOrValue | Yes | Callback to handle JSHandle references during serialization |
| handles | any[] | No | Array of handle objects for resolving handle references during deserialization |
Outputs
| Name | Type | Description |
|---|---|---|
| SerializedValue | SerializedValue | Portable representation of the JavaScript value |
| deserialized | any | Reconstructed JavaScript value from serialized form |
Usage Examples
import { serializeAsCallArgument, parseEvaluationResultValue } from 'playwright-core/lib/utils/isomorphic/utilityScriptSerializers';
// Serialize a complex value
const serialized = serializeAsCallArgument(
{ name: 'test', date: new Date('2024-01-01'), pattern: /hello/gi },
(value) => ({ fallThrough: value })
);
// Deserialize a browser result
const result = parseEvaluationResultValue({
o: [
{ k: 'title', v: 'Hello World' },
{ k: 'count', v: 42 },
],
id: 1
});
// result = { title: 'Hello World', count: 42 }