@@ -33,8 +33,12 @@ 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+
3637.. include :: /components/require_autoload.rst.inc
3738
39+ To use the ``ObjectNormalizer ``, the :doc: `PropertyAccess component </components/property_access/index >`
40+ must also be installed.
41+
3842Usage
3943-----
4044
@@ -45,18 +49,18 @@ which Encoders and Normalizer are going to be available::
4549 use Symfony\Component\Serializer\Serializer;
4650 use Symfony\Component\Serializer\Encoder\XmlEncoder;
4751 use Symfony\Component\Serializer\Encoder\JsonEncoder;
48- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
52+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
4953
5054 $encoders = array(new XmlEncoder(), new JsonEncoder());
51- $normalizers = array(new GetSetMethodNormalizer ());
55+ $normalizers = array(new ObjectNormalizer ());
5256
5357 $serializer = new Serializer($normalizers, $encoders);
5458
55- There are several normalizers available, e.g. the
56- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ` or
57- the :class: ` Symfony \\ Component \\ Serializer \\ Normalizer \\ PropertyNormalizer ` .
59+ The preferred normalizer is the
60+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `, but other
61+ normalizers are available .
5862To read more about them, refer to the `Normalizers `_ section of this page. All
59- the examples shown below use the ``GetSetMethodNormalizer ``.
63+ the examples shown below use the ``ObjectNormalizer ``.
6064
6165Serializing an Object
6266---------------------
@@ -147,6 +151,28 @@ needs three parameters:
147151#. The name of the class this information will be decoded to
148152#. The encoder used to convert that information into an array
149153
154+ Deserializing in an Existing Object
155+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156+
157+ The serializer can also be used to update an existing object::
158+
159+ $person = new Acme\Person();
160+ $person->setName('bar');
161+ $person->setAge(99);
162+ $person->setSportsman(true);
163+
164+ $data = <<<EOF
165+ <person>
166+ <name>foo</name>
167+ <age>69</age>
168+ </person>
169+ EOF;
170+
171+ $serializer->deserialize($data, 'Acme\Person', 'xml', array('object_to_populate' => $person));
172+ // $obj2 = Acme\Person(name: 'foo', age: '99', sportsman: true)
173+
174+ This is a common need when working with an ORM.
175+
150176.. _component-serializer-attributes-groups :
151177
152178Attributes Groups
@@ -289,8 +315,13 @@ You are now able to serialize only attributes in the groups you want::
289315Ignoring Attributes
290316-------------------
291317
318+ .. note ::
319+
320+ Using attribute groups instead of the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
321+ method is considered best practice.
322+
292323.. versionadded :: 2.3
293- The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
324+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
294325 method was introduced in Symfony 2.3.
295326
296327.. versionadded :: 2.7
@@ -299,14 +330,14 @@ Ignoring Attributes
299330
300331As an option, there's a way to ignore attributes from the origin object. To remove
301332those attributes use the
302- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
333+ :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
303334method on the normalizer definition::
304335
305336 use Symfony\Component\Serializer\Serializer;
306337 use Symfony\Component\Serializer\Encoder\JsonEncoder;
307- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
338+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
308339
309- $normalizer = new GetSetMethodNormalizer ();
340+ $normalizer = new ObjectNormalizer ();
310341 $normalizer->setIgnoredAttributes(array('age'));
311342 $encoder = new JsonEncoder();
312343
@@ -363,11 +394,11 @@ including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormal
363394and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `::
364395
365396 use Symfony\Component\Serializer\Encoder\JsonEncoder
366- use Symfony\Component\Serializer\Normalizer\PropertyNormalizer ;
397+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
367398 use Symfony\Component\Serializer\Serializer;
368399
369400 $nameConverter = new OrgPrefixNameConverter();
370- $normalizer = new PropertyNormalizer (null, $nameConverter);
401+ $normalizer = new ObjectNormalizer (null, $nameConverter);
371402
372403 $serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
373404
@@ -398,9 +429,9 @@ snake_case and CamelCased styles during serialization and deserialization
398429processes::
399430
400431 use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
401- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
432+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
402433
403- $normalizer = new GetSetMethodNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
434+ $normalizer = new ObjectNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
404435
405436 class Person
406437 {
@@ -431,6 +462,9 @@ If you are using isser methods (methods prefixed by ``is``, like
431462``Acme\Person::isSportsman() ``), the Serializer component will automatically
432463detect and use it to serialize related attributes.
433464
465+ The ``ObjectNormalizer `` also takes care of methods starting with ``has ``, ``add ``
466+ and ``remove ``.
467+
434468Using Callbacks to Serialize Properties with Object Instances
435469-------------------------------------------------------------
436470
@@ -467,23 +501,42 @@ Normalizers
467501
468502There are several types of normalizers available:
469503
504+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
505+ This normalizer leverages the :doc: `PropertyAccess Component </components/property_access/index >`
506+ to read and write in the object. It means that it can access to properties
507+ directly and trough getters, setters, hassers, adders and removers. It supports
508+ calling the constructor during the denormalization process.
509+
510+ Objects are normalized to a map of property names (method name stripped of
511+ the "get"/"set"/"has"/"remove" prefix and converted to lower case) to property
512+ values.
513+
514+ The ``ObjectNormalizer `` is the most powerful normalizer. It is a configured
515+ by default when using the Symfony Standard Edition with the serializer enabled.
516+
470517:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
471518 This normalizer reads the content of the class by calling the "getters"
472519 (public methods starting with "get"). It will denormalize data by calling
473520 the constructor and the "setters" (public methods starting with "set").
474521
475- Objects are serialized to a map of property names (method name stripped of
522+ Objects are normalized to a map of property names (method name stripped of
476523 the "get" prefix and converted to lower case) to property values.
477524
478525:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
479526 This normalizer directly reads and writes public properties as well as
480- **private and protected ** properties. Objects are normalized to a map of
481- property names to property values.
527+ **private and protected ** properties. It supports calling the constructor
528+ during the denormalization process.
529+
530+ Objects are normalized to a map of property names to property values.
482531
483- .. versionadded :: 2.6 The
484- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
532+ .. versionadded :: 2.6
533+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
485534 class was introduced in Symfony 2.6.
486535
536+ .. versionadded :: 2.7
537+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
538+ class was introduced in Symfony 2.7.
539+
487540Handling Circular References
488541----------------------------
489542
@@ -569,7 +622,7 @@ by custom callables. This is especially useful when serializing entities
569622having unique identifiers::
570623
571624 $encoder = new JsonEncoder();
572- $normalizer = new GetSetMethodNormalizer ();
625+ $normalizer = new ObjectNormalizer ();
573626
574627 $normalizer->setCircularReferenceHandler(function ($object) {
575628 return $object->getName();
0 commit comments