PHP class for working with JSON objects

v1.0.0 2023-05-04 14:53 UTC

This package is auto-updated.

Last update: 2024-05-04 17:37:11 UTC


README

The package provides methods to encode and decode JSON.

  • It has sensible defaults, so you don't have to specify flags all the time.
  • It has handy method to encode for HTML safely.
  • It throws \IC\Json\Exception\JsonEncodeException when encoding fails.
  • It throws \IC\Json\Exception\JsonDecodeException when decoding fails.
  • It handles \JsonSerializable, \DateTimeInterface, and \SimpleXMLElement well.

Requirements

  • PHP 8.0 or higher.
  • SimpleXML PHP extension.

Installation

The package could be installed with composer:

composer require inerciacreativa/json --prefer-dist

General usage

Encoding:

use \IC\Json\Json;

$data = ['name' => 'Jose', 'team' => 'Inercia Creativa'];
$json = Json::encode($data);

Encoding for HTML:

use \IC\Json\Json;

$data = ['name' => 'Jose', 'team' => 'Inercia Creativa'];
$json = Json::htmlEncode($data);

Decoding:

use \IC\Json\Json;

$json = '{"name":"Jose","team":"Inercia Creativa"}';
$data = Json::decode($json);

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

composer phpunit

Static analysis

The code is statically analyzed with PHPStan. To run static analysis:

composer phpstan

License

The IC JSON is free software. It is released under the terms of the MIT License. Please see LICENSE for more information.

Maintained by Inercia Creativa.