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

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

phputil/json
============

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

1.3.1(8y ago)56.1k↓50%1MITPHPPHP &gt;=5.3

Since Oct 22Pushed 8y ago2 watchersCompare

[ Source](https://github.com/thiagodp/json)[ Packagist](https://packagist.org/packages/phputil/json)[ Docs](http://github.com/thiagodp/json)[ RSS](/packages/phputil-json/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (9)Used By (1)

JSON
====

[](#json)

A JSON encoder and decoder that can also convert PHP objects with private or protected attributes.

[![Build Status](https://camo.githubusercontent.com/775ab2fc50fde1b2b7f26839098e731fbaf87444f20638b67d98a1e2e0a41d6e/68747470733a2f2f7472617669732d63692e6f72672f74686961676f64702f6a736f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/thiagodp/json)

We use [semantic versioning](http://semver.org/). See [our releases](https://github.com/thiagodp/json/releases).

### Classes

[](#classes)

- [phputil\\JSON](https://github.com/thiagodp/json/blob/master/lib/JSON.php)

### Installation

[](#installation)

```
composer require phputil/json
```

Depends only on [phputil\\RTTI](https://github.com/thiagodp/rtti). Requires PHP &gt;= `5.4`.

### Example 1

[](#example-1)

Converting an object with `private` or `protected` attributes.

```

```

### Example 2

[](#example-2)

Converting a *dynamic object*.

```
$obj = new stdClass();
$obj->name = 'Suzan';
$obj->age = 21;

echo JSON::encode( $obj ); // { "name": "Suzan", "age": 21 }
```

### Example 3

[](#example-3)

Converting an `array` of *dynamic objects* to JSON and back again.

```
$obj1 = new stdClass();
$obj1->name = 'Bob';

$obj2 = new stdClass();
$obj2->name = 'Suzan';
$obj2->age = 21;

$json = JSON::encode( array( $obj1, $obj2 ) );
echo $json; // [ { "name": "Bob" }, { "name": "Suzan", "age": 21 } ]

$array = JSON::decode( $json );
var_dump( $array ); // array with the two PHP dynamic objects
```

### Example 4

[](#example-4)

Converting attributes from classes that use the `__call` magic method.

```
class Foo {
	private $a = 1;
	protected $b = 2;
	public $c = 3;

	function __call( $name, $args ) {
	    if ( 'getA' === $name ) { return $this->a; }
	    if ( 'getB' === $name ) { return $this->b; }
	    if ( 'getC' === $name ) { return $this->c; }
	}
}

echo JSON::encode( new Foo() ); // { "a": 1, "b": 2, "c": 3 }
```

### Example 5

[](#example-5)

Ignoring `NULL` values in objects' attributes or array values.

```
$arr = array( 'name' => 'Bob', 'phone' => null, 'age' => 21 ); // phone is null
// true as the third argument makes encode() to ignore null values
echo JSON::encode( $arr, 'get', true ); // { "name": "Bob", "age": 21 }
```

### Example 6

[](#example-6)

Using value conversors. A value conversor is a function to convert values of a certain type correctly. For example, suppose that you need to convert values of the type `DateTime` to the format `year-month-day`. All you need is to register the type and a function to convert its values, using the static method `addConversion`:

```
JSON::addConversion( 'DateTime', function( $value ) {
	return $value->format( 'Y-m-d' ); // year-month-day
} );

$obj = new stdClass();
$obj->user = 'bob';
$obj->birthdate = new DateTime( "12/31/1980" ); // month/day/year

echo JSON::encode( $obj ); // { "user": "bob", "birthdate": "1980-12-31" }
```

License
-------

[](#license)

[MIT](https://choosealicense.com/licenses/mit/) (c) [Thiago Delgado Pinto](https://github.com/thiagodp)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity63

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

Recently: every ~172 days

Total

8

Last Release

3033d ago

PHP version history (2 changes)1.0PHP &gt;=5.2.0

1.3.1PHP &gt;=5.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/154324d722a6ee9c252a0338329781084a97af2d0ea9faaf39176df5a689a2ec?d=identicon)[thiagodp](/maintainers/thiagodp)

---

Top Contributors

[![thiagodp](https://avatars.githubusercontent.com/u/2997844?v=4)](https://github.com/thiagodp "thiagodp (26 commits)")

---

Tags

classconverterdecoderencoderjsonphpphputilprivate-attributephpjsonobjectencodedecodeprivateprotected

### Embed Badge

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

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

###  Alternatives

[kherge/json

Encodes, decodes, and validates JSON data.

61226.6k6](/packages/kherge-json)

PHPackages © 2026

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