Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Cookbook
  4. How to Use the Serializer
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Activating the Serializer
  • Adding Normalizers and Encoders

How to Use the Serializer

Edit this page

Warning: You are browsing the documentation for Symfony 2.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

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. You should also check out the JMSSerializerBundle, which expands on the functionality offered by Symfony's core serializer.

Activating the Serializer

2.3

The Serializer has always existed in Symfony, but prior to Symfony 2.3, you needed to build the serializer service yourself.

The serializer service is not available by default. To turn it on, activate it in your configuration:

  • YAML
  • XML
  • PHP
1
2
3
4
5
# app/config/config.yml
framework:
    # ...
    serializer:
        enabled: true
1
2
3
4
5
<!-- app/config/config.xml -->
<framework:config>
    <!-- ... -->
    <framework:serializer enabled="true" />
</framework:config>
1
2
3
4
5
6
7
// app/config/config.php
$container->loadFromExtension('framework', array(
    // ...
    'serializer' => array(
        'enabled' => true
    ),
));

Adding Normalizers and Encoders

Once enabled, the serializer service will be available in the container and will be loaded with two encoders (JsonEncoder and XmlEncoder) but no normalizers, meaning you'll need to load your own.

You can load normalizers and/or encoders 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 GetSetMethodNormalizer:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
# app/config/services.yml
services:
    get_set_method_normalizer:
        class: Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer
        tags:
            - { name: serializer.normalizer }
1
2
3
4
5
6
<!-- app/config/services.xml -->
<services>
    <service id="get_set_method_normalizer" class="Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer">
        <tag name="serializer.normalizer" />
    </service>
</services>
1
2
3
4
5
6
7
8
// app/config/services.php
use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition(
    'Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer'
));
$definition->addTag('serializer.normalizer');
$container->setDefinition('get_set_method_normalizer', $definition);

Note

The GetSetMethodNormalizer is broken by design. As soon as you have a circular object graph, an infinite loop is created when calling the getters. You're encouraged to add your own normalizers that fit your use-case.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Get your Symfony expertise recognized

    Get your Symfony expertise recognized

    Check Code Performance in Dev, Test, Staging & Production

    Check Code Performance in Dev, Test, Staging & Production

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Samuel Vogel, a Symfony contributor

    Thanks Samuel Vogel (@samuelvogel) for being a Symfony contributor

    1 commit • 32 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia