andreypostal/json-handler-php

这是一个轻量级且简单的JSON助手,可以帮助您轻松处理json和对象。

v1.1.0 2024-08-20 04:05 UTC

This package is auto-updated.

Last update: 2024-09-30 03:29:22 UTC


README

Coverage Status Maintainability

这是一个轻量级且简单的JSON助手,可以帮助您轻松处理json和对象。

安装

composer require andreypostal/json-handler-php

使用方法

当创建代表JSON实体的 值对象 时,只需将 JsonItemAttribute 添加到将存在于JSON中的每个属性。

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123, "name": "my name" }
class MyObject {
    #[JsonItemAttribute]
    public int $id;
    #[JsonItemAttribute]
    public name $name;
}

如果整个对象是一个直接1:1匹配(或键的完美镜像)的JsonObject,可以使用 JsonObjectAttribute

use \Andrey\JsonHandler\Attributes\JsonObjectAttribute;

// { "id": 123, "name": "my name" }
#[JsonObjectAttribute]
class MyObject {
    public int $id;
    public string $name;
}

当需要添加自定义键或希望使项目成为必填项时,也可以同时使用两者。

use \Andrey\JsonHandler\Attributes\JsonObjectAttribute;
use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123, "custom_name": "my name" }
#[JsonObjectAttribute]
class MyObject {
    public int $id;
    #[JsonItemAttribute(key: 'custom_name')]
    public string $name;
}

如果您的 值对象 有一些属性在JSON中不存在,可以省略该属性的属性,其他属性将正常处理。

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123 }
class MyObject {
    #[JsonItemAttribute]
    public int $id;
    public int $myAppGeneratesIt;
}

如果处理中的JSON项目必须存在,必须在属性中添加所需标志。

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "id": 123 } or { "id": 123, "name": "my name" }
class MyObject {
    #[JsonItemAttribute(required: true)]
    public int $id;
    #[JsonItemAttribute]
    public string $name;
}

当您的JSON中的一些键与对象不同时,可以在属性中包含JSON键。

use \Andrey\JsonHandler\Attributes\JsonItemAttribute;

// { "customer_name": "the customer name" }
class MyObject {
    #[JsonItemAttribute(key: 'customer_name')]
    public string $name;
}

此外,如果您有一个属性是其他对象的数组,您必须使用 type 选项在属性中通知该类。这将作为一个提示,以便加湿器可以实例化适当的对象。这也适用于枚举。

use \Andrey\JsonHandler\JsonItemAttribute;
use \MyNamespace\MyOtherObj;

// { "list": [ { "key": "value" } ] }
class MyObject {
    /** @var MyOtherObj[] */
    #[JsonItemAttribute(type: MyOtherObj::class)]
    public array $list;
}

type选项可用于验证数组中的所有项目都具有某些期望的类型,如“字符串”、“整数”...

处理器

为了利用上述定义,您必须使用 JsonHandler。此外,还有两个特质可供使用,分别是 JsonHydratorTraitJsonSerializerTrait,它们提供了序列化和加湿的方法。

use \Andrey\JsonHandler\JsonHandler;
use \MyNamespace\MyObject;

$handler = new JsonHandler();

$myObject = new MyObject();

// This parses the json string and hydrates the original object, modifying it
$handler->hydrateObject($jsonString, $myObject);

// If you don't want to modify the original object you can use the immutable hydration
$hydratedObject = $handler->hydrateObjectImmutable($jsonString, $myObject);

// You can also use an array to hydrate the object
$handler->hydrateObject($jsonArr, $myObject);

// And to fetch the information as an array you can just serialize it using the handler.
// This allows you to easily implement the JsonSerializable interface in your object.
$arr = $handler->serialize($myObject);

// The json handler also provides the methods to decode and encode
$jsonString = JsonHandler::Encode($arr);
$jsonArr = JsonHandler::Decode($jsonString);