|
| 1 | +# Technically JSON |
| 2 | + |
| 3 | +`Technically\Json` is a minimalistic wrapper around PHP native `json_encode` and `json_decode` functions, |
| 4 | +which always throws an exception in case of an error. |
| 5 | + |
| 6 | +![Tests Status][badge] |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- PHP 7.4+ |
| 11 | +- PHP 8.0 |
| 12 | +- Semver |
| 13 | +- Tests |
| 14 | + |
| 15 | +## Problem |
| 16 | + |
| 17 | +Look, if an error occurs with `json_decode()`, by default, it sets the global error state |
| 18 | +that can be retrieved with `json_last_error()` and `json_last_error_msg()`. |
| 19 | +Alternatively, developers can use `JSON_THROW_ON_ERROR` option to make `json_decode()` throw |
| 20 | +an exception in case of invalid input. |
| 21 | + |
| 22 | +Proper way of using the native `json_decode()` function, though many developers forget about it: |
| 23 | + |
| 24 | +```php |
| 25 | +try { |
| 26 | + $data = json_decode($input, false, 512, JSON_THROW_ON_ERROR); |
| 27 | +} catch (JsonException $exception) { |
| 28 | + // handle invalid input |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +Or this: |
| 33 | + |
| 34 | +```php |
| 35 | +$data = \json_decode($input); |
| 36 | +$error = \json_last_error(); |
| 37 | + |
| 38 | +if ($error !== JSON_ERROR_NONE) { |
| 39 | + throw new \JsonException(\json_last_error_msg(), $error); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +I believe it should be the default behavior of `json_decode()` and `json_encode()` to throw on errors. |
| 44 | +We don't have to remember about it. |
| 45 | + |
| 46 | +## Solution |
| 47 | + |
| 48 | +```php |
| 49 | +use function Technically\Json\json_decode; |
| 50 | + |
| 51 | +try { |
| 52 | + $data = json_decode($input); |
| 53 | +} catch (JsonException $exception) { |
| 54 | + // handle invalid input |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Installation |
| 59 | + |
| 60 | +Use [Composer][getcomposer] package manager to add the library to your project: |
| 61 | + |
| 62 | +``` |
| 63 | +composer require technically/json |
| 64 | +``` |
| 65 | + |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +The namespaced `json_encode()` and `json_decode()` functions do always add `JSON_THROW_ON_ERROR` flag, |
| 70 | +but in the rest they work identically to the native functions: same arguments, same options, same result. |
| 71 | + |
| 72 | +```php |
| 73 | +<?php |
| 74 | + |
| 75 | +use JsonException; |
| 76 | +use function Technically\Json\json_decode; |
| 77 | +use function Technically\Json\json_encode; |
| 78 | + |
| 79 | +// encode |
| 80 | +echo json_encode(['name' => 'technically/json', 'type' => 'library']); |
| 81 | + |
| 82 | +// decode with safety check exception to protected |
| 83 | +try { |
| 84 | + $data = json_decode('[{ invalid JSON'); |
| 85 | +} catch (JsonException $exception) { |
| 86 | + // handle invalid data |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | + |
| 91 | +## Changelog |
| 92 | + |
| 93 | +All notable changes to this project will be documented in the [CHANGELOG](./CHANGELOG.md) file. |
| 94 | + |
| 95 | + |
| 96 | +## Credits |
| 97 | + |
| 98 | +- Implemented by [Ivan Voskoboinyk][author] |
| 99 | + |
| 100 | + |
| 101 | +[getcomposer]: https://getcomposer.org/ |
| 102 | +[author]: https://github.com/e1himself?utm_source=web&utm_medium=github&utm_campaign=technically/array-container |
| 103 | +[badge]: https://github.com/technically-php/array-container/actions/workflows/test.yml/badge.svg |
0 commit comments