PHPackages                             matthiasnoback/naive-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. matthiasnoback/naive-serializer

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

matthiasnoback/naive-serializer
===============================

A naive JSON serializer which recursively converts an object graph to and from JSON, without any configuration or custom code.

v0.11.1(5y ago)509.8k↑18.8%41MITPHPPHP ^7.4|^8.0CI failing

Since Feb 4Pushed 5y ago2 watchersCompare

[ Source](https://github.com/matthiasnoback/naive-serializer)[ Packagist](https://packagist.org/packages/matthiasnoback/naive-serializer)[ RSS](/packages/matthiasnoback-naive-serializer/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (6)Versions (21)Used By (1)

Naive serializer
================

[](#naive-serializer)

The [JsonSerializer](src/JsonSerializer.php) that comes with this library is a very simple serializer/deserializer which recursively converts an object graph to and from JSON, without any configuration or custom code. Its design goals are:

- Users shouldn't be forced to add custom configuration to their existing classes.
- Users shouldn't need to write any supporting code.
- The solution should take care of as few edge cases as possible.
- The solution should be as small as possible, without becoming useless (&lt;=100 LOC).
- The solution should warn the user about its limitations using descriptive exceptions.

In order to make this work, this library restricts you to using only values of type:

- `null`
- scalar (int, float, bool)
- user-defined objects (so no built-in PHP classes like `\DateTimeImmutable`)
- arrays where every value is of the same type (maps or lists)
- and any combination of the above

Furthermore, you need to define the types you used in standard `@var` docblock annotations (as you probably already do), e.g.

```
/**
 * @var string
 *
 * @var int
 *
 * @var bool
 *
 * You can use a relative class name:
 *
 * @var ClassName
 *
 * Or a full class name:
 *
 * @var Fully\Qualified\Class\Name
 */
```

Of course, every property should have just one `@var` annotation.

For simple types, you can use PHP native property types as well:

```
private string $string;
private int $int;
private bool $bool;
private ClassName $object;
```

`array`-typed properties still require an additional `@var` annotation. You can define lists of the above types by simply adding `[]` to the `@var` annotation, e.g.

```
/**
 * @var Fully\Qualified\Class\Name[]
 */
private array $array;
```

To work around the limitation that you can't use PHP's built-in classes, simply convert the data internally to something else. For example, to use a `\DateTimeImmutable` timestamp:

```
/**
 * @var string
 */
private $timestamp;

public function __construct(\DateTimeImmutable $timestamp)
{
    $this->timestamp = $timestamp->format(\DateTime::ISO8601);
}

public function getTimestamp() : \DateTimeImmutable
{
    return \DateTimeImmutable::createFromFormat(\DateTime::ISO8601, $this->timestamp);
}
```

To use the serializer:

```
// create an object
$object  = ...;

$serializedData = Serializer::serialize($object);

// $serializedData will be a pretty-printed JSON string
```

To deserialize the data:

```
$restoredObject = Serializer::deserialize(
    Fully\Qualified\Class\Name::class,
    $serializedData
);

// $restoredObject will be of type Fully\Qualified\Class\Name
```

You can force the (de)serializer to ignore certain properties by adding `@ignore` somewhere in its doc block:

```
final class IgnoredProperty
{
    /**
     * @var array
     * @ignore
     */
    public $events = [];

    /**
     * @var string
     */
    public $foo;
}
```

If you like, you can create an instance of `JsonSerializer` and call its object methods instead of `Serializer`'s static methods.

Thanks
======

[](#thanks)

This library stands on the shoulders of the `phpdocumentor/reflection-docblock` library, which does all the work related to property type resolving.

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

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

Recently: every ~37 days

Total

15

Last Release

1981d ago

PHP version history (5 changes)v0.1.1PHP ^7.0

v0.7.0PHP ^7.1

v0.9.1PHP ^7.3

v0.10.0PHP ^7.4

v0.11.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1193078?v=4)[Matthias Noback](/maintainers/matthiasnoback)[@matthiasnoback](https://github.com/matthiasnoback)

---

Top Contributors

[![matthiasnoback](https://avatars.githubusercontent.com/u/1193078?v=4)](https://github.com/matthiasnoback "matthiasnoback (33 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.5k1.5M88](/packages/mcp-sdk)[phpdocumentor/reflection

Reflection library to do Static Analysis for PHP Projects

12525.9M148](/packages/phpdocumentor-reflection)[oat-sa/tao-core

TAO core extension

66143.7k122](/packages/oat-sa-tao-core)[symfony/ai-platform

PHP library for interacting with AI platform provider.

521.4M289](/packages/symfony-ai-platform)[cognesy/instructor-php

The complete AI toolkit for PHP: unified LLM API, structured outputs, agents, and coding agent control

318123.0k1](/packages/cognesy-instructor-php)

PHPackages © 2026

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