2017 © Pedro Peláez
 

library json

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

image

suin/json

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

  • Thursday, January 4, 2018
  • by suin
  • Repository
  • 1 Watchers
  • 6 Stars
  • 43 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 10 % Grown

The README.md

Json

![travis-ci-badge] ![packagist-dt-badge] ![license-badge] ![release-version-badge] ![code-climate-maintainability-badge] ![code-climate-test-coverage-badge] !php-version-badge, (*1)

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

Features

Compatible interface

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

Built-in function interface:, (*4)

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:, (*5)

\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:, (*6)

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

For about the full migration example, see quick migration., (*7)

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

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

// 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

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., (*9)

Installation via Composer

``` bash $ composer require suin/json, (*10)


## Example ### Decoding JSON to values using `Json` class ```php <?php use Suin\Json; $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(Json::decode($json)); var_dump(Json::decode($json, true)); // Output: // object(stdClass)#%d (5) { // ["a"]=> // int(1) // ["b"]=> // int(2) // ["c"]=> // int(3) // ["d"]=> // int(4) // ["e"]=> // int(5) // } // array(5) { // ["a"]=> // int(1) // ["b"]=> // int(2) // ["c"]=> // int(3) // ["d"]=> // int(4) // ["e"]=> // int(5) // }

Encoding values to JSON using Json class

<?php

use Suin\Json;

$value = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
var_dump(Json::encode($value));
// Output:
// string(31) "{"a":1,"b":2,"c":3,"d":4,"e":5}"

To see more examples, visit ./example folder., (*11)

Changelog

Please see CHANGELOG for more details., (*12)

Contributing

Please see CONTRIBUTING for more details., (*13)

The Versions

04/01 2018

dev-master

9999999-dev

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

json wrapper encoder decoder exceptions json_encode json_decode

04/01 2018

1.2.0

1.2.0.0

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

json wrapper encoder decoder exceptions json_encode json_decode

03/01 2018

1.1.1

1.1.1.0

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

json wrapper encoder decoder exceptions json_encode json_decode

29/12 2017

1.1.0

1.1.0.0

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

json wrapper encoder decoder exceptions json_encode json_decode

29/12 2017

1.0.0

1.0.0.0

A Simple wrapper of json_decode() and json_encode(). This provides object-oriented interface and exception-based error handing.

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

json wrapper encoder decoder exceptions json_encode json_decode