PHPackages                             mobijay/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. mobijay/json-serializer

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

mobijay/json-serializer
=======================

Serialize PHP object using attribute definitions

0.2.5(2y ago)0247GPL-3.0-or-laterPHPPHP ^8.0

Since Mar 5Pushed 2y ago1 watchersCompare

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

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

The class you wants to serialize must `implements \JsonSerializable`

Then you have to `use JsonSerializeByAttribute` Trait. It contains the serialization method;

for each field or method you want to serialize add the following attribute `#\[JsonSerialize\] the field name is used as key in the serialized json.

If you want to change the key in the json you have to add it #\[JsonSerialize('customPrivateField')\]

If null value shouldn't serialized you have to specify it #\[JsonSerialize('customPrivateField',false)\] by default it is true

A complete example:

`

```
{
use JsonSerializeByAttribute;

    #[JsonSerialize('nullField1',false)]
    private ?string $nullField1;
    #[JsonSerialize('nullField2',true)]
    private ?string $nullField2 = null;

    #[JsonSerialize('customPrivateField')]
    private $privateField;

    public $disturbanceField1;
    private $disturbanceField2;

    #[JsonSerialize]
    public $publicField;

    #[JsonSerialize] public Carbon $carbonField;
    #[JsonSerialize] public array $unSetArrayField;
    #[JsonSerialize] public array $arrayField;
    #[JsonSerialize] public array $associativeArrayField;

    public JsonTestOneNested $jsonTestOneNested1;

    #[JsonSerialize]
    public JsonTestOneNested $jsonTestOneNested2;

    #[JsonSerialize("objectWithoutSerializable")]
    public JsonTestOneNestedWithoutJsonSerializable $jsonTestOneNestedWithoutJsonSerializable1;

    public function __construct()
    {
        $this->privateField = 'privateFieldValue';
        $this->publicField = 'publicFieldValue';
        $this->carbonField = Carbon::createFromFormat('Y-m-d H:i:s', '2023-10-23 11:33:32');
        $this->associativeArrayField = ["testKey1" => "testValue1", "testKey2" => "testValue2",];
        $this->arrayField = ["test1", "test2"];
        $this->jsonTestOneNested2 = new JsonTestOneNested();
        $this->jsonTestOneNestedWithoutJsonSerializable1 = new JsonTestOneNestedWithoutJsonSerializable();

    }

    #[\Mobijay\JsonSerializer\Attributes\JsonSerialize('customPublicMethod')]
    public function publicMethod()
    {
        return 'publicMethodValue';
    }

    #[\Mobijay\JsonSerializer\Attributes\JsonSerialize]
    public function privateMethod()
    {
        return 'privateMethodValue';
    }

    public function disturbanceMethod()
    {
        return 'disturbanceMethodValue';
    }

}

class JsonTestOneNested implements \JsonSerializable
{
use JsonSerializeByAttribute;

    #[JsonSerialize('nestedCustomPrivateField')]
    private $privateField = "nestedPrivateFieldValue";

}

class JsonTestOneNestedWithoutJsonSerializable
{
private $privateField = "nestedPrivateFieldValueWithout";
public $publicFieldWithoutSerializable = "publicFieldWithoutSerializableValue";
}```

```

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity40

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

Total

4

Last Release

817d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/00f60b7994e840f0e06b78e6ae56175802ddb49608993e75158c85da26d74166?d=identicon)[mobijay](/maintainers/mobijay)

---

Top Contributors

[![GiuseppeMobijay](https://avatars.githubusercontent.com/u/8307986?v=4)](https://github.com/GiuseppeMobijay "GiuseppeMobijay (23 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[illuminate/support

The Illuminate Support package.

582110.9M39.8k](/packages/illuminate-support)[illuminate/config

The Illuminate Config package.

10943.9M2.4k](/packages/illuminate-config)[illuminate/session

The Illuminate Session package.

9938.5M821](/packages/illuminate-session)[illuminate/pagination

The Illuminate Pagination package.

10533.5M989](/packages/illuminate-pagination)[illuminate/broadcasting

The Illuminate Broadcasting package.

7126.9M203](/packages/illuminate-broadcasting)[illuminate/redis

The Illuminate Redis package.

8314.4M362](/packages/illuminate-redis)

PHPackages © 2026

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