@@ -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,30 @@ 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+
176+ .. _component-serializer-attributes-groups :
177+
150178Attributes Groups
151179-----------------
152180
@@ -200,6 +228,8 @@ like the following::
200228 // For YAML
201229 // $classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader('/path/to/your/definition.yml'));
202230
231+ .. _component-serializer-attributes-groups-annotations :
232+
203233Then, create your groups definition:
204234
205235.. configuration-block ::
@@ -285,8 +315,13 @@ You are now able to serialize only attributes in the groups you want::
285315Ignoring Attributes
286316-------------------
287317
318+ .. note ::
319+
320+ Using attribute groups instead of the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer::setIgnoredAttributes `
321+ method is considered best practice.
322+
288323.. versionadded :: 2.3
289- The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
324+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
290325 method was introduced in Symfony 2.3.
291326
292327.. versionadded :: 2.7
@@ -295,14 +330,14 @@ Ignoring Attributes
295330
296331As an option, there's a way to ignore attributes from the origin object. To remove
297332those attributes use the
298- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer ::setIgnoredAttributes `
333+ :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ AbstractNormalizer ::setIgnoredAttributes `
299334method on the normalizer definition::
300335
301336 use Symfony\Component\Serializer\Serializer;
302337 use Symfony\Component\Serializer\Encoder\JsonEncoder;
303- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
338+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
304339
305- $normalizer = new GetSetMethodNormalizer ();
340+ $normalizer = new ObjectNormalizer ();
306341 $normalizer->setIgnoredAttributes(array('age'));
307342 $encoder = new JsonEncoder();
308343
@@ -359,11 +394,11 @@ including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormal
359394and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `::
360395
361396 use Symfony\Component\Serializer\Encoder\JsonEncoder
362- use Symfony\Component\Serializer\Normalizer\PropertyNormalizer ;
397+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
363398 use Symfony\Component\Serializer\Serializer;
364399
365400 $nameConverter = new OrgPrefixNameConverter();
366- $normalizer = new PropertyNormalizer (null, $nameConverter);
401+ $normalizer = new ObjectNormalizer (null, $nameConverter);
367402
368403 $serializer = new Serializer(array($normalizer), array(new JsonEncoder()));
369404
@@ -394,9 +429,9 @@ snake_case and CamelCased styles during serialization and deserialization
394429processes::
395430
396431 use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
397- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer ;
432+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ;
398433
399- $normalizer = new GetSetMethodNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
434+ $normalizer = new ObjectNormalizer (null, new CamelCaseToSnakeCaseNameConverter());
400435
401436 class Person
402437 {
@@ -427,6 +462,9 @@ If you are using isser methods (methods prefixed by ``is``, like
427462``Acme\Person::isSportsman() ``), the Serializer component will automatically
428463detect and use it to serialize related attributes.
429464
465+ The ``ObjectNormalizer `` also takes care of methods starting with ``has ``, ``add ``
466+ and ``remove ``.
467+
430468Using Callbacks to Serialize Properties with Object Instances
431469-------------------------------------------------------------
432470
@@ -463,23 +501,42 @@ Normalizers
463501
464502There are several types of normalizers available:
465503
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 through 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+
466517:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
467518 This normalizer reads the content of the class by calling the "getters"
468519 (public methods starting with "get"). It will denormalize data by calling
469520 the constructor and the "setters" (public methods starting with "set").
470521
471- 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
472523 the "get" prefix and converted to lower case) to property values.
473524
474525:class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
475526 This normalizer directly reads and writes public properties as well as
476- **private and protected ** properties. Objects are normalized to a map of
477- 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.
478531
479- .. versionadded :: 2.6 The
480- :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
532+ .. versionadded :: 2.6
533+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `
481534 class was introduced in Symfony 2.6.
482535
536+ .. versionadded :: 2.7
537+ The :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ ObjectNormalizer `
538+ class was introduced in Symfony 2.7.
539+
483540Handling Circular References
484541----------------------------
485542
@@ -565,7 +622,7 @@ by custom callables. This is especially useful when serializing entities
565622having unique identifiers::
566623
567624 $encoder = new JsonEncoder();
568- $normalizer = new GetSetMethodNormalizer ();
625+ $normalizer = new ObjectNormalizer ();
569626
570627 $normalizer->setCircularReferenceHandler(function ($object) {
571628 return $object->getName();
0 commit comments