tsantos/fast-object-normalizer
Composer 安装命令:
composer require tsantos/fast-object-normalizer
包简介
Generate dedicated normalizers for your objects to increase serialization performance
README 文档
README
Symfony Object Normalizer that improves the serialization process up to 5x (e.g. 400%) compared to built-in normalizers. This boost of performance can be accomplished because this normalizer generates dedicated normalizes to your data classes, so you don't need to worry about runtime overhead like reflections or code relying on metadata (e.g. ClassMetadata).
Instalation
composer require tsantos/fast-object-normalizer
Symfony Applications
This package ships with a Symfony Bundle that autmatically adds the normalizer to the normalizer stack.
Applications using Flex
Just install the package and the Symfony Flex automatically register the bundle into your application.
Applications not using Flex
You need to register the bundle manually:
<?php // config/bundles.php return [ // .. TSantos\FastObjectNormalizer\Bridge\Symfony\FastObjectNormalizerBundle::class => ['all' => true] ];
Standalone Applications
For applications using Symfony Serializer as a standalone component, you'll need to register the normalizer manually:
<?php require 'vendor/autoload.php'; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\ObjectDenormalizer; $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); $discriminator = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); $serializer = new Serializer([ new ArrayDenormalizer(), new FastObjectNormalizer( classGenerator: new NormalizerClassGenerator($classMetadataFactory, $discriminator), classDumper: new NormalizerClassDumper('./var/cache/serializer'), classMetadataFactory: $classMetadataFactory, ), new ObjectNormalizer($classMetadataFactory) ], ['json' => new JsonEncoder()]);
Note that ObjectNormalizer still in the normalizer stack. This is not a requirement, but it is a good idea to have
the built-in normalizer working because Fast Object Normalizer may not support the object that is being serialized.
Configuration
Fast Object Normalizer is aimed to serialize data class (e.g: DTO), but in somecases it is possible that your
application tries to serialize an object which holds a resource or a connection and normally there is an exclusive
normalizer that deals with that type. To ensure that Fast Object Normalizer skip such types, you can configure it
to serialize only types that matchs some pattern:
<?php $normalizer = new FastObjectNormalizer( includedTypes: ['^App\\\Entity'] //... other args here )
# config/packages/fast_object_normalizer.yaml fast_object_normalizer: includedTypes: ["^App\\Entity"]
Configuring the normalizer with includedTypes you know exactly what types the normalizer will support. In the major of
times, you'll opt to this configuration to avoid weird behaviors.
There is also the oposite of includedTypes which now will accepts all types except those defined in the excludedList:
<?php $normalizer = new FastObjectNormalizer( includedTypes: ['^App\\\Entity'] //... other args here )
# config/packages/fast_object_normalizer.yaml fast_object_normalizer: excludedTypes: ["^Some\\Vendor\\Object"]
Finally, you can combine both options:
<?php $normalizer = new FastObjectNormalizer( includedTypes: ['^App\\\Entity'], excludedTypes: ['^App\\\Entity\\\\ExcludedType'], //... other args here )
# config/packages/fast_object_normalizer.yaml fast_object_normalizer: excludedTypes: ['^App\\\Entity'] includedTypes: ["^App\\\Entity\\\\ExcludedType"]
With this configuration, you are allowing all types starting with App\Entity but excluding App\Entity\ExcludedType.
YOU MUST BE AWARE
This packages tries to be the more transparent as possible to allow you to use it without break your application.
As explained previously, this package creates dedicated normalizers to serialize your data as fast as possible, which
means that the built-in normalizer AbstractObjectNormalizer will not be reached. It means that features like attribute
grouping, null value skipping, attribute callback etc. had to be reimplemented here to boost performance, that is,
it is possible to have some different behaviors compared to AbstractObjectNormalizer. Please, open an issue if you
find some divergent behavior.
The table below list all the features currently supported by built-in normalizer and Fast Object Normalizer.
| Feature | Builtin Normalizer | Fast Object Normalizer |
|---|---|---|
| Groups | Yes | Yes |
| Ignore attributes | Yes | Yes |
| Skip null values | Yes | Yes |
| Serialization of Interfaces and Abstract classes | Yes | Yes |
| Attribute callbacks | Yes | Yes |
| Circular Reference Handle | Yes | Yes |
| Object to populate | Yes | Yes |
| Default constructor args | Yes | Yes |
| Max depth handle | Yes | Not yet |
| Avoid extra attributes | Yes | Not yet |
| Type enforcement | Yes | Not yet |
| Deep object to populate | Yes | Not yet |
| Preserve empty objects | Yes | Not yet |
| Skip uninitialized values | Yes | Not yet |
If you want to use Fast Object Normalizer but need a specific feature to be implemented, I'd be very happy to
point you in how we can implement it.
tsantos/fast-object-normalizer 适用场景与选型建议
tsantos/fast-object-normalizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tsantos/fast-object-normalizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tsantos/fast-object-normalizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-31