Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9a402c2

Browse filesBrowse 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
Copy full SHA for 9a402c2

File tree

1 file changed

+73
-0
lines changed
Filter options

1 file changed

+73
-0
lines changed

‎components/serializer.rst

Copy file name to clipboardExpand all lines: components/serializer.rst
+73Lines changed: 73 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.