Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit9d50c77

Browse files
committed
document metadata aware name conversion
1 parentd4310d9 commit9d50c77

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

‎components/serializer.rst‎

Lines 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+
<serializerxmlns="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+
<classname="App\Entity\Person">
598+
<attributename="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)

[8]ページ先頭

©2009-2025 Movatter.jp