JSON

Packagist GitLab GitHub Bitbucket Gitea

A wrapper for the standard ext-json with sane defaults.

Installation

composer require 'arokettu/json'

Encoding

<?php
function \Arokettu\Json\Json::encode(mixed $value, int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, int $depth = 512): string;

Main features:

  • JSON_THROW_ON_ERROR is enforced

  • Two convenience constants:

    • \Arokettu\Json\Json::ENCODE_DEFAULT = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE

    • \Arokettu\Json\Json::ENCODE_PRETTY = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT

Decoding

<?php
function \Arokettu\Json\Json::decode(string $json, int $options = 0, int $depth = 512): mixed;

Main features:

  • JSON_THROW_ON_ERROR is enforced

  • JSON_OBJECT_AS_ARRAY is ignored

  • JSON objects are decoded to instances of ArrayObject instead of stdClass or array

License

The library is available as open source under the terms of the MIT License.