Download the PHP package vishalshakya/json-handler without Composer
On this page you can find all versions of the php package vishalshakya/json-handler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download vishalshakya/json-handler
More information about vishalshakya/json-handler
Files in vishalshakya/json-handler
Package json-handler
Short Description A package for simplified JSON handling in PHP
License MIT
Informations about the package json-handler
JSON Handler
Installation
Install the package using Composer:
Requirements
- PHP 7.4 or higher
- PHPUnit (for running tests, required only for development)
Usage
To use the JsonHandler
class, make sure to import it into your PHP file:
Encoding Data
The encode
method allows you to convert an array or object into a JSON string:
Output:
Decoding JSON Strings
The decode
method converts a JSON string into a PHP data structure. By default, it returns an object:
If you want to decode the JSON string into an associative array, pass true
as the second argument:
Exception Handling
The JsonHandler
class throws exceptions to indicate errors in encoding or decoding JSON data. Here are the possible exceptions:
InvalidArgumentException
is thrown when the input data type is invalid or the JSON string is not valid.JsonException
is thrown when there is an error in the JSON encoding or decoding process.
Make sure to handle these exceptions appropriately in your code.
Development
If you want to run the tests for this package, make sure you have PHPUnit installed. You can install it using Composer:
Then, run the tests with the following command:
License
This package is open source and released under the MIT License. Feel free to modify and distribute it as needed.