Skip to content

Commit 9a402c2

Browse files
committed
minor #10422 Document metadata aware name conversion (fbourigault)
This PR was merged into the master branch. Discussion ---------- Document metadata aware name conversion Documentation for symfony/symfony#28505. I wrote this section with strong inspiration from the discriminator section. # To do - [ ] Add an introduction Commits ------- 9d50c77 document metadata aware name conversion
2 parents 46b6c9f + 9d50c77 commit 9a402c2

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

components/serializer.rst

+73
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,79 @@ processes::
531531
$anne = $normalizer->denormalize(array('first_name' => 'Anne'), 'Person');
532532
// Person object with firstName: 'Anne'
533533

534+
Configure name conversion using metadata
535+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
536+
537+
When using this component inside a Symfony application and the class metadata factory is enabled
538+
as explained in the :ref:`Attributes Groups section <component-serializer-attributes-groups>`,
539+
this is already set up and you only need to provide the configuration. Otherwise::
540+
541+
// ...
542+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
543+
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
544+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
545+
use Symfony\Component\Serializer\Serializer;
546+
547+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
548+
549+
$metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory);
550+
551+
$serializer = new Serializer(
552+
array(new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)),
553+
array('json' => new JsonEncoder())
554+
);
555+
556+
Now configure your name conversion mapping. Consider an application that
557+
defines a ``Person`` entity with a ``firstName`` property:
558+
559+
.. configuration-block::
560+
561+
.. code-block:: php-annotations
562+
563+
namespace App\Entity;
564+
565+
use Symfony\Component\Serializer\Annotation\SerializedName;
566+
567+
class Person
568+
{
569+
/**
570+
* @SerializedName("firstname")
571+
*/
572+
private $firstName;
573+
574+
public function __construct($firstName)
575+
{
576+
$this->firstName = $firstName;
577+
}
578+
579+
// ...
580+
}
581+
582+
.. code-block:: yaml
583+
584+
App\Entity\Person:
585+
attributes:
586+
firstName:
587+
serialized_name: firstname
588+
589+
.. code-block:: xml
590+
591+
<?xml version="1.0" ?>
592+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
593+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
594+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
595+
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
596+
>
597+
<class name="App\Entity\Person">
598+
<attribute name="firstName" serialized-name="firstname" />
599+
</class>
600+
</serializer>
601+
602+
Once configured, the serializer uses the mapping to convert pproperty names when serializing and deserializing::
603+
604+
$serialized = $serializer->serialize(new Person("Kévin"));
605+
// {"firstname": "Kévin"}
606+
534607
Serializing Boolean Attributes
535608
------------------------------
536609

0 commit comments

Comments
 (0)