1of0/json
最新稳定版本:0.3.4
Composer 安装命令:
composer require 1of0/json
包简介
Abstraction layer for json_encode() and json_decode(), providing Json.NET-like functionality through annotations or mappings
README 文档
README
1of0/php-serializer
This library provides advanced serialization features built over the PHP json_encode() and json_decode() functions.
Most of the features are inspired by the popular .NET library Json.NET.
Installation
This library is available on Packagist, and installable with composer:
composer require 1of0/json
Quick start
The most straightforward way of using this library is using the static methods on the Convert class. The Convert
class is a static facade around the singleton instance of the Serializer class.
<?php
use OneOfZero\Json\Convert;
use OneOfZero\Json\Serializer;
// Basic serialization
$json = Convert::toJson($myObject);
// Basic deserialization
$object = Convert::fromJson($json);
// Type hint example
$object = Convert::fromJson($json, \MyNamespace\MyClass::class);
// This is a more verbose form of Convert::toJson($myObject)
$json = Serializer::get()->serialize($myObject);
How does it work?
The serializer takes annotated or XML/YAML/JSON/PHP mapped objects, and pre-processes them before feeding them to the
json_encode() function. Inversely, the deserializer feeds the JSON to the json_decode() function, and
post-processes the result to get as close a match to the original object (assuming it's properly mapped/annotated).
Documentation
There is ApiGen generated documentation available:
- The minimal documentation excludes documentation for internal classes.
- The full documentation includes internal classes, and can be useful when extending the serializer.
Other documentation and a set of examples is being worked on. If you need help getting something to work, or if something is not clear, don't hesitate to file an issue.
Features
Mappers and mapper chaining
The serializer and deserializer behaviour can be influenced with mappings on classes and class members. This library supports mappings with:
- Annotations
- XML
- YAML
- JSON
- PHP
These mappers can be chained in any order to provide a single merged mapping.
Embedded type information
This is a feature inspired by the zumba/json-serializer library.
By default the serializer will embed type information in serialized objects. The type information allows the deserializer to deserialize a JSON object into its original type without PHPDoc, annotations or mappings.
The embedded data is the additional @type property that holds the fully qualified class name of the serialized
object:
{
"@type": "MyNamespace\\MyClass",
"propertyA": "valueA",
"propertyB": "valueB",
...
}
Serialization
The embedding of type information can be disabled for individual classes with class-level mappings, or can disabled globally in the configuration:
<?php
$configuration->embedTypeMetadata = false;
Deserialization
Due to security concerns the deserializer only deserializes whitelisted types (by default this whitelist is empty). Classes can be added to the whitelist through the configuration:
<?php
$configuration->getMetaHintWhitelist()->allowClass(MyClass::class);
You can also whitelist by namespace, class inheritance, or regex. See the API documentation of the MetaHintWhiteList
class for details.
Object and member converters
Much like Json.NET's custom converters, this library also allows you to build and specify custom converters for specified properties.
Contract resolver
Another feature that was loosely ported from Json.NET is the contract resolver. A contract resolver allows you to dynamically manipulate the serialization mapping for every node in the (de)serialization tree. An example use-case for a contract resolver is conversion of property names.
Serialization groups
(not implemented yet)
Reference properties
You might often deal with sub-objects that you don't want to serialize, but rather just reference. This library can serialize and deserialize references like that. To achieve this:
- The referenced object needs to implement the
ReferableInterfaceinterface - The property that holds the referenced object has to be marked with the
@IsReferenceannotation - A reference resolver needs to exist that supports the referenced object (and needs to implement the
ReferenceResolverInterfaceinterface)
Bugs and feature requests
Please post any bugs and feature requests to the issue tracker on GitLab.
Also don't hesitate to file an issue if the documentation is lacking.
License
The library is licensed under the MIT license, of which the full text can be found in the LICENSE file.
1of0/json 适用场景与选型建议
1of0/json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 194 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 08 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「annotations」 「json」 「serialization」 「serializer」 「json.net」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 1of0/json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 1of0/json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 1of0/json 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Fork of jms/serializer 1.14.1 with support for modern PHP versions.
A collection of helpers for PHPUnit to ease testing Tarantool libraries.
Kinikit - PHP Application development framework MVC component
De/normalize business objects without tightly coupling them to your normalization format
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 194
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-19