{"id":20638654,"url":"https://github.com/bd808/php-unserialize-js","last_synced_at":"2025-08-22T11:13:47.650Z","repository":{"id":7539339,"uuid":"8891507","full_name":"bd808/php-unserialize-js","owner":"bd808","description":"Convert serialized PHP data to a javascript object graph.","archived":false,"fork":false,"pushed_at":"2022-12-18T01:25:33.000Z","size":417,"stargazers_count":55,"open_issues_count":1,"forks_count":15,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-15T21:52:46.042Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bd808.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-03-19T23:02:48.000Z","updated_at":"2025-04-09T12:50:40.000Z","dependencies_parsed_at":"2022-09-01T18:11:06.638Z","dependency_job_id":null,"html_url":"https://github.com/bd808/php-unserialize-js","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd808%2Fphp-unserialize-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd808%2Fphp-unserialize-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd808%2Fphp-unserialize-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd808%2Fphp-unserialize-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bd808","download_url":"https://codeload.github.com/bd808/php-unserialize-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249161130,"owners_count":21222469,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T15:19:01.810Z","updated_at":"2025-04-15T21:52:50.852Z","avatar_url":"https://github.com/bd808.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm version](https://img.shields.io/npm/v/phpunserialize.svg?style=flat)](https://www.npmjs.com/package/phpunserialize)\n\nphpUnserialize\n==============\n\nConvert serialized PHP data to a javascript object graph.\n\n\nWhy?\n----\n\u003e OMG why would anyone do something this perverse? PHP has a `json_encode()`\n\u003e method so you don't have to try and cobble together ugly hacks like this.\n\nIt all started so innocently. The guy at the desk next to mine asked \"hey is\nthere a javascript library that can turn this php serialize mess into\nsomething that I can read?\" I gaped. He explained that he was trying to slap\ntogether a js testing harness for a set of REST services that returned\nserialized PHP as their transport representation.\n\nA [google search][] turned up [something][] so I went back to listening to the\nlatest [OMM][] album. Fifteen minutes later the stream of curses coming from\nGallilama started harshing my groove. It turns out that the venerable phpjs\nfunction only handles a particular subset of PHP's `serialize` output.\nSpecifically it doesn't handle references and objects at all. Google found\na [java implementation][] that looked more complete. I did a quick port of it\nto javascript and moved on to my [$wingin' Utter$][] playlist.\n\nThe next day I checked in and found out that strange things were afoot with my\nport. It turns out that private and protected members `serialize` in an\n\"interesting\" way. PHP prepends the member name with either the class name\n(private) or an asterisk (protected) surrounded by null bytes (\\u0000). The\nhack parser was going into an infinite loop when it tried to extract these\nvalues.\n\nBy this point I was fully committed. Nothing less than a TDD validated library\nthat could handle just about any craziness I threw at it would do. I'm sure\nthere are still gaps, but this \"quick hack\" is working for our twisted needs.\n\n\nImplementation Details\n----------------------\nPHP's serialization format is not well documented, but this function takes\na best guess approach to parsing and interpreting it. Serialized integers,\nfloats, booleans, strings, arrays, objects and references are currently\nsupported.\n\nPHP's array type is a hybrid of javascript's array and object types.\nphpUnserialize translates PHP arrays having only 0-based consecutive numeric\nkeys into javascript arrays. All other arrays are translated into javascript\nobjects.\n\nSerialized members of a PHP object carry scope information via name mangling.\n`phpUnserialize` strips this scope signifier prefix from private and protected\nmembers.\n\nCheck out the [tests][] for more details or read the source.\n\n\nUsage\n-----\nThe `phpUnserialize.js` file implements the [Universal Module Definition][]\npattern which attempts to be compatible with multiple script loaders including\n[AMD][], [CommonJS][] and direct usage in an HTML file.\n\nPlain HTML:\n```html\n\u003cscript src=\"phpUnserialize.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  var foo = phpUnserialize('s:3:\"foo\";');\n\u003c/script\u003e\n```\n\nWith an [AMD][] loader:\n```javascript\ndefine([\"phpunserialize\"], function (phpUnserialize) {\n  return {\n    foo: phpUnserialize('s:3:\"foo\";')\n  };\n});\n```\n\nWith a [CommonJS][] loader:\n```javascript\nvar phpUnserialize = require('phpunserialize');\nvar foo = phpUnserialize('s:3:\"foo\";');\n```\n\nRunning the Unit Tests\n----------------------\n```sh\nnpm install\nnpm test\n```\n\n---\n[google search]: https://www.google.com/search?q=php+unserialize+javascript\n[something]: http://phpjs.org/functions/unserialize/\n[OMM]: http://www.oldmanmarkley.com/\n[java implementation]: https://code.google.com/p/serialized-php-parser\n[$wingin' Utter$]: http://swinginutters.com/\n[tests]: php-unserialize.spec.js\n[Universal Module Definition]: https://github.com/umdjs/umd\n[AMD]: https://github.com/amdjs/amdjs-api/blob/master/AMD.md\n[CommonJS]: http://wiki.commonjs.org/wiki/CommonJS\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbd808%2Fphp-unserialize-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbd808%2Fphp-unserialize-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbd808%2Fphp-unserialize-js/lists"}