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

  • Pass JSON_OBJECT_AS_ARRAY to get associative arrays

  • JSON objects are decoded to instances of ArrayObject instead of stdClass when parsed as objects

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

Force decoding objects as associative arrays

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

Force decoding objects as instances of ArrayObject

License

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