Skip to content

Document metadata aware name conversion #10422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,79 @@ processes::
$anne = $normalizer->denormalize(array('first_name' => 'Anne'), 'Person');
// Person object with firstName: 'Anne'

Configure name conversion using metadata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When using this component inside a Symfony application and the class metadata factory is enabled
as explained in the :ref:`Attributes Groups section <component-serializer-attributes-groups>`,
this is already set up and you only need to provide the configuration. Otherwise::

// ...
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);

$serializer = new Serializer(
array(new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)),
array('json' => new JsonEncoder())
);

Now configure your name conversion mapping. Consider an application that
defines a ``Person`` entity with a ``firstName`` property:

.. configuration-block::

.. code-block:: php-annotations

namespace App\Entity;

use Symfony\Component\Serializer\Annotation\SerializedName;

class Person
{
/**
* @SerializedName("firstname")
*/
private $firstName;

public function __construct($firstName)
{
$this->firstName = $firstName;
}

// ...
}

.. code-block:: yaml

App\Entity\Person:
attributes:
firstName:
serialized_name: firstname

.. code-block:: xml

<?xml version="1.0" ?>
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="App\Entity\Person">
<attribute name="firstName" serialized-name="firstname" />
</class>
</serializer>

Once configured, the serializer uses the mapping to convert pproperty names when serializing and deserializing::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo pproperty 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(fixed in 2121344)


$serialized = $serializer->serialize(new Person("Kévin"));
// {"firstname": "Kévin"}

Serializing Boolean Attributes
------------------------------

Expand Down