PHPackages                             jesseschalken/pure-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jesseschalken/pure-json

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

jesseschalken/pure-json
=======================

json\_encode/json\_decode wrapper with error checking and one-to-one mapping between PHP and JSON values

1.3.0(10y ago)141.5k↓40.9%1LGPL-2.1PHP

Since Aug 8Pushed 10y ago1 watchersCompare

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

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

PureJSON
========

[](#purejson)

`json_encode()`/`json_decode()` wrapper for PHP 5.3+ with one-to-one mapping between JSON and PHP values.

```
use PureJSON\JSON;

$json  = JSON::encode($value);
$value = JSON::decode($json);
```

- `JSON::encode()` will only accept values which can be converted into their exact original by `JSON::decode()`, so that `JSON::decode(JSON::encode($x)) === $x`. The accepted values are:

    - `int`
    - `string`
    - `float` (but not `INF`, `-INF` or `NAN`)
    - `bool`
    - `null`
    - `array` (whose contents are also valid)
- `JSON::encode()`/`JSON::decode()` will assume PHP strings are UTF-8 by default. To encode/decode binary or ISO-8859-1 strings, use `JSON::encode(..., true)` and `JSON::decode(..., true)`.
- To pretty print JSON, use `JSON::encode(..., ..., true)`.
- `JSON::encode()`/`JSON::decode()` will check `json_last_error()` for you and throw a `PureJSON\JSONException` with an appropriate [code](http://php.net/manual/en/function.json-last-error.php) and [message](http://php.net/manual/en/function.json-last-error-msg.php).

Serialization
-------------

[](#serialization)

The methods `JSON::serialize()` and `JSON::deserialize()` differ from `JSON::encode()` and `JSON::decode()` by mapping the JSON `{...}` syntax to and from PHP objects instead of to and from PHP associative arrays. Whereas `JSON::encode()` rejects objects and accepts associative arrays, `JSON::serialize()` rejects associative arrays and accepts objects.

In order for `JSON::deserialize()` to reproduce an instance of the original class given to `JSON::serialize()`:

1. Objects provided to `JSON::serialize()` must implement the `PureJSON\Serializable` interface.
2. The method `jsonProps()` is used for properties and `jsonType()` is used to fill a special `@type` property to identify the type of the object.
3. `JSON::deserialize()` requires an explicit list of classes implementing `PureJSON\Serializable` to possibly instantiate using the method `jsonCreate($props)`.

(By storing a type tag instead of the PHP class name in JSON, the PHP class can be renamed while maintaining compatibility with existing serialized data, and if the JSON given to `JSON::deserialize()` is produced by an attacker, they cannot instantiate classes outside of the explicit list.)

### Exmaple

[](#exmaple)

With `JSON::encode()`/`JSON::decode()`:

```
use PureJson\JSON;

$company = array(
    'name'      => 'Good Company',
    'employees' => array(
    	array(
        	'name' => 'Jesse',
            'role' => 'sales',
        ),
        array(
        	'name' => 'Ben',
            'role' => 'development',
        ),
    ),
);

// encode/decode will reproduce the original array
$json    = JSON::encode($company);
$company = JSON::decode($json);
```

```
{
    "name": "Good Company",
    "employees": [
        {
            "name": "Jesse",
            "role": "sales"
        },
        {
            "name": "Ben",
            "role": "Development"
        }
    ]
}
```

With `JSON::serialize()`/`JSON::deserialize()`:

```
use PureJson\JSON;
use PureJson\Serializable;

class Company implements Serializable {
    public static function jsonCreate(array $props) {
        return new self($props['name'], $props['employees']);
    }

    public static function jsonType() {
    	return 'company';
    }

    private $name;
    private $employees;

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

    public function jsonProps() {
        return array(
            'name'      => $this->name,
            'employees' => $this->employees,
        );
    }
}

class Employee implements Serializable {
    public static function jsonCreate(array $props) {
        return new self($props['name'], $props['role']);
    }

    public static function jsonType() {
        return 'employee';
    }

    private $name;
    private $role;

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

    public function jsonProps() {
        return array(
            'name' => $this->name,
            'role' => $this->role,
        );
    }
}

$company = new Company(
    'Good Company',
    array(
    	new Employee('Jesse', 'sales'),
        new Employee('Ben', 'development'),
    )
);

// serialize/deserialize will produce the original object graph
$json    = JSON::serialize($company);
$company = JSON::deserialize($json, array(
    Company::class,
    Employee::class,
));
```

```
{
    "@type": "company",
    "name": "Good Company",
    "employees": [
        {
            "@type": "employee",
            "name": "Jesse",
            "role": "sales"
        },
        {
            "@type": "employee",
            "name": "Ben",
            "role": "Development"
        }
    ]
}
```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community9

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

Total

5

Last Release

3656d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1559204?v=4)[Jesse Schalken](/maintainers/jesseschalken)[@jesseschalken](https://github.com/jesseschalken)

---

Top Contributors

[![jesseschalken](https://avatars.githubusercontent.com/u/1559204?v=4)](https://github.com/jesseschalken "jesseschalken (30 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[diff-sniffer/diff-sniffer

Diff Sniffer, a tool to validate coding standards only in changed line

141.3k](/packages/diff-sniffer-diff-sniffer)

PHPackages © 2026

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