denisyfrolov/json-serializer

用于将任何对象序列化为JSON的简单PHP序列化器。

1.0.0 2018-08-10 23:27 UTC

This package is not auto-updated.

Last update: 2024-09-26 01:00:10 UTC


README

Build Status Build Status Code Coverage Scrutinizer Code Quality Code Intelligence Status Latest Stable Version Total Downloads License

安装

使用 Composer 安装此包

$ composer require denisyfrolov/json-serializer

简介

什么是序列化?

在数据存储的上下文中,序列化是将数据结构或对象状态转换为可以存储(例如,在文件或内存缓冲区中,或通过网络连接传输)并在以后相同或另一个计算机环境中重新构建的格式的过程。

功能

这是一个非常简单的类,允许您将任何子对象序列化为JSON。

  • 递归序列化您允许序列化的所有属性,包括其他类的对象的二维数组。
  • 使用注解自定义序列化对象的方案,并标记哪些类和属性应进行序列化。
  • 允许您自定义JSON的字段名称。
  • 允许您从对象序列化中排除任何属性。
  • 允许您防止序列化任何属性,这些属性所属的类不应序列化。

序列化

使您的类继承自序列化器类 JsonSerializableObject

use JsonSerializer\JsonSerializableObject;

class Order extends JsonSerializableObject
{

}

使用类注解区域中的标志 @JsonSerializable 标记您的类为可序列化

use JsonSerializer\JsonSerializableObject;

/**
 * @JsonSerializable
 */
class Order extends JsonSerializableObject
{

}

如果您的类使用不同的类作为您要序列化的属性的类型,则将这些类全部标记为可序列化。否则将抛出异常:Class ClassName is not marked as serializable

为所有要序列化的属性创建getter。此外,使属性公开访问

use JsonSerializer\JsonSerializableObject;

/**
 * @JsonSerializable
 */
class Order extends JsonSerializableObject
{
    /**
     * @var integer
     */
    private $orderId = 0;

    /**
     * Get OrderId
     *
     * @return int
     */
    public function getOrderId(): int
    {
        return $this->orderId;
    }

    /**
     * Set OrderId
     *
     * @param integer $orderId
     *
     * @return Order
     */
    public function setOrderId(int $orderId): Order
    {
        $this->orderId = $orderId;

        return $this;
    }
}

默认情况下,序列化器使用属性名称作为JSON模式中的字段名称。如果您想自定义模式中的名称,请在属性注解区域中使用 @JsonPropertyName property_name 关键字

use JsonSerializer\JsonSerializableObject;

/**
 * @JsonSerializable
 */
class Order extends JsonSerializableObject
{
    /**
     * @JsonPropertyName order_id
     * 
     * @var integer
     */
    private $orderId = 0;

    /**
     * Get OrderId
     *
     * @return int
     */
    public function getOrderId(): int
    {
        return $this->orderId;
    }

    /**
     * Set OrderId
     *
     * @param integer $orderId
     *
     * @return Order
     */
    public function setOrderId(int $orderId): Order
    {
        $this->orderId = $orderId;

        return $this;
    }
}

要防止任何属性序列化,请在属性注解区域中使用关键字 @JsonPropertyNonSerializable 将属性标记为 不可序列化

use JsonSerializer\JsonSerializableObject;

/**
 * @JsonSerializable
 */
class Order extends JsonSerializableObject
{
    /**
     * @JsonPropertyName order_id
     * 
     * @var integer
     */
    private $orderId = 0;

    /**
     * @JsonPropertyNonSerializable
     * 
     * @var float
     */
    private $amount = 0;

    /**
     * Get OrderId
     *
     * @return int
     */
    public function getOrderId(): int
    {
        return $this->orderId;
    }

    /**
     * Set OrderId
     *
     * @param integer $orderId
     *
     * @return Order
     */
    public function setOrderId(int $orderId): Order
    {
        $this->orderId = $orderId;

        return $this;
    }

    /**
     * Get Amount
     *
     * @return float
     */
    public function getAmount(): float
    {
        return $this->amount;
    }

    /**
     * Set Amount
     *
     * @param float $amount
     *
     * @return Order
     */
    public function setAmount(float $amount): Order
    {
        $this->amount = $amount;

        return $this;
    }
}

调用 jsonSerialize() 方法将您的对象序列化为JSON格式。

require_once '../vendor/autoload.php';

$order = new Order();
$order->setOrderId(12345);
$order->setAmount(100);

print $order->jsonSerialize();

输出

{
    "order_id": 12345,
    "amount": 100
}

示例

您可以在项目的 example 文件夹中找到包含所有类的此示例。

代码

use JsonSerializer\JsonSerializableObject;

/**
 * Order
 * 
 * @JsonSerializable
 * 
 */
class Order extends JsonSerializableObject
{
    /**
     * @JsonPropertyName order_id
     * 
     * @var integer
     */
    private $orderId = 0;

    /**
     * @JsonPropertyName customer
     * 
     * @var Customer
     */
    private $customer;

    /**
     * @JsonPropertyName products
     * 
     * @var array
     */
    private $products = array();

    /**
     * Get OrderId
     *
     * @return int
     */
    public function getOrderId(): int
    {
        return $this->orderId;
    }

    /**
     * Set OrderId
     *
     * @param integer $orderId
     *
     * @return Order
     */
    public function setOrderId(int $orderId): Order
    {
        $this->orderId = $orderId;

        return $this;
    }

    /**
     * Get customer
     *
     * @return Customer
     */
    public function getCustomer(): Customer
    {
        return $this->customer !== null ? $this->customer : new Customer();
    }

    /**
     * Set customer
     *
     * @param Customer $customer
     *
     * @return Order
     */
    public function setCustomer(Customer $customer): Order
    {
        $this->customer = $customer;

        return $this;
    }

    /**
     * Get Products
     *
     * @return array
     */
    public function getProducts(): array
    {
        return ($this->products !== null and is_array($this->products) and count($this->products) > 0) ? $this->products : array(new Product());
    }

    /**
     * Add Product
     *
     * @param Product $product
     *
     * @return Order
     */
    public function addProduct(Product $product): Order
    {
        $this->products[] = $product;

        return $this;
    }
    
}
require_once '../vendor/autoload.php';
require_once 'Order.php';
require_once 'Product.php';
require_once 'Customer.php';

$customer = new Customer();
$customer->setName('Test Customer Name');
$customer->setEmail('example@email.com');

$product = new Product();
$product->setName('Test Product 1 Name');
$product->setQuantity(2);
$product->setPrice(10.52);

$product2 = new Product();
$product2->setName('Test Product 2 Name');
$product2->setQuantity(4);
$product2->setPrice(11.53);

$order = new Order();
$order->setOrderId(12345);
$order->setCustomer($customer);
$order->addProduct($product);
$order->addProduct($product2);

print $order->jsonSerialize();

输出

{
    "order_id": 12345,
    "customer": {
        "name": "Test Customer Name",
        "email": "example@email.com"
    },
    "products": [
        {
            "name": "Test Product 1 Name",
            "quantity": 2,
            "price": 10.52,
            "amount": 21.04
        },
        {
            "name": "Test Product 2 Name",
            "quantity": 4,
            "price": 11.53,
            "amount": 46.12
        }
    ]
}

质量

要在命令行中运行PHPUnit测试,请转到项目的根目录并执行 phpunit

此库试图遵守 PSR-4

如果您注意到合规性疏忽,请通过拉取请求发送补丁。

贡献

对包的贡献总是受欢迎的!

作者

许可证

代码库受MIT许可证许可。