Skip to content

Commit bfd41e3

Browse files
committed
[Serializer] ObjectNormalizer, object_to_populate doc. Minor enhancements.
1 parent 8b0c026 commit bfd41e3

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

components/serializer.rst

+70-18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ You can install the component in 2 different ways:
3333
* :doc:`Install it via Composer </components/using_components>` (``symfony/serializer`` on `Packagist`_);
3434
* Use the official Git repository (https://github.com/symfony/Serializer).
3535

36+
To use the ``ObjectNormalizer``, the :doc:`PropertyAccess component </components/property_access/index>`
37+
must also be installed.
38+
3639
Usage
3740
-----
3841

@@ -43,18 +46,18 @@ which Encoders and Normalizer are going to be available::
4346
use Symfony\Component\Serializer\Serializer;
4447
use Symfony\Component\Serializer\Encoder\XmlEncoder;
4548
use Symfony\Component\Serializer\Encoder\JsonEncoder;
46-
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
49+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
4750

4851
$encoders = array(new XmlEncoder(), new JsonEncoder());
49-
$normalizers = array(new GetSetMethodNormalizer());
52+
$normalizers = array(new ObjectNormalizer());
5053

5154
$serializer = new Serializer($normalizers, $encoders);
5255

53-
There are several normalizers available, e.g. the
54-
:class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer` or
55-
the :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`.
56+
The preferred normalizer is
57+
:class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer` but other
58+
normalizers are available.
5659
To read more about them, refer to the `Normalizers`_ section of this page. All
57-
the examples shown below use the ``GetSetMethodNormalizer``.
60+
the examples shown below use the ``ObjectNormalizer``.
5861

5962
Serializing an Object
6063
---------------------
@@ -145,6 +148,28 @@ needs three parameters:
145148
#. The name of the class this information will be decoded to
146149
#. The encoder used to convert that information into an array
147150

151+
Deserializing in an Existing Object
152+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153+
154+
The serializer can also be used to update an existing object::
155+
156+
$person = new Acme\Person();
157+
$person->setName('bar');
158+
$person->setAge(99);
159+
$person->setSportsman(true);
160+
161+
$data = <<<EOF
162+
<person>
163+
<name>foo</name>
164+
<age>69</age>
165+
</person>
166+
EOF;
167+
168+
$serializer->deserialize($data, 'Acme\Person', 'xml', array('object_to_populate' => $person));
169+
// $obj2 = Acme\Person(name: 'foo', age: '99', sportsman: true)
170+
171+
This is a common need when working with an ORM.
172+
148173
Attributes Groups
149174
-----------------
150175

@@ -283,8 +308,13 @@ You are now able to serialize only attributes in the groups you want::
283308
Ignoring Attributes
284309
-------------------
285310

311+
.. note::
312+
313+
Using attribute groups instead of the :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
314+
method is considered best practice.
315+
286316
.. versionadded:: 2.3
287-
The :method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
317+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
288318
method was introduced in Symfony 2.3.
289319

290320
.. versionadded:: 2.7
@@ -293,14 +323,14 @@ Ignoring Attributes
293323

294324
As an option, there's a way to ignore attributes from the origin object. To remove
295325
those attributes use the
296-
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
326+
:method:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer::setIgnoredAttributes`
297327
method on the normalizer definition::
298328

299329
use Symfony\Component\Serializer\Serializer;
300330
use Symfony\Component\Serializer\Encoder\JsonEncoder;
301-
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
331+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
302332

303-
$normalizer = new GetSetMethodNormalizer();
333+
$normalizer = new ObjectNormalizer();
304334
$normalizer->setIgnoredAttributes(array('age'));
305335
$encoder = new JsonEncoder();
306336

@@ -357,11 +387,11 @@ including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormal
357387
and :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`::
358388

359389
use Symfony\Component\Serializer\Encoder\JsonEncoder
360-
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
390+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
361391
use Symfony\Component\Serializer\Serializer;
362392

363393
$nameConverter = new OrgPrefixNameConverter();
364-
$normalizer = new PropertyNormalizer(null, $nameConverter);
394+
$normalizer = new ObjectNormalizer(null, $nameConverter);
365395

366396
$serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
367397

@@ -392,9 +422,9 @@ snake_case and CamelCased styles during serialization and deserialization
392422
processes::
393423

394424
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
395-
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
425+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
396426

397-
$normalizer = new GetSetMethodNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
427+
$normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
398428

399429
class Person
400430
{
@@ -425,6 +455,9 @@ If you are using isser methods (methods prefixed by ``is``, like
425455
``Acme\Person::isSportsman()``), the Serializer component will automatically
426456
detect and use it to serialize related attributes.
427457

458+
The ``ObjectNormalizer`` also take care of methods starting by ``has``, ``add``
459+
and ``remove``.
460+
428461
Using Callbacks to Serialize Properties with Object Instances
429462
-------------------------------------------------------------
430463

@@ -461,23 +494,42 @@ Normalizers
461494

462495
There are several types of normalizers available:
463496

497+
:class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`
498+
This normalizer leverages the :doc:`PropertyAccess Component </components/property_access/index>`
499+
to read and write in the object. It means that it can access to properties
500+
directly and trough getters, setters, hassers, adders and removers.
501+
It supports calling the constructor during the denormalization process.
502+
503+
Objects are normalized to a map of property names (method name stripped of
504+
the "get"/"set"/"has"/"remove" prefix and converted to lower case) to property
505+
values.
506+
507+
The ``ObjectNormalizer`` is the most powerful normalizer. It is a configured
508+
by default when using the Symfony Standard Edition with the serializer enabled.
509+
464510
:class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer`
465511
This normalizer reads the content of the class by calling the "getters"
466512
(public methods starting with "get"). It will denormalize data by calling
467513
the constructor and the "setters" (public methods starting with "set").
468514

469-
Objects are serialized to a map of property names (method name stripped of
515+
Objects are normalized to a map of property names (method name stripped of
470516
the "get" prefix and converted to lower case) to property values.
471517

472518
:class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`
473519
This normalizer directly reads and writes public properties as well as
474-
**private and protected** properties. Objects are normalized to a map of
475-
property names to property values.
520+
**private and protected** properties.
521+
It supports calling the constructor during the denormalization process.
522+
523+
Objects are normalized to a map of property names to property values.
476524

477525
.. versionadded:: 2.6 The
478526
:class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`
479527
class was introduced in Symfony 2.6.
480528

529+
.. versionadded:: 2.7 The
530+
:class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`
531+
class was introduced in Symfony 2.7.
532+
481533
Handling Circular References
482534
----------------------------
483535

@@ -563,7 +615,7 @@ by custom callables. This is especially useful when serializing entities
563615
having unique identifiers::
564616

565617
$encoder = new JsonEncoder();
566-
$normalizer = new GetSetMethodNormalizer();
618+
$normalizer = new ObjectNormalizer();
567619

568620
$normalizer->setCircularReferenceHandler(function ($object) {
569621
return $object->getName();

0 commit comments

Comments
 (0)