jojo1981 / jms-serializer-handlers
Handlers for the JMS Serializer
Installs: 10 138
Dependents: 0
Suggesters: 1
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- ext-dom: *
- jms/serializer: ^3.0
- jojo1981/typed-collection: ^5.0
- jojo1981/typed-set: ^5.0
Requires (Dev)
- dg/bypass-finals: ^1.3
- doctrine/cache: ^1.8
- php-coveralls/php-coveralls: ^2.4
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
README
Author: Joost Nijhuis <jnijhuis81@gmail.com>
This library is an extension for the jms/serializer
package and contains custom handlers which add support to work with some 3th party libraries.
More information about JMS Serializer Handlers can be found here.
This library adds support for:
- instances of
Jojo1981\TypedCollection\Collection
from the packagejojo1981/typed-collection
.
Installation
Library
git clone https://github.com/jojo1981/jms-serializer-handlers.git
Composer
composer require jojo1981/jms-serializer-handlers
Usage
<?php use JMS\Serializer\Accessor\DefaultAccessorStrategy; use JMS\Serializer\Construction\UnserializeObjectConstructor; use JMS\Serializer\Handler\HandlerRegistry; use JMS\Serializer\Naming\CamelCaseNamingStrategy; use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; use JMS\Serializer\SerializerBuilder; use Jojo1981\JmsSerializerHandlers\TypedCollectionAccessorStrategyDecorator; use Jojo1981\JmsSerializerHandlers\TypedCollectionObjectConstructorDecorator; use Jojo1981\JmsSerializerHandlers\TypedCollectionSerializationHandler; use Jojo1981\JmsSerializerHandlers\TypedSetSerializationHandler; use Jojo1981\JmsSerializerHandlers\UnionSerializationHandler; require 'vendor/autoload.php'; $propertyNamingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()); $accessorStrategy = new TypedCollectionAccessorStrategyDecorator(new DefaultAccessorStrategy()); $serializer = (new SerializerBuilder()) ->setDebug(true) ->setCacheDir(__DIR__ . '/var/cache/serializer') ->setAccessorStrategy($accessorStrategy) ->setPropertyNamingStrategy($propertyNamingStrategy) ->addDefaultHandlers() ->configureHandlers(static function (HandlerRegistry $handlerRegistry): void { $handlerRegistry->registerSubscribingHandler(new TypedCollectionSerializationHandler()); $handlerRegistry->registerSubscribingHandler(new TypedSetSerializationHandler()); $handlerRegistry->registerSubscribingHandler(new UnionSerializationHandler()); }) ->setObjectConstructor(new TypedCollectionObjectConstructorDecorator(new UnserializeObjectConstructor())) ->build();