npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

php2json

v0.9.0

Published

Parse PHP serialize into JSON format

Downloads

36

Readme

PHP2JSON

Supports unserializing PHP serialize data into a JS object that can be parsed into JSON for further processing.

Does not require custom JS Classes for unserializing data.

Can be imported into JS as a ESM module or used a CLI command.

Installation

npm install php2json

Usage with JS

Use with JS:

import { unserializePHP } from 'php2json'

const data = unserializePHP('O:11:"MyClass":2:{s:3:"foo";s:3:"bar";s:3:"baz";i:123}')
console.log(data) // { "_type": "MyClass", "foo": "bar", "baz": 123 }

It is recommended to encode serialized data into base64 before porting it to other environments.

  1. In PHP, encode into base64:
serialized($myObj); // 'O:11:"MyClass":2:{s:3:"foo";s:3:"bar";s:3:"baz";i:123;}'
base64_encode(serialized($myObj)); // 'Tzo3OiJNeUNsYXNzIjoyOntzOjM6ImZvbyI7czozOiJiYXIiO3M6MzoiYmF6IjtpOjEyMzt9='
  1. Use the decode flag to decode the string from base64 before deserialization:
import { unserializePHP } from 'php2json'

const data = unserializePHP('Tzo3OiJNeUNsYXNzIjoyOntzOjM6ImZvbyI7czozOiJiYXIiO3M6MzoiYmF6IjtpOjEyMzt9=', { decode: 'base64' })
console.log(data) // { "_type": "MyClass", "foo": "bar", "baz": 123 }

Usage as a CLI Command

Install globally:

npm install -g php2json

Use as a regular CLI command by passing stdin:

> echo 'O:11:"MyClass":2:{s:3:"foo";s:3:"bar";s:3:"baz";i:123;}' | php2json
{"_type":"MyClass","foo":"bar","baz":123}
> php2json --help
Supports unserializing PHP data into a JS object that can be parsed into JSON for further processing.

Flags:
    --decode=base64 - Decode Base64 string into UTF-8 before processing.

This allows for nice data processing with nushell. E.g. read serialized string → convert to JSON → convert to structured data → interactively explore the result:

nu> 'O:11:"MyClass":2:{s:3:"foo";s:3:"bar";s:3:"baz";i:123;}' | php2json | from json | explore

Grammar

The grammar was taken from Parsing serialized PHP data with BNF using Instaparse - Stack Overflow.

<S> = expr
<expr> = (string | integer | double | boolean | null | array | object | reference | enum)+
<digit> = #'[0-9]'
<number> = negative* (decimal-num | integer-num)
<negative> = '-'
<integer-num> = digit+
<decimal-num> = integer-num '.' integer-num
<zero-or-one> = '0'|'1'
size = refIndex = digit+
key = (string | integer)
<val> = expr
array = <'a:'> <size> <':{'> (key val)+ <'}'> <';'>?
boolean = <'b:'> zero-or-one <';'>
null = <'N;'>
integer = <'i:'> number <';'>
double = <'d:'> number <';'>
string = <'s:'> <size> <':"'> characters <'";'>
object = <'O:'> <size> <':{'> (string val)+ <'}'> <';'>?
reference = <'r:'> <refIndex>
enum = <'E:'> <size> <':"'> characters <'";'>

Unserialize to JS types

Primitive types

'N;' → null
'b:0;' → false
's:5:"hello";' → "hello"
'i:123;' → 123
'd:123.45;' → 123.45

Complex types

  • Enums:

Due to the lack of enums support in JavaScript, the enum expressions are parsed into plain strings.

'E:17:"Weekdays:Saturday";' → "Weekdays:Saturday;"
  • Arrays:

Array is converted into a JS object if there is at least 1 non-numeric key.

'a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}' → [1, 2, 3]

'a:3:{i:0;i:1;s:1:"a";i:2;i:2;i:3;}' → { "0": 1, "a": 2, "2": 3 }
  • Objects:
'O:11:"MyClass":2:{s:3:"foo";s:3:"bar";s:3:"baz";i:123;}' → { "_type": "MyClass", "foo": "bar", "baz": 123 }


'O:2:"MyClass":1:{s:1:"a";i:10;s:1:"b";r:1;}}' → { _type: "MyClass", a: 10, b: [Circular] }

References

  1. Parsing serialized PHP data with BNF using Instaparse - Stack Overflow
  2. PHP Documentation: serialize