# php-obj-deserializer

> deserialize php objects to javascript objects

Latest version **0.1.4** (published 2022-05-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install php-obj-deserializer
pnpm add php-obj-deserializer
yarn add php-obj-deserializer
bun add php-obj-deserializer
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2022-05-20 |
| First published | 2022-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 38.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | HBeserra |
| Maintainers | hbeserra |
| Keywords | php, objects, js |

## Links

- npm: https://www.npmjs.com/package/php-obj-deserializer
- Repository: https://github.com/HBeserra/PHP-object-deserializer
- Homepage: https://github.com/HBeserra/PHP-object-deserializer#readme
- Issues: https://github.com/HBeserra/PHP-object-deserializer/issues
- npm.io page: https://npm.io/package/php-obj-deserializer

## Dependencies (1)

- [typescript](https://npm.io/package/typescript.md) ^4.6.4

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.1.4 (latest) — 2022-05-20
- 0.1.3 — 2022-05-19
- 0.1.2 — 2022-05-18
- 0.1.1 — 2022-05-18
- 0.1.0 — 2022-05-15

## README

# PHP Object Deserializer
Convert serialized PHP data to a javascript.


# This fork
As the origin by [bd808](https://github.com/bd808/php-unserialize-js) was written in pure javascript I decided to rewrite using typescript.


### Example 

**PHP:**
```
$array = array(
  "foo" => "bar",
  "foobar" => "baz"
);
echo serialize($array);
```

**JS/TS**
```
import phpObjectDeserializer from "php-obj-deserializer";

const php_str = 'a:2:{s:3:\"foo\";s:3:\"bar\";s:6:\"foobar\";s:3:\"baz\";}'

phpObjectDeserializer(php_str)
  .then(console.log)
  .catch(console.error)
```

**Result:**
```
{ 
  foo:    'bar',
  foobar: 'baz' 
}
```

#### CommonJS

```
var phpObjDeserializer = require("php-obj-deserializer")

var php_str = 'a:2:{s:3:\"foo\";s:3:\"bar\";s:6:\"foobar\";s:3:\"baz\";}'

phpObjDeserializer.default()
  .then(console.log)
  .catch(console.error)
```

## Anatomy of a PHP serialize()'ed value:
|Type|Anatomy|--|
|--|--|--|
|**String**|`s:size:value;`|String values are always in double quotes|
|**Integer**|`i:value;`|
|**Boolean**|`b:value;`|does not store `true` or `false`, does store `1` or `0`|
|**Null**|`N;`|
|**Array**|`a:size:{key;value;...}`| `key` and `value` repeat for all elements. Array keys are **always** integers or strings|
|**Object**|`O:strlen(object_name):object_name:object_size:{s:strlen(property_name):property_name:property_definition;...}`| `strlen` of property name, `property_name` and `property_definition` repeat for all properties in the object|

**PHP Serialization Function Implementation Notes by the comunity:**

    "null => 'value'" equates to 's:0:"";s:5:"value";'
    "true => 'value'" equates to 'i:1;s:5:"value";'
    "false => 'value'" equates to 'i:0;s:5:"value";'
  
    "array(whatever the contents) => 'value'" equates to an "illegal offset type" warning because you can't use an array as a key; however, if you use a variable containing an array as a key, it will equate to 's:5:"Array";s:5:"value";', and attempting to use an object as a key will result in the same behavior as using an array will.

## Sources

- [php manual of serialize function](https://www.php.net/manual/pt_BR/function.serialize.php)
- [bd808](https://github.com/bd808/php-unserialize-js)

---
_Source: https://npm.io/package/php-obj-deserializer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
