php-serialize

Serialize and unserialize PHP's serialized data format in JavaScript

Library
npm
v5.1.3
120stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
40/100Fair
Development Activity4
Maintenance20
Community64
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
63/100Good
Architecture62
Code Quality68
Innovation50
Learning Curve70

php-serialize implements PHP’s native serialize()/unserialize() string format in JavaScript, so a Node.js or browser application can read and write data produced by (or destined for) a PHP system without shelling out to PHP itself. It supports the full range of PHP serialized types — scalars, arrays, associative arrays, and objects — including PHP’s Serializable interface convention for custom object encode/decode logic.

This is a common need when a JavaScript service sits alongside a legacy PHP application (e.g. WordPress, Laravel, or a custom CMS) and has to read serialized columns directly from a shared database, or when migrating a PHP codebase to Node incrementally and needing a compatibility shim for stored data during the transition.

What You Get

  • A serialize() function that converts JS primitives, arrays, and class instances into PHP’s serialized string format
  • An unserialize() function that parses PHP serialized strings back into JS values, arrays, or class instances
  • Support for PHP’s Serializable interface convention via optional serialize()/unserialize() methods on passed classes
  • A phpToJsScope mapping so unserialized PHP objects can be reconstructed as specific JS classes, including namespaced PHP class names
  • An isSerialized() helper to detect whether a given string is valid PHP serialized data before attempting to parse it
  • Full TypeScript typings bundled with the package

Common Use Cases

  • Reading serialized columns (e.g. WordPress wp_options, Laravel session data) directly from a shared MySQL/Postgres database from a Node service
  • Building a Node-based migration or ETL tool that converts a legacy PHP application’s stored data into a new format
  • Interoperating with a PHP backend’s API or cache layer that exchanges data in PHP’s native serialized format instead of JSON

Under The Hood

Architecture: The library is split into focused modules under src/: parser.ts implements a recursive-descent parser over the raw serialized string (tracking a cursor position through nested arrays/objects), serialize.ts walks a JS value and emits the equivalent PHP token stream, unserialize.ts reconstructs JS values (and optionally class instances via a phpToJsScope map) from parsed tokens, and isSerialized.ts/helpers.ts provide validation and shared utilities. index.ts re-exports the public API. There’s no external dependency — the whole format is hand-parsed.

Tech Stack: Written in TypeScript, compiled to separate CommonJS and ES module builds via tsc, with a bundled .d.ts typings build. Test suite uses ava with ts-node for direct TypeScript execution, plus snapshot testing. A companion test/serialize.php script cross-checks output against real PHP serialize() calls, giving the test suite actual PHP-format ground truth rather than only self-consistency checks.

Code Quality: Each module is under 130 lines, with dedicated test files per feature (serialize-test.ts, unserialize-test.ts, isSerialized-test.ts) plus snapshot fixtures. Cross-validation against real PHP output is a notable quality signal for a format-compatibility library. Linting combines tsc --noEmit, ESLint, and Prettier. Development activity has slowed (health score flags low recent activity), though the API surface is stable and small enough that this matters less than for a larger project.

API Design: Three functions (serialize, unserialize, isSerialized) cover the entire public surface, mirroring PHP’s own function names for immediate familiarity to anyone who has used PHP’s native serialization. Optional phpToJsScope and options parameters (encoding, strict mode) are opt-in, so the zero-argument case just works for scalar/array data.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search