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

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

dc/json
=======

JSON Serializer

084PHPCI failing

Since Dec 9Pushed 9y ago2 watchersCompare

[ Source](https://github.com/digitalcreations/json)[ Packagist](https://packagist.org/packages/dc/json)[ RSS](/packages/dc-json/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![DC\JSON - Typed JSON](logo.png)](logo.png)

Allow serializing directly to classes using only phpDoc and type hints.

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

[](#installation)

```
$ composer install dc/json

```

Or add it to `composer.json`:

```
"require": {
	"dc/json": "0.*"
}
```

```
$ composer install

```

**This package suggests `dc/ioc` and `dc/cache`, but it really is a very strong recommendation. It will be painful or slow to use without it.**

[![Build status](https://camo.githubusercontent.com/1d01f6f7c86ef4027fcfe33045a49dafa759f80cd0009e26fc4196ca69dc0ba0/687474703a2f2f7465616d636974792e6469676974616c6372656174696f6e732e6e6f2f6170702f726573742f6275696c64732f6275696c64547970653a2869643a44634a736f6e5f4275696c64292f73746174757349636f6e)](http://teamcity.digitalcreations.no/viewType.html?buildTypeId=DcJson_Build&guest=1 "Build status")

Getting started
===============

[](#getting-started)

Get hold of a new `\DC\JSON\Serializer` and start serializing:

```
class Cat {
    /** @var string */
    public $name;
}
$json = '[{ "name": "Sniffles" }, { "name": "Snuggles" }]';
$serializer = new \DC\JSON\Serializer();
$cats = $serializer->deserialize($catsJson, '\Cat[]');
```

If you are using a `dc/ioc` container, you'll need to do this:

```
\DC\JSON\IoC\SerializerSetup::setup($container);
```

Serialization handlers
======================

[](#serialization-handlers)

You can register a serialization handler to change how a specific class is serialized. This is different from using the `JsonSerializable` interface in PHP, as a serialization handler allows you to change how built-in classes are serialized and deserialized.

The interface itself is easy to implement. The library comes with one default handler, which serializes `\DateTime`objects to and from ISO-8601 format. Look at that handler for a good example.

If you want to register serialization your own handlers, it is easiest to do using IoC:

```
$container->register('\MyHandler')->to('\DC\JSON\Handler')->withContainerLifetime();
```

How classes are constructed
===========================

[](#how-classes-are-constructed)

Classes are constructed in this manner:

1. Look for the property names in the constructor. Use the constructor to fill in those values.
2. Look for public setters of the form `setX` for the remaining values (this is controlled by convention, which you can override). Use them.
3. Look for public properties for the remaining values.

So, given this class:

```
class Cat {
    private $name;
    private $age;
    public $paws = 4;

    function __construct($name) {
        $this->name = $name;
    }

    function setAge($age) {
        $this->age = $age;
    }
}
```

...and this JSON:

```
{
    "name": "Snuggles",
    "age": 6,
    "paws": 4
}
```

...These two are equivalent:

```
$cat = $serializer->deserialize($json, '\Cat');
// or
$cat = new \Cat("Snuggles");
$cat->setAge(6);
$cat->paws = 4;
```

Performance
===========

[](#performance)

It is no secret that `json_decode` and `json_encode` is a lot faster than this package. In fact, we use those internally for the actual serialization. The magic of this package gets applied before serialization and after deserialization.

Using a combination of type hints and documentation, we are smart about constructing your objects the way they were intended to be constructed. But, parsing your documentation is resource intensive, therefore it is good practice to heed a few rules:

1. Try to keep the serializer instance around for multiple serializations. All of the reflection happens only the first time a class is encountered.
2. Install `dc/cache` and `dc/cache-memcache`, and provide it to the serializer. If you do, all the information obtained from the reflection is cached for a while.

When using a primed cache, deserializing is about 4-5 times as slow a `json_decode`.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/592f1016d288aeb80fddba8b7749f5bf91f83305b332e8d8f37db3d19c16b7d3?d=identicon)[vegardlarsen](/maintainers/vegardlarsen)

![](https://www.gravatar.com/avatar/53f38207340517e705d87fb858772629b30fa6f1d40a65668dc5599c099d3413?d=identicon)[francisrath](/maintainers/francisrath)

---

Top Contributors

[![vegardlarsen](https://avatars.githubusercontent.com/u/436508?v=4)](https://github.com/vegardlarsen "vegardlarsen (10 commits)")

### Embed Badge

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

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

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

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