good-php/serialization
Composer 安装命令:
composer require good-php/serialization
包简介
Extendable reflection-based serializer with support for JSON and PHP primitive formats
README 文档
README
The concept is similar to Moshi, a Java/Kotlin serialization library - the least effort without sacrificing customizability, support for different formats or ease of use.
This is what it can serialize and deserialize out-of-the-box:
/** * @template T1 */ class Item { /** * @param BackedEnumStub[] $array * @param Collection<int, T1> * @param T1 $generic * @param NestedGeneric<int, T1> $nested */ public function __construct( // Scalars public readonly int $int, public readonly float $float, public readonly string $string, public readonly bool $bool, // Nullable and optional values public readonly ?string $nullableString, public readonly int|null|MissingValue $optional, // Custom property names #[SerializedName('two')] public readonly string $one, // Backed enums public readonly BackedEnumStub $backedEnum, // Generics and nested objects public readonly mixed $generic, public readonly NestedGenerics $nestedGeneric, // Arrays and Illuminate Collection of any type (with generics!) public readonly array $array, public readonly Collection $collection, // Dates public readonly DateTime $dateTime, public readonly Carbon $carbon, ) {} }
You can then convert it into a "primitive" (scalars and arrays of scalars) or JSON:
$primitiveAdapter = $serializer->adapter( PrimitiveTypeAdapter::class, NamedType::wrap(Item::class, [Carbon::class]) ); // -> ['int' => 123, ...] $primitiveAdapter->serialize(new Item(...)); $jsonAdapter = $serializer->adapter( JsonTypeAdapter::class, NamedType::wrap(Item::class, [PrimitiveType::int()]) ); // new Item(123, ...) $jsonAdapter->deserialize('{"int": 123, ...}');
Documentation
Basic documentation is available in docs/. For examples, you can look at the test suite: tests/Integration.
Why this over everything else?
There are some alternatives to this, but they usually lack one of the following:
- stupid simple internal structure: no node tree, no value/JSON wrappers, no in-repo custom reflection implementation, no PHP parsing
- doesn't rely on inheritance of serializable classes, hence allows serializing third-party classes
- parses existing PHPDoc information instead of duplicating it through attributes
- supports generic types which are quite useful for wrapper types
- allows simple extension through mappers and complex stuff through type adapters
- produces developer-friendly error messages for invalid data
- correctly handles optional (missing keys) and
nullvalues as separate concerns - simple to extend with additional formats
good-php/serialization 适用场景与选型建议
good-php/serialization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.54k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 good-php/serialization 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 good-php/serialization 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 25
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-08