PHPackages                             andreypostal/json-handler-php - 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. andreypostal/json-handler-php

Abandoned → [andreypostal/php-pancake-object](/?search=andreypostal%2Fphp-pancake-object)ArchivedLibrary[Parsing &amp; Serialization](/categories/parsing)

andreypostal/json-handler-php
=============================

Just a light and simple JSON helper that will make it easy for you to deal with json and objects

v1.1.0(1y ago)8312MITPHP

Since Aug 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/andreypostal/json-handler-php)[ Packagist](https://packagist.org/packages/andreypostal/json-handler-php)[ RSS](/packages/andreypostal-json-handler-php/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (0)

Json Handler
============

[](#json-handler)

[![Coverage Status](https://camo.githubusercontent.com/34afb34e1ea684c40389ef7c4f80784fc18c978a3d854fa169eedb03147c36ea/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f616e64726579706f7374616c2f6a736f6e2d68616e646c65722d7068702f62616467652e737667)](https://coveralls.io/github/andreypostal/json-handler-php) [![Maintainability](https://camo.githubusercontent.com/ccad1360436d69a7fd53daac201c91b0b8ac5be54cc364cdb693f806827f19b4/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36336533356666303232306630326430323462392f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/andreypostal/json-handler-php/maintainability)

Just a light and simple JSON helper that will make it easy for you to deal with json and objects.

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

[](#installation)

```
composer require andreypostal/json-handler-php

```

Usage
-----

[](#usage)

### Classes

[](#classes)

When creating your **Value Objects** that represent a **JSON entity** you just need to add the `JsonItemAttribute` to each property that will be present in the JSON.

```
use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123, "name": "my name" }
class MyObject {
    #[JsonItemAttribute]
    public int $id;
    #[JsonItemAttribute]
    public name $name;
}
```

In the case of the entire object being a JsonObject with a direct 1:1 match (or perfect mirror of the keys), you can use the `JsonObjectAttribute`

```
use \Andrey\JsonHandler\Attributes\JsonObjectAttribute;

// { "id": 123, "name": "my name" }
#[JsonObjectAttribute]
class MyObject {
    public int $id;
    public string $name;
}
```

You can also combine both when need to add custom key or if you want to make an item required.

```
use \Andrey\JsonHandler\Attributes\JsonObjectAttribute;
use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123, "custom_name": "my name" }
#[JsonObjectAttribute]
class MyObject {
    public int $id;
    #[JsonItemAttribute(key: 'custom_name')]
    public string $name;
}
```

If your **Value Object** has some property that **won't be present** in the JSON, you can just omit the attribute for it and the other ones will be processed normally.

```
use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123 }
class MyObject {
    #[JsonItemAttribute]
    public int $id;
    public int $myAppGeneratesIt;
}
```

In case the items are required to exist in the JSON being processed, you must add the required flag in the attribute.

```
use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123 } or { "id": 123, "name": "my name" }
class MyObject {
    #[JsonItemAttribute(required: true)]
    public int $id;
    #[JsonItemAttribute]
    public string $name;
}
```

When some of the keys in your JSON are different from your object, you can include the JSON key in the attribute.

```
use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "customer_name": "the customer name" }
class MyObject {
    #[JsonItemAttribute(key: 'customer_name')]
    public string $name;
}
```

Also, if you have a property that is an array of other object, you must inform the class in the attribute using the `type` option. This will work as a hint so the hydrator can instantiate the appropriate object. This works with enums as well.

```
use \Andrey\JsonHandler\JsonItemAttribute;
use \MyNamespace\MyOtherObj;

// { "list": [ { "key": "value" } ] }
class MyObject {
    /** @var MyOtherObj[] */
    #[JsonItemAttribute(type: MyOtherObj::class)]
    public array $list;
}
```

The type option can be used to validate that all the items in an array have some desired type as well, like "string", "integer"...

### Handler

[](#handler)

In order to utilize the definitions mentioned above, you must utilize the `JsonHandler`. Two traits are available as well, the `JsonHydratorTrait` and `JsonSerializerTrait` that provide the methods both for serialization and hydration.

```
use \Andrey\JsonHandler\JsonHandler;
use \MyNamespace\MyObject;

$handler = new JsonHandler();

$myObject = new MyObject();

// This parses the json string and hydrates the original object, modifying it
$handler->hydrateObject($jsonString, $myObject);

// If you don't want to modify the original object you can use the immutable hydration
$hydratedObject = $handler->hydrateObjectImmutable($jsonString, $myObject);

// You can also use an array to hydrate the object
$handler->hydrateObject($jsonArr, $myObject);

// And to fetch the information as an array you can just serialize it using the handler.
// This allows you to easily implement the JsonSerializable interface in your object.
$arr = $handler->serialize($myObject);

// The json handler also provides the methods to decode and encode
$jsonString = JsonHandler::Encode($arr);
$jsonArr = JsonHandler::Decode($jsonString);
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

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

Total

5

Last Release

515d ago

Major Versions

v1.1.0 → v2.0.0-beta2025-02-04

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15055086?v=4)[Andrey Postal](/maintainers/andreypostal)[@andreypostal](https://github.com/andreypostal)

---

Top Contributors

[![andreypostal](https://avatars.githubusercontent.com/u/15055086?v=4)](https://github.com/andreypostal "andreypostal (24 commits)")

---

Tags

deserializationhydratorjsonjson-parserparserphpserializationserializervalue-objectsjsonserializervalue objects

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andreypostal-json-handler-php/health.svg)

```
[![Health](https://phpackages.com/badges/andreypostal-json-handler-php/health.svg)](https://phpackages.com/packages/andreypostal-json-handler-php)
```

###  Alternatives

[zumba/json-serializer

Serialize PHP variables, including objects, in JSON format. Support to unserialize it too.

130784.7k16](/packages/zumba-json-serializer)[laktak/hjson

JSON for Humans. A configuration file format with relaxed syntax, fewer mistakes and more comments.

86245.3k14](/packages/laktak-hjson)[thunderer/serializard

Flexible serializer

2667.9k1](/packages/thunderer-serializard)[opensoft/simple-serializer

Simple Serializer

1914.2k1](/packages/opensoft-simple-serializer)

PHPackages © 2026

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