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

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

suin/json
=========

A Simple wrapper of json\_decode() and json\_encode(). This provides object-oriented interface and exception-based error handing.

1.2.0(8y ago)1027.6k13MITPHPPHP &gt;=7.1

Since Dec 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/suin/php-json)[ Packagist](https://packagist.org/packages/suin/json)[ RSS](/packages/suin-json/feed)WikiDiscussions master Synced today

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

Json
====

[](#json)

[![travis-ci-badge](https://camo.githubusercontent.com/7d377498946f7964227888c85d1ace393d3732a6d0f43e0538356e7773f71620/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7375696e2f7068702d6a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/suin/php-json) [![packagist-dt-badge](https://camo.githubusercontent.com/d1cbaeb85235962a9920d0dd23b5436e0809a92eafb4258b78a2db1b4502f026/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7375696e2f6a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/suin/json) [![license-badge](https://camo.githubusercontent.com/cc3637c80aa520c1135d71d5a0bcb3b07373398732b4bdaa6ce2c5711cd1a5ef/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7375696e2f7068702d6a736f6e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md) [![release-version-badge](https://camo.githubusercontent.com/811fe9e40bc6065d4c0679cde0d602abee4a86cbb704f6a960d45f8465f79c1d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7375696e2f6a736f6e2e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://packagist.org/packages/suin/json) [![code-climate-maintainability-badge](https://camo.githubusercontent.com/cac2e3dbbdc48fa6bdef1706a327ee4c45648d7dbb3f9f6318e61ff10a67a89d/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f7375696e2f7068702d6a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/suin/php-json) [![code-climate-test-coverage-badge](https://camo.githubusercontent.com/09543cc6f996f8502156dd5732c295b845f529bb34d72080b51e31fa10b79a99/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f632f7375696e2f7068702d6a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/suin/php-json) [![php-version-badge](https://camo.githubusercontent.com/d9d88bcbd88ed6c3eaaf6fe3cab4c499989d8763ee51a5417069706a4b2b6ad1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7375696e2f6a736f6e2e7376673f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/d9d88bcbd88ed6c3eaaf6fe3cab4c499989d8763ee51a5417069706a4b2b6ad1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7375696e2f6a736f6e2e7376673f7374796c653d666c61742d737175617265)

Just a simple wrapper of `json_decode()` and `json_encode()`, but provides better interfaces: exception-based error handling and object oriented APIs.

Features
--------

[](#features)

### Compatible interface

[](#compatible-interface)

This library provides the same interface with built-in functions, so you can replace your code easier.

Built-in function interface:

```
json_decode(
  string $json,
  ?bool $assoc = false,
  ?int $depth = 512,
  ?int $options = 0
): mixed
json_encode(
  mixed $value,
  ?int $options = 0,
  ?int $depth = 512
): string
```

This library interface:

```
\Suin\Json\json_decode(
  string $json,
  ?bool $assoc = false,
  ?int $depth = 512,
  ?int $options = 0
): mixed
\Suin\Json\json_encode(
  mixed $value,
  ?int $options = 0,
  ?int $depth = 512
): string
```

```
\Suin\Json::decode(
  string $json,
  ?bool $assoc = false,
  ?int $depth = 512,
  ?int $options = 0
): mixed
\Suin\Json::encode(
  mixed $value,
  ?int $options = 0,
  ?int $depth = 512
): string
```

So that developers easily migrate to this library from the built-in functions. Just adding the following one line in the head of file:

```
use function Suin\Json\json_decode;
use function Suin\Json\json_encode;
```

For about the full migration example, see [quick migration](./example/00-quick-migration.php).

### Exception-based error handling

[](#exception-based-error-handling)

- Throws `DecodingException` when failed to decode JSON.
- Throws `EncodingException` when failed to encode values.
- You don't have to treat `json_last_error()` any more.

```
// Error handling example
$json = "{'Organization': 'PHP Documentation Team'}";
try {
    Json::decode($json);
} catch (Json\DecodingException $e) {
    var_dump($e->getMessage());
    var_dump($e->getContext()->json());
}
// Output:
// string(35) "Failed to decode JSON: Syntax error"
// string(42) "{'Organization': 'PHP Documentation Team'}"
```

### Object-oriented interface

[](#object-oriented-interface)

As `Decoder` and `Encoder` class can be instantiated, you can re-use a preconfigured single decoder/encoder in several places.

```
// preconfigured decoder
$decoder = (new Decoder)->preferArray();
$array1 = $decoder->decode($json1);
$array2 = $decoder->decode($json2); // re-use it
$array3 = $decoder->decode($json3); // re-use it

// preconfigured encoder
$encoder = (new Encoder)->prettyPrint()->unescapeSlashes()->unescapeUnicode();
$json1 = $encoder->encode($value1);
$json2 = $encoder->encode($value2); // re-use it
$json3 = $encoder->encode($value3); // re-use it
```

### Immutable `Decoder` object

[](#immutable-decoder-object)

As the `Decoder` object setting can not be changed once being instantiated, it is safer even in the case of sharing the object among some modules.

Installation via Composer
-------------------------

[](#installation-via-composer)

```
$ composer require suin/json
```

Example
-------

[](#example)

### Decoding JSON to values using `Json` class

[](#decoding-json-to-values-using-json-class)

```
