sanmai/json-serializer
最新稳定版本:0.2.7
Composer 安装命令:
composer require sanmai/json-serializer
包简介
Flexible JSON Serializer
README 文档
README
This library is a thin wrapper around jms/serializer.
composer require sanmai/json-serializer
The purpose of this library is to make simpler deserialization/serialization of objects and, specifically, of arrays of objects, and scalar values. All you need is to follow a simple protocol.
ItemList
JMS Serializer supports deserializing arrays out of the box, but it is ever so slightly complicated since a user must specify a type in a full form, as in array<T>, all the while returned deserialized value will be a plain array. This library abstracts away this extra complexity by providing a two-method protocol instead.
For example, deserialising this object:
use JSONSerializer\Contracts\ItemList; class ItemListExample implements ItemList { /** @var ItemExample[] */ public $items = []; public static function getListType(): string { return ItemExample::class; } public static function withList(array $list) { $itemList = new self(); $itemList->items = $list; return $itemList; } }
From a JSON array:
[
{"name": "foo"},
{"name": "bar"}
]
With an all-familiar method:
use JSONSerializer\Serializer; $serializer = new Serializer(); $result = $serializer->deserialize($json, ItemListExample::class);
Will leave an instance of ItemListExample in $result with $result->items filled with two items as in the source array.
ScalarValue
There's a similar convenience interface called ScalarValue to aid with unserializing wrapped primitive scalar values.
use JSONSerializer\Contracts\ScalarValue; class ScalarValueExample implements ScalarValue { /** @var int */ public $value; public static function withValue($value) { $item = new self(); $item->value = $value; return $item; } public static function getType(): string { return 'int'; } }
统计信息
- 总下载量: 39.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-29