JMSSerializerBundle Documentation
repository·master·Indexed 23 days ago
https://github.com/schmittjoh/jmsserializerbundleA Symfony bundle that integrates the JMS Serializer library into Symfony applications for advanced object-to-format conversion (such as JSON and XML). It provides configuration for custom serializer handlers, event listeners, expression language functions, and metadata directories, as well as a Twig filter for direct serialization.
What's inside JMSSerializerBundle
- JMSSerializerBundle is a Symfony bundle that integrates the serializer library into the Symfony framework. It allows you to use the powerful JMS Serializer capabilities within your Symfony application for object serialization and deserialization.
Upgrade Custom Handlers and Traversable objects from 0.9 to 0.10
masterWhen upgrading from 0.9 to 0.10:
- Custom Handlers:
SerializationHandlerInterfaceandDeserializationHandlerInterfacehave been removed. Use the new handler system for specific types, or the event system for handling arbitrary/unknown types. - Traversable Objects: Objects implementing
Traversableare no longer treated specially and are serialized as regular objects. To restore previous behavior, use the@Typeannotation to force serialization to anarray:
/** @Type("array") */ private $myTraversableObject;- Datetime Handlers: Most
jms_serializer.handlersconfiguration is gone. You can still configure the built-indatetimehandler, but it is recommended to specify the format directly via the@Typeannotation:
/** @Type("DateTime<'Y-m-d', 'UTC'>") */ private $createdAt;/** @Type("array") */ private $myTraversableObject; /** @Type("DateTime<'Y-m-d', 'UTC'>") */ private $createdAt;- Custom Handlers:
Upgrade Serializer alias and DateTime format from 1.x to 2.0
masterWhen upgrading from version 1.x to 2.0, note the following breaking changes:
- Alias Change: The
serializeralias has been removed. Use thejms_serializeralias instead. - DateTime Format: The default datetime format has changed from
ISO8601(Y-m-d\TH:i:sO) toRFC3339(Y-m-d\TH:i:sP). - Metadata Configuration: Defining non-existent metadata directories will now trigger an exception. Additionally, the
nameattribute (orkey) for metadata directory definitions is now mandatory.
- Alias Change: The
Register JMSSerializerBundle in Symfony
masterAfter installation, you must register the bundle in your
AppKernel.phpfile within theregisterBundles()method.// in AppKernel::registerBundles() $bundles = array( // ... new JMS\SerializerBundle\JMSSerializerBundle(), // ... );Install JMSSerializerBundle via Composer
masterTo download the latest stable version of the bundle, run the following command in your project directory. This requires Composer to be installed on your system.
$ composer require jms/serializer-bundleUpgrade Naming Strategy configuration from 2.x to 3.0
masterIn version 3.0, defining naming strategies via parameters is no longer supported. You must now define the naming strategy under the
jms_serializer.property_namingconfiguration key using the service ID.# Before 3.0 parameters: jms_serializer.serialized_name_annotation_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy # After 3.0 jms_serializer: property_naming: id: 'jms_serializer.identical_property_naming_strategy' # service id of the naming strategyEnable JMSSerializerBundle in Symfony Standard applications
masterFor applications using the Symfony Standard structure, you must manually register the bundle in your Kernel by adding it to the
registerBundles()method inapp/AppKernel.php.<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new JMS\SerializerBundle\JMSSerializerBundle(), ); // ... } }Add custom functions to Expression Language
masterYou can extend the Expression Language used by the serializer by providing a custom function provider via the
jms.expression.function_providertag. Your provider must implementSymfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterfaceand return an array ofExpressionFunctionobjects in itsgetFunctions()method.use Symfony\Component\ExpressionLanguage\ExpressionFunction; use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; class MyFunctionProvider implements ExpressionFunctionProviderInterface { public function getFunctions() { return [ new ExpressionFunction('str_rot13', function ($arg) { return sprintf('str_rot13(%s)', $arg); }, function (array $variables, $value) { return str_rot13($value); }) ]; } }Upgrade Twig filter from 3.x to 4.0
masterWhen upgrading from version 3.x to 4.0, the Twig filter used for serialization has been renamed. Replace theserializefilter withjms_serialize.Register custom Serializer Handlers
masterYou can register custom handlers to manage specific types or formats during serialization or deserialization. Use the
jms_serializer.handlerorjms_serializer.subscribing_handlertags on your service.If you omit the
methodattribute, the bundle will attempt to use a default naming convention:serializeTypeToFormatfor serialization ordeserializeTypeFromFormatfor deserialization.my_handler: class: MyHandler tags: - name: jms_serializer.handler type: DateTime direction: serialization format: json method: serializeDateTimeToJsonRegister Serializer Event Listeners and Subscribers
masterTo hook into the serialization lifecycle, register listeners using the
jms_serializer.event_listenerorjms_serializer.event_subscribertags.Note: These are not standard Symfony kernel events; you cannot use
kernel.event_listeneror@DI\Observeannotations to catch these. You must use the specific JMS serializer tags.Install JMSSerializerBundle via Composer
masterInstall the bundle using the Composer package manager by running the following command:
composer require jms/serializer-bundle