8000 [Serializer] Docs for the PropertyInfo integration · matthieu88160/symfony-docs@8e2deb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e2deb0

Browse files
dunglasxabbuh
authored andcommitted
[Serializer] Docs for the PropertyInfo integration
1 parent a476938 commit 8e2deb0

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

components/serializer.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,74 @@ you indicate that you're expecting an array instead of a single object.
797797
$data = ...; // The serialized data from the previous example
798798
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
799799
800+
Recursive Denormalization and Type Safety
801+
-----------------------------------------
802+
803+
The Serializer Component can use the :doc:`PropertyInfo Component </components/property_info>` to denormalize
804+
complex types (objects). The type of the class' property will be guessed using the provided
805+
extractor and used to recursively denormalize the inner data.
806+
807+
When using the Symfony Standard Edition, all normalizers are automatically configured to use the registered extractors.
808+
When using the component standalone, an implementation of :class:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface`,
809+
(usually an instance of :class:`Symfony\\Component\\PropertyInfo\\PropertyInfoExtractor`) must be passed as the 4th
810+
parameter of the ``ObjectNormalizer``::
811+
812+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
813+
use Symfony\Component\Serializer\Serializer;
814+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
815+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
816+
817+
namespace Acme;
818+
819+
class ObjectOuter
820+
{
821+
private $inner;
822+
private $date;
823+
824+
public function getInner()
825+
{
826+
return $this->inner;
827+
}
828+
829+
public function setInner(ObjectInner $inner)
830+
{
831+
$this->inner = $inner;
832+
}
833+
834+
public function setDate(\DateTimeInterface $date)
835+
{
836+
$this->date = $date;
837+
}
838+
839+
public function getDate()
840+
{
841+
return $this->date;
842+
}
843+
}
844+
845+
class ObjectInner
846+
{
847+
public $foo;
848+
public $bar;
849+
}
850+
851+
$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor()); //
852+
$serializer = new Serializer(array(new DateTimeNormalizer(), $normalizer));
853+
854+
$obj = $serializer->denormalize(
855+
array('inner' => array('foo' => 'foo', 'bar' => 'bar'), 'date' => '1988/01/21'),
856+
'Acme\ObjectOuter'
857+
);
858+
859+
dump($obj->getInner()->foo); // 'foo'
860+
dump($obj->getInner()->bar); // 'bar'
861+
dump($obj->getDate()->format('Y-m-d')); // '1988-01-21'
862+
863+
When a ``PropertyTypeExtractor`` is available, the normalizer will also check that the data to denormalize
864+
matches the type of the property (even for primitive types). For instance, if a ``string`` is provided, but
865+
the type of the property is ``int``, an :class:`Symfony\\Component\\Serializer\\Exception\\UnexpectedValueException`
866+
will be thrown.
867+
800868
Learn more
801869
----------
802870

0 commit comments

Comments
 (0)
0