germania-kg / jsondecoder
Decode JSON in strings and PSR-7 messages. Supports JsonException on older PHP
1.2.0
2023-01-04 08:10 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- psr/http-message: ^1.0
- symfony/polyfill-php73: ^1.11
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.12
- php-coveralls/php-coveralls: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^8.0|^9.0
This package is auto-updated.
Last update: 2024-12-20 00:11:48 UTC
README
JsonDecoder
Decode JSON in strings and PSR-7 messages. Supports JsonException on older PHP.
Installation with Composer
$ composer require germania-kg/jsondecoder
Usage
The callable JsonDecoder accepts the same parameters than described in PHP's documentation on json_decode. Additionally, it accepts some widely-used kinds of data:
Psr\Http\Message\MessageInterface
Psr\Http\Message\ResponseInterface
Psr\Http\Message\StreamInterface
string
use Germania\JsonDecoder\JsonDecoder; // Let: // $response instance of ResponseInterface, // $body instance of StreamInterface $body = $response->getBody(); $str = $body->__toString(); try { $decoder = new JsonDecoder; $decoded = $decoder( $response ); $decoded = $decoder( $body ); $decoded = $decoder( $str ); } catch ( \JsonException $e) { echo $e->getMessage(); }
Exceptions
When the decoding fails, a \JsonException (mind the global namespace!) will be thrown. This class is provided by Symfony's Polyfill PHP 7.3 for those lacking PHP 7.3.
Issues
Development
$ git clone https://github.com/GermaniaKG/JsonDecoder.git
$ cd JsonDecoder
$ composer install
Unit tests
Either copy phpunit.xml.dist
to phpunit.xml
and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test # or $ vendor/bin/phpunit