php-alchemist/json
最新稳定版本:v1.0.0
Composer 安装命令:
composer require php-alchemist/json
包简介
Collection of services for JSON functionality
README 文档
README
Package: php-alchemist/json
Current Version: v1.x -- (2025/06)
What?
A basic library for working with json and being able to map json objects to a particular class
Usage
The PHP Alchemist JSON library consists of two parts that can be used together or separately.
JsonHydratorTrait
The first is a trait PHPAlchemist\Json\Trait\JsonHydratorTrait which has a single function called
hydrateFromJson() which accepts a string called $json which is expected to be a JSON object. It
then will loop through the object and looks for setter functions or if the property exists for each
key and then sets the data appropriately on the object.
JsonMapper
The second is PHPAlchemist\Json\Service\JsonMapper which has a map() functino which will accept
a JSON string ($json) and a classname ($class). This will validate the JSON and throw a
PHPAlchemist\Json\Exception\BadJsonException if it is invalid. It will then instantiate a new class
of the given type, check to see if the hydrateFromJson() exists. If it does, then it will use that
otherwise it will then go through the same process to set the JSON keys via setter or public access.
Example
class Demo { public string $test; public string $doSomething; } $json = '{"test":"blah","doSomething":"asdf"}'; $mapper = new PHPAlchemist\Json\Service\JsonMapper(); $asdf = $mapper->map($json, Demo::class); var_dump($asdf);
OUTPUT:
object(Demo)#1 (2) { ["test"]=> string(4) "blah" ["doSomething"]=> string(4) "asdf" }
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2025-06-25