vishalshakya/json-handler
最新稳定版本:v1.0.0
Composer 安装命令:
composer require vishalshakya/json-handler
包简介
A package for simplified JSON handling in PHP
README 文档
README
Installation
Install the package using Composer:
composer require vishalshakya/json-handler
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:
use Vishalshakya\JsonHandler;
Encoding Data
The encode method allows you to convert an array or object into a JSON string:
$data = ['name' => 'John Doe', 'age' => 25]; $jsonString = JsonHandler::encode($data); echo $jsonString;
Output:
{"name":"John Doe","age":25}
Decoding JSON Strings
The decode method converts a JSON string into a PHP data structure. By default, it returns an object:
$jsonString = '{"name":"John Doe","age":25}'; $object = JsonHandler::decode($jsonString); echo $object->name; // John Doe echo $object->age; // 25
If you want to decode the JSON string into an associative array, pass true as the second argument:
$jsonString = '{"name":"John Doe","age":25}'; $array = JsonHandler::decode($jsonString, true); echo $array['name']; // John Doe echo $array['age']; // 25
Exception Handling
The JsonHandler class throws exceptions to indicate errors in encoding or decoding JSON data. Here are the possible exceptions:
InvalidArgumentExceptionis thrown when the input data type is invalid or the JSON string is not valid.JsonExceptionis 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:
composer require --dev phpunit/phpunit
Then, run the tests with the following command:
vendor/bin/phpunit
License
This package is open source and released under the MIT License. Feel free to modify and distribute it as needed.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-25