PHPackages                             sanmai/json-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. sanmai/json-serializer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sanmai/json-serializer
======================

Flexible JSON Serializer

0.2.9(2w ago)146.1k↑55.6%[1 PRs](https://github.com/sanmai/json-serializer/pulls)4MITPHPPHP &gt;=8.2CI passing

Since Sep 28Pushed 2w ago1 watchersCompare

[ Source](https://github.com/sanmai/json-serializer)[ Packagist](https://packagist.org/packages/sanmai/json-serializer)[ RSS](/packages/sanmai-json-serializer/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (4)Dependencies (30)Versions (15)Used By (4)

Flexible JSON Serializer
========================

[](#flexible-json-serializer)

[![Latest Stable Version](https://camo.githubusercontent.com/20076afa149b298d606e8f72b29365f8eece719cb8aa21fc494c53d0827348db/68747470733a2f2f706f7365722e707567782e6f72672f73616e6d61692f6a736f6e2d73657269616c697a65722f762f737461626c65)](https://packagist.org/packages/sanmai/json-serializer)[![Coverage Status](https://camo.githubusercontent.com/b57507304677c705cf32c7b926132f63872532bc18f82228ef7a49887ec5ea23/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73616e6d61692f6a736f6e2d73657269616c697a65722f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/sanmai/json-serializer?branch=main)

This library is a thin wrapper around [jms/serializer](https://github.com/schmittjoh/serializer).

```
composer require sanmai/json-serializer

```

This library makes it simpler to serialize and deserialize arrays of objects, scalar values, and plain objects. All you need is to follow a simple protocol.

How it works
------------

[](#how-it-works)

The library provides one general rule for deserializing every JSON value into a domain type.

Normally, application code using JMS Serializer follows a simple and predictable pattern:

```
$result = $serializer->deserialize($json, MyType::class, 'json');
```

This works well for objects and generalizes naturally to different domain types. However, the pattern breaks for root-level arrays and scalar values. The caller must use JMS type expressions and primitive type strings instead of a class. For example, deserializing this array:

```
[
    {"name": "foo"},
    {"name": "bar"}
]
```

Requires an out-of-pattern string type:

```
$items = $serializer->deserialize($json, sprintf('array', ItemExample::class), 'json');
```

A scalar value requires a similar exception:

```
$number = $serializer->deserialize($json, 'int', 'json');
```

Consequently, the caller has to use a different kind of type declaration to handle a plain array or primitive value instead of a domain object.

This library restores the general class-based rule for all three JSON shapes:

```
$item = $serializer->deserialize($json, ItemExample::class);
$items = $serializer->deserialize($json, ItemListExample::class);
$number = $serializer->deserialize($json, ScalarValueExample::class);
```

A class like `ItemExample` already declares its own fields and its own serialization guidance. Arrays and scalar values have no such place, so that knowledge leaks into the calling code, or into a serializer adapter class.

The interfaces this library adds put that knowledge in the type itself, turning lists and scalars into first-class domain members. So there is no extra class and no adapter layer, and nothing about a type lives outside of it. Calling code can then ask for a domain type whatever the shape of the JSON, and the library handles the rest.

ItemList
--------

[](#itemlist)

JMS Serializer supports deserializing arrays out of the box, but it is ever so slightly complicated since a user must specify a type in a full form, as in `array`, all the while returned deserialized value will be a plain untyped array.

This library abstracts away this extra complexity by providing a two-method protocol instead to constructs the domain collection.

Here is how it looks:

```
use JSONSerializer\Contracts\ItemList;

class ItemListExample implements ItemList
{
    /** @var ItemExample[] */
    public array $items = [];

    public static function getListType(): string
    {
        return ItemExample::class;
    }

    public static function withList(array $list)
    {
        $itemList = new self();
        $itemList->items = $list;

        return $itemList;
    }
}
```

From a JSON array:

```
[
    {"name": "foo"},
    {"name": "bar"}
]
```

With an all-familiar method:

```
use JSONSerializer\Serializer;

$serializer = new Serializer();

$result = $serializer->deserialize($json, ItemListExample::class);
```

This leaves an instance of `ItemListExample` in `$result`, with `$result->items` holding the two items from the source array.

ScalarValue
-----------

[](#scalarvalue)

There is a similar convenience interface called `ScalarValue` to aid with deserializing wrapped primitive scalar values.

```
use JSONSerializer\Contracts\ScalarValue;

class ScalarValueExample implements ScalarValue
{
    public int $value;

    public static function withValue($value)
    {
        $item = new self();
        $item->value = $value;

        return $item;
    }

    public static function getType(): string
    {
        return 'int';
    }
}
```

Troubleshooting
---------------

[](#troubleshooting)

### Class "JSONSerializer\\Serializer" not found

[](#class-jsonserializerserializer-not-found)

If you are getting an error that class `JSONSerializer\Serializer` was not found, you must have forgotten to do:

```
composer require sanmai/json-serializer

```

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance96

Actively maintained with recent releases

Popularity30

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 85.4% 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 ~193 days

Recently: every ~269 days

Total

12

Last Release

19d ago

PHP version history (4 changes)0.1PHP ^7.3 || ^8.0

0.2.3PHP &gt;=7.3

0.2.5PHP &gt;=7.4

0.2.7PHP &gt;=8.2

### Community

Maintainers

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

---

Top Contributors

[![sanmai](https://avatars.githubusercontent.com/u/139488?v=4)](https://github.com/sanmai "sanmai (41 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")

---

Tags

jsonphpserializer

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M235](/packages/sulu-sulu)[horstoeko/zugferd

A library for creating and reading european electronic invoices

4306.4M37](/packages/horstoeko-zugferd)[dagger/dagger

Dagger PHP SDK

271.4k](/packages/dagger-dagger)[carlosbuenosvinos/ddd

Domain-Driven Design PHP helper classes (Application Services, Transactions, Domain Events, etc.

661195.8k4](/packages/carlosbuenosvinos-ddd)[mhujer/jms-serializer-uuid

Uuid serializer and deserializer for JMS Serializer library

291.3M4](/packages/mhujer-jms-serializer-uuid)[instride/data-definitions

Data Definitions allows you to define your DataObject Imports and Exports using a nice GUI and re-run the definitions as often you like.

8022.6k](/packages/instride-data-definitions)

PHPackages © 2026

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