You are browsing the documentation for Symfony 3.3 which is not maintained anymore.
Consider upgrading your projects to Symfony 5.2.
How to Use the Serializer
How to Use the Serializer¶
Serializing and deserializing to and from objects and different formats (e.g. JSON or XML) is a very complex topic. Symfony comes with a Serializer Component, which gives you some tools that you can leverage for your solution.
In fact, before you start, get familiar with the serializer, normalizers and encoders by reading the Serializer Component.
Activating the Serializer¶
The serializer
service is not available by default. To turn it on, activate
it in your configuration:
- YAML
1 2 3 4 5 6
# app/config/config.yml framework: # ... serializer: { enable_annotations: true } # Alternatively, if you don't want to use annotations #serializer: { enabled: true }
- XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<!-- app/config/config.xml --> <?xml version="1.0" encoding="UTF-8"?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:framework="http://symfony.com/schema/dic/symfony" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> <framework:config> <!-- ... --> <framework:serializer enable-annotations="true" /> <!-- Alternatively, if you don't want to use annotations <framework:serializer enabled="true" /> --> </framework:config> </container>
- PHP
1 2 3 4 5 6 7 8 9
// app/config/config.php $container->loadFromExtension('framework', array( // ... 'serializer' => array( 'enable_annotations' => true, // Alternatively, if you don't want to use annotations //'enabled' => true, ), ));
Using the Serializer Service¶
Once enabled, the serializer
service can be injected in any service where
you need it or it can be used in a controller:
// src/AppBundle/Controller/DefaultController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Serializer\SerializerInterface;
class DefaultController extends Controller
{
public function indexAction(SerializerInterface $serializer)
{
// keep reading for usage examples
}
}
Adding Normalizers and Encoders¶
Once enabled, the serializer
service will be available in the container.
It comes with a set of useful encoders
and normalizers.
Encoders supporting the following formats are enabled:
- JSON:
Symfony\Component\Serializer\Encoder\JsonEncoder
- XML:
Symfony\Component\Serializer\Encoder\XmlEncoder
As well as the following normalizers:
Symfony\Component\Serializer\Normalizer\ObjectNormalizer
to handle typical data objectsSymfony\Component\Serializer\Normalizer\DateTimeNormalizer
for objects implementing theDateTimeInterface
interfaceSymfony\Component\Serializer\Normalizer\DataUriNormalizer
to transformSplFileInfo
objects in Data URIsSymfony\Component\Serializer\Normalizer\JsonSerializableNormalizer
to deal with objects implementing theJsonSerializable
interfaceSymfony\Component\Serializer\Normalizer\ArrayDenormalizer
to denormalize arrays of objects using a format like MyObject[] (note the [] suffix)
Custom normalizers and/or encoders can also be loaded by tagging them as serializer.normalizer and serializer.encoder. It’s also possible to set the priority of the tag in order to decide the matching order.
Here is an example on how to load the
Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
, a
faster alternative to the ObjectNormalizer when data objects always use
getters and setters:
- YAML
1 2 3 4 5 6
# app/config/services.yml services: get_set_method_normalizer: class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer public: false tags: [serializer.normalizer]
- XML
1 2 3 4 5 6 7 8 9 10 11 12 13
<!-- app/config/services.xml --> <?xml version="1.0" encoding="UTF-8"?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="get_set_method_normalizer" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer" public="false"> <tag name="serializer.normalizer" /> </service> </services> </container>
- PHP
1 2 3 4 5 6 7
// app/config/services.php use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; $container->register('get_set_method_normalizer', GetSetMethodNormalizer::class) ->setPublic(false) ->addTag('serializer.normalizer') ;
Using Serialization Groups Annotations¶
Enable serialization groups annotation with the following configuration:
- YAML
1 2 3 4 5
# app/config/config.yml framework: # ... serializer: enable_annotations: true
- XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!-- app/config/config.xml --> <?xml version="1.0" encoding="UTF-8"?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:framework="http://symfony.com/schema/dic/symfony" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> <framework:config> <!-- ... --> <framework:serializer enable-annotations="true" /> </framework:config> </container>
- PHP
1 2 3 4 5 6 7
// app/config/config.php $container->loadFromExtension('framework', array( // ... 'serializer' => array( 'enable_annotations' => true, ), ));
Next, add the @Groups annotations to your class and choose which groups to use when serializing:
$json = $serializer->serialize(
$someObject,
'json', array('groups' => array('group1'))
);
In addition to the @Groups
annotation, the Serializer component also
supports Yaml or XML files. These files are automatically loaded when being
stored in one of the following locations:
- The
serialization.yml
orserialization.xml
file in theResources/config/
directory of a bundle; - All
*.yml
and*.xml
files in theResources/config/serialization/
directory of a bundle.
Enabling the Metadata Cache¶
Metadata used by the Serializer component such as groups can be cached to
enhance application performance. Any service implementing the Doctrine\Common\Cache\Cache
interface can be used.
A service leveraging APCu (and APC for PHP < 5.5) is built-in.
- YAML
1 2 3 4 5
# app/config/config_prod.yml framework: # ... serializer: cache: serializer.mapping.cache.apc
- XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!-- app/config/config_prod.xml --> <?xml version="1.0" encoding="UTF-8"?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:framework="http://symfony.com/schema/dic/symfony" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> <framework:config> <!-- ... --> <framework:serializer cache="serializer.mapping.cache.apc" /> </framework:config> </container>
- PHP
1 2 3 4 5 6 7
// app/config/config_prod.php $container->loadFromExtension('framework', array( // ... 'serializer' => array( 'cache' => 'serializer.mapping.cache.apc', ), ));
Enabling a Name Converter¶
The use of a name converter service can be defined in the configuration using the name_converter option.
The built-in CamelCase to snake_case name converter
can be enabled by using the serializer.name_converter.camel_case_to_snake_case
value:
- YAML
1 2 3 4 5
# app/config/config.yml framework: # ... serializer: name_converter: 'serializer.name_converter.camel_case_to_snake_case'
- XML
1 2 3 4 5
<!-- app/config/config.xml --> <framework:config> <!-- ... --> <framework:serializer name-converter="serializer.name_converter.camel_case_to_snake_case" /> </framework:config>
- PHP
1 2 3 4 5 6 7
// app/config/config.php $container->loadFromExtension('framework', array( // ... 'serializer' => array( 'name_converter' => 'serializer.name_converter.camel_case_to_snake_case', ), ));
Going Further with the Serializer¶
ApiPlatform provides an API system supporting JSON-LD and Hydra Core Vocabulary hypermedia formats. It is built on top of the Symfony Framework and its Serializer component. It provides custom normalizers and a custom encoder, custom metadata and a caching system.
If you want to leverage the full power of the Symfony Serializer component, take a look at how this bundle works.
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.