https://github.com/hampel/json
A simple wrapper for json_encode and json_decode with exception based error handling
https://github.com/hampel/json
json php
Last synced: 3 months ago
JSON representation
A simple wrapper for json_encode and json_decode with exception based error handling
- Host: GitHub
- URL: https://github.com/hampel/json
- Owner: hampel
- License: mit
- Created: 2023-06-09T07:38:04.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-10T01:52:53.000Z (about 3 years ago)
- Last Synced: 2025-05-25T10:44:16.130Z (about 1 year ago)
- Topics: json, php
- Language: PHP
- Homepage:
- Size: 38.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
JSON Wrapper
============
[](https://packagist.org/packages/hampel/json)
[](https://packagist.org/packages/hampel/json)
[](https://github.com/hampel/json/issues)
[](https://packagist.org/packages/hampel/json)
A simple wrapper for `json_encode` and `json_decode` with exception based error handling
By [Simon Hampel](mailto:simon@hampelgroup.com)
__Note:__ as of PHP v7.3 there is now a `JSON_THROW_ON_ERROR` option for both native commands which should effectively
render this package obsolete.
Installation
------------
To install using composer, run the following command:
`composer require hampel/json`
Note that there are three versions of this package, depending on the version of PHP you use:
* v2.1 supports PHP >= v5.3.3
* v2.2 supports PHP >= v5.4.0
* v2.3 supports PHP >= v5.5.0
The three versions will be maintained in parallel
Usage
-----
All parameters are the same as specified for the PHP functions json_encode and json_decode respectively.
The main difference that this class provides is that it throws exceptions when there are errors and translates the
error codes into meaningful text for you automatically.
```php
1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
// Encode a variable as JSON:
echo Json::encode($data);
// Encode options
$options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;
echo Json::encode($data, $options);
// Decode JSON:
print_r(Json::decode('{"a":1,"b":2,"c":3,"d":4,"e":5}'));
// Error handling
try {
Json::decode('{"a":1,"b":2,"c":3,"d":4,"e":5'); // missing }
} catch (JsonException $e) {
echo "Oops: " . $e->getMessage();
}
```