php-serialize
Serialize and unserialize PHP's serialized data format in JavaScript
Repository Health
Technical Analysis
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
Serializableinterface convention via optionalserialize()/unserialize()methods on passed classes - A
phpToJsScopemapping 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.
Used by 5 apps in this directory
Amplication
Developer Tools · AI Code Assistants · Automation
Create production-ready backend services with your organization's standards baked in — generating NestJS, Prisma, and GraphQL code that you own and control.
Another Redis Desktop Manager
Developer Tools · Databases
Fast, stable Redis GUI with cluster, SSH, and massive key support
Automatisch
Automation · No Code Platforms
Self-hosted, no-code workflow automation that keeps your data on your own servers—a privacy-first alternative to Zapier with 90+ integrations.
Rallly
Scheduling
Self-hostable group scheduling polls that eliminate back-and-forth emails and find the best meeting time for everyone.
Swetrix
Analytics
Privacy-first, cookieless web analytics with error tracking, session replays, and performance monitoring — self-host or use Cloud.