PHPackages                             oire/serializer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. oire/serializer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

oire/serializer
===============

Data serializer for better storage and exchange.

v1.1(9y ago)06MITPHPPHP &gt;= 7.1

Since Apr 23Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Oire/serializer)[ Packagist](https://packagist.org/packages/oire/serializer)[ RSS](/packages/oire-serializer/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Oiré Serializer — Data Serializer for Better Storage and Exchange
=================================================================

[](#oiré-serializer--data-serializer-for-better-storage-and-exchange)

[![Build Status](https://camo.githubusercontent.com/e59771170cb3c29d705254bc9ee24f6853d0cb6ea8158c89104cfe1520a6562d/68747470733a2f2f7472617669732d63692e6f72672f4f6972652f73657269616c697a65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Oire/serializer)[![MIT License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://github.com/Oire/serializer/blob/master/LICENSE)

Serializes various data to different formats. There is also a possibility to additionally encode the output to URL-and filename-safe base64.
Depends on [Oirë Base64](https://github.com/Oire/base64) for encoding binary data to a storable format.

Requirements
------------

[](#requirements)

Requires PHP 7.1 or later with JSON support enabled.
Requires additional PECL extensions for certain serialization modes, see below. If no PECL is available and none of the required extensions is installed in your environment (such a shared hosting), you can freely use Oirë Serializer with JSON.

Installation
------------

[](#installation)

Via [Composer](https://getcomposer.org/):

`composer require oire/serializer`

Or manually. Note that you will need `base64.php` from [Oirë Base64](https://github.com/Oire/base64/):

```
require_once("oire/base64.php");
require_once("oire/serializer.php");
```

Running Tests
-------------

[](#running-tests)

Run `phpunit` in the projects directory.

Usage Examples
--------------

[](#usage-examples)

Serialization to JSON. Note that you can either call methods consequently or chain them, like this:

```
use \Oire\Serializer;
$data = ["fruit" => "orange", "vegetable" => "carrot", "money" => 3000, "randomArray" => [1, 2, 3, 4, 5], "Lambë" => "Українська"];
try {
	// You can pass "j", "json" or 1 to setMode() to get JSON
	$jsonSerialized = (new Serializer())->setMode("json")->serialize($data);
} catch(Exception $e) {
	// Handle errors
}
```

This will output:

```
{"fruit":"orange","vegetable":"carrot","money":3000,"randomArray":[1,2,3,4,5],"Lambë":"Українська"}
```

Now unserializing:

```
// Without chaining it will be like this
$s = new Serializer();
// You may wrap this also with try...catch
$s->setMode("j");
try {
	$unserialized = $s->unserialize($jsonSerialized);
} catch(Exception $e) {
	// Handle errors
}
```

This will return the original array. If you want to get an object instead, pass `false` as the third parameter to `unserialize()`.
If you pass `true` as the second parameter to `serialize()`, the serialized data will be additionally encoded to URL-and filename-safe base64. This might be particularly useful if you choose a binary format such as MessagePack:

```
$msgPackSerialized = (new Serializer())->setMode("mp")->serialize($data, true);
```

This will output:

```
haVmcnVpdKZvcmFuZ2WpdmVnZXRhYmxlpmNhcnJvdKVtb25lec0LuKtyYW5kb21BcnJheZUBAgMEBaZMYW1iw6u00KPQutGA0LDRl9C90YHRjNC60LA

```

Supported Serialization Modes
-----------------------------

[](#supported-serialization-modes)

Currently the following modes are supported. Note, if the mode is binary, a raw binary string is returned if `$base64` parameter is left as `false` during serialization which can be uncomfortable for reading or storage.

- [JSON](http://json.org/), non-binary. Pass `1`, `"j"` or `"json"` to `setMode()` to set this mode.
- [MessagePack](http://msgpack.org/), binary. Pass `2`, `"m"`, `"mp"`, `"msgpack"` or `"messagepack"` to `setMode()` to set this mode. Note that the corresponding [PECL extension](https://pecl.php.net/package/msgpack) should be installed for this to work.
- [Igbinary](https://github.com/igbinary/igbinary), binary. Pass `3`, `"i"`, `"ib"`, `"ig"` or `"igbinary"` to `setMode()` to set this mode. Note that the corresponding [PECL extension](https://pecl.php.net/package/igbinary) should be installed for this to work.

Methods
-------

[](#methods)

The methods are documented in the source file, but their description is given below.
We recommend to wrap every call in `try...catch` since Oirë Serializer throws exceptions in case of errors.

- Class constructor— you might provide a serialization mode when calling the constructor or directly call `setMode()`.
- `setMode(int|string $mode)` — accepts either a numeric representation of the mode or a readable string such as `"json"`. See the Supported Modes section above. Chainable, so returns the current class instance.
- `getMode(bool $asString = false): int|string` — gets the current serialization mode set by `setMode()`. If `$asString` is set to `true`, returns a readable mode name such as `"json"`, a numeric representation is returned otherwise (it is the default behavior). Throws an exception if the mode is not set or if it could not be found.
- `getAvailableModes(bool $json = false): array|string` — gets all available serialization modes. If the `$json` parameter is set to `true`, returns them as a JSON string, an associative array is returned otherwise (this is the default behavior).
- `serialize(mixed $data, bool $base64 = false): string` — serializes given data according to the serialization mode set with `setMode()`. If `$base64` is set to `true`, additionally encodes the serialized data to URL-and filename-safe base64 (particularly useful for binary serialization formats such as MessagePack). If set to `false` (default), the serialized data is returned as a string, be it binary or not.
- `unserialize(string $data, bool $base64 = false, bool $assoc = true): mixed` — unserializes given data according to the serialization mode set with `setMode()`. If `$base64` is set to `true`, assumes that the data had been additionally encoded to URL-safe base64 after serialization. If `$assoc` is set to `true` (default), returns an associative array, an object is returned otherwise. Note that the last parameter is applicable only to JSON serialization.

License
-------

[](#license)

Copyright © 2017, Andre Polykanine also known as Menelion Elensúlë.
This software is licensed under an MIT license.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

2

Last Release

3348d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/67d293487324e144a24cd2e8bc374a33d28e15716fd9a222ccf4f0a2d9036323?d=identicon)[Oire](/maintainers/Oire)

---

Tags

serializationdeserializationsingle-file

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/oire-serializer/health.svg)

```
[![Health](https://phpackages.com/badges/oire-serializer/health.svg)](https://phpackages.com/packages/oire-serializer)
```

###  Alternatives

[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k141.9M920](/packages/jms-serializer)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k92.4M673](/packages/jms-serializer-bundle)[flix-tech/avro-serde-php

A library to serialize and deserialize Avro records making use of the confluent schema registry

684.2M20](/packages/flix-tech-avro-serde-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
