pnz / json-exception
Json decode/encode library with exception throwing on error.
Installs: 190 070
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 1
Requires
- php: ^7.0 || ^8.0
- ext-json: *
This package is auto-updated.
Last update: 2024-11-18 21:00:48 UTC
README
Usage
This library provides Json::decode()
and Json::encode()
implementations throwing \JsonException
on errors.
To use this library:
- install the library by using composer:
composer require pnz/json-exception
- replace the usage of
json_decode()
withJson::decode()
- replace the usage of
json_encode()
withJson::encode()
- catch the
\JsonException
eventually thrown by the functions
Example: Old code
$data = json_decode($jsonString, ...); if (ERROR_NONE !== json_last_error()) { // handle the error: thown a custom exception, or return // $error = json_last_error_msg(); // $errorCode = json_last_error(); }
new code:
try { $data = Json::decode($jsonString, ...); } catch(\JsonException $e) { // Handle the exception }
Development
To run the PHP coding-styles checks (php-cs-fixer
and phpstan
) run the make phpcs
command to:
- download the
php-cs-fixer
tool intools/
(if not present) - download the
phpstan
tool intools/
(if not present) - Run
php-cs-fixer
on the source code - Run
phpstan
on the source code
To run the tests, use make tests
to
- download
phpunit
tool intools/
if not present - run
phpunit
tests