How to Use the Serializer
In applications using Symfony Flex, run this command toinstall the before using it:
Using the Serializer Service
Once enabled, the serializer service can be injected in any service whereyou need it or it can be used in a controller:
- // src/Controller/DefaultController.php
- namespace App\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\Serializer\SerializerInterface;
- class DefaultController extends AbstractController
- {
- public function index(SerializerInterface $serializer)
- {
- // keep reading for usage examples
- }
- }
Once enabled, the serializer
service will be available in the container.It comes with a set of useful and normalizers.
Encoders supporting the following formats are enabled:
- JSON:
- XML:
XmlEncoder
- CSV:
DateTimeNormalizer
forobjects implementing theinterface
DateTimeZoneNormalizer
forobjects
DataUriNormalizer
totransform objects inJsonSerializableNormalizer
to deal with objects implementing theinterface
ArrayDenormalizer
todenormalize arrays of objects using a format like MyObject[] (note the [] suffix)
New in version 4.3: The DateTimeZoneNormalizer
was introduced in Symfony 4.3.
Custom normalizers and/or encoders can also be loaded by tagging them as andserializer.encoder. It's alsopossible to set the priority of the tag in order to decide the matching order.
Here is an example on how to load the, afaster alternative to the ObjectNormalizer when data objects always usegetters (
getXxx()
), issers (isXxx()
) or hassers (hasXxx()
) to readproperties and setters (setXxx()
) to change properties:
- YAML
- # config/services.yaml
- services:
- get_set_method_normalizer:
- class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
- public: false
- tags: [serializer.normalizer]
- XML
- PHP
- // 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
To use annotations, first add support for them via the SensioFrameworkExtraBundle:
- $ composer require sensio/framework-extra-bundle
Tip
The value of the groups
key can be a single string, or an array of strings.
In addition to the @Groups
annotation, the Serializer component alsosupports YAML or XML files. These files are automatically loaded when beingstored in one of the following locations:
- All and
.xml
files in theconfig/serializer/
directory. - The
serialization.yaml
orserialization.xml
file intheResources/config/
directory of a bundle;
The metadata for the serializer is automatically cached to enhance applicationperformance. By default, the serializer uses the cache.system
cache poolwhich is configured using the option.
Enabling a Name Converter
The use of a service can be defined in the configuration using the name_converteroption.
The built-in can be enabled by using the value:
- YAML
- # config/packages/framework.yaml
- framework:
- # ...
- serializer:
- name_converter: 'serializer.name_converter.camel_case_to_snake_case'
- XML
- <!-- config/packages/framework.xml -->
- <framework:config>
- <!-- ... -->
- <framework:serializer name-converter="serializer.name_converter.camel_case_to_snake_case"/>
- </framework:config>
- PHP
- JSON-LD along with the
- OpenAPI v2 (formerly Swagger) and v3
- JSON:API
- JSON
- XML
- YAML
- CSVIt is built on top of the Symfony Framework and its Serializercomponent. It provides custom normalizers and a custom encoder, custom metadataand a caching system.
If you want to leverage the full power of the Symfony Serializer component,take a look at how this bundle works.