PHPackages                             suin/marshaller - 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. suin/marshaller

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

suin/marshaller
===============

Type-safe data mapping between JSON and a PHP class object.

1.0.1(8y ago)873[1 issues](https://github.com/suin/php-marshaller/issues)MITPHPPHP &gt;=7.1

Since Dec 29Pushed 8y ago1 watchersCompare

[ Source](https://github.com/suin/php-marshaller)[ Packagist](https://packagist.org/packages/suin/marshaller)[ RSS](/packages/suin-marshaller/feed)WikiDiscussions master Synced 2d ago

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

Marshaller
==========

[](#marshaller)

[![travis-ci-badge](https://camo.githubusercontent.com/f0441a58ee317fb46518781bc4b46142bf08f4edc1ecc041737e7aaa476ef97a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7375696e2f7068702d6d61727368616c6c65722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/suin/php-marshaller) [![packagist-dt-badge](https://camo.githubusercontent.com/9270cbfaec7450fba2af5e300bb7fa42032a72ddd75e8ab18fff4ea6338880b6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7375696e2f6d61727368616c6c65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/suin/marshaller) [![license-badge](https://camo.githubusercontent.com/2bcefced31878fd1efc73ec909ea083d742248d1d005e6d068649d0175212fe6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7375696e2f7068702d6d61727368616c6c65722e7376673f7374796c653d666c61742d737175617265)](LICENSE.md) [![release-version-badge](https://camo.githubusercontent.com/ad900eaa9435c3c24301851e66ffd8c9313ac6b3616f1325f72e059580595ce8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7375696e2f6d61727368616c6c65722e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://packagist.org/packages/suin/marshaller) [![php-version-badge](https://camo.githubusercontent.com/91b729377a4e7a5a3c0ff3eb742528f50b5c7d4871edacdec8a73665cb637bc4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7375696e2f6d61727368616c6c65722e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/91b729377a4e7a5a3c0ff3eb742528f50b5c7d4871edacdec8a73665cb637bc4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7375696e2f6d61727368616c6c65722e7376673f7374796c653d666c61742d737175617265)

Type-safe data mapping between JSON and a PHP class object.

Features
--------

[](#features)

1. Transforms JSON to PHP class object.
2. Transforms PHP object to JSON.

How does it works?
------------------

[](#how-does-it-works)

Marshaller analyze all private properties and convert them into JSON. Unmarshaller analyze the signature of the constructor of the given class and convert JSON into an object.

Installation via Composer
-------------------------

[](#installation-via-composer)

```
$ composer require suin/marshaller
```

Usage
-----

[](#usage)

### Marshal object and Unmarshal JSON

[](#marshal-object-and-unmarshal-json)

This is the simplest use case of Marshaller.

```
use Suin\Marshaller\JsonMarshaller;
use Suin\Marshaller\StandardProtocol;

// Transform an object to JSON.
$marshaller = new JsonMarshaller(new StandardProtocol);
$json = $marshaller->marshal(new Cat('Oliver'));
var_dump($json);
// Output:
// string(17) "{"name":"Oliver"}"

// Transform JSON to an object.
$cat = $marshaller->unmarshal($json, Cat::class);
var_dump($cat);
// Output:
// object(Cat)#%d (1) {
//   ["name":"Cat":private]=>
//   string(6) "Oliver"
// }
```

Please see [example#01](./example/01-marshal-object-and-unmarshal-json.php) for details.

### Protocols

[](#protocols)

Sometimes you would want to decode a JSON value in special transform way. In such a case, you can also define transforming rules between a PHP object and a JSON object by using `Protocol`s and `Format`s.

A `Format` describes how a single class or type becomes JSON and vice versa. All `Format`s must follow the interface below:

```
interface Format {
  public function read(A $jsonValue): B
  public function write(B $object): A
}
```

For example, a `Format` is implemented like this:

```
class HealthFormat // naming rule: class name + "Format"
{
    // Define how transform a JSON value to a PHP object.
    public function read(string $health): Health
    {
        return new Health($health === 'healthy');
    }

    // Define how transform a PHP object to a JSON value.
    public function write(Health $health): string
    {
        return $health->isHealthy() ? 'healthy' : 'sick';
    }
}
```

Also, a protocol is a class that have a collection of `Format`s. The following example has only one format, but some more formats would be included here in real use case.

```
class HealthProtocol extends StandardProtocol
{
    public function __construct()
    {
        parent::__construct(
            new HealthFormat
        );
    }
}
```

To see complete example code of protocols and formats, please see [example#02](./example/02-define-protocol.php). Also a complex example is seen in [./tests/ExampleModel/StudentProtocol.php](./tests/ExampleModel/StudentProtocol.php)

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more details.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for more details.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~5 days

Total

2

Last Release

3101d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/df0e4439766a9bc915e2db0e797f10f222b701133dd0c20fc2ddcc658078eb9f?d=identicon)[suin](/maintainers/suin)

---

Top Contributors

[![suin](https://avatars.githubusercontent.com/u/855338?v=4)](https://github.com/suin "suin (11 commits)")

---

Tags

classdecodeencodejsonmarshallerobjectjsonclassobjectmarshallerencoderdecoderunmarshaller

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/suin-marshaller/health.svg)

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

###  Alternatives

[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6753.8k](/packages/sbsaga-toon)[karriere/json-decoder

JsonDecoder implementation that allows you to convert your JSON data into PHP class objects

141445.7k12](/packages/karriere-json-decoder)[suin/json

A Simple wrapper of json\_decode() and json\_encode(). This provides object-oriented interface and exception-based error handing.

1027.6k3](/packages/suin-json)[ovidigital/js-object-to-json

PHP library to convert a JavaScript object string to JSON formatted string

18183.6k7](/packages/ovidigital-js-object-to-json)[shaarli/netscape-bookmark-parser

Generic Netscape bookmark parser

2647.1k](/packages/shaarli-netscape-bookmark-parser)

PHPackages © 2026

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