@@ -122,29 +122,6 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
122122is the object to be serialized and the second is used to choose the proper encoder,
123123in this case :class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder `.
124124
125- Ignoring Attributes when Serializing
126- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127-
128- .. versionadded :: 2.3
129- The :method: `GetSetMethodNormalizer::setIgnoredAttributes<Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes> `
130- method was introduced in Symfony 2.3.
131-
132- As an option, there's a way to ignore attributes from the origin object when
133- serializing. To remove those attributes use the
134- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes `
135- method on the normalizer definition::
136-
137- use Symfony\Component\Serializer\Serializer;
138- use Symfony\Component\Serializer\Encoder\JsonEncoder;
139- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
140-
141- $normalizer = new GetSetMethodNormalizer();
142- $normalizer->setIgnoredAttributes(array('age'));
143- $encoder = new JsonEncoder();
144-
145- $serializer = new Serializer(array($normalizer), array($encoder));
146- $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman":false}
147-
148125Deserializing an Object
149126-----------------------
150127
@@ -168,6 +145,152 @@ needs three parameters:
168145#. The name of the class this information will be decoded to
169146#. The encoder used to convert that information into an array
170147
148+ Attributes Groups
149+ -----------------
150+
151+ .. versionadded :: 2.7
152+ The support of serialization and deserialization groups was introduced
153+ in Symfony 2.7.
154+
155+ Sometimes, you want to serialize different sets of attributes from your
156+ entities. Groups are a handy way to achieve this need.
157+
158+ Assume you have the following plain-old-PHP object::
159+
160+ namespace Acme;
161+
162+ class MyObj
163+ {
164+ public $foo;
165+ public $bar;
166+ }
167+
168+ The definition of serialization can be specified using annotations, XML
169+ or YAML. The :class: `Symfony\\ Component\\ Serializer\\ Mapping\\ Factory\\ ClassMetadataFactory `
170+ that will be used by the normalizer must be aware of the format to use.
171+
172+ Initialize the :class: `Symfony\\ Component\\ Serializer\\ Mapping\\ Factory\\ ClassMetadataFactory `
173+ like the following::
174+
175+ use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
176+ // For annotations
177+ usr Doctrine\Common\Annotations\AnnotationReader;
178+ use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
179+ // For XML
180+ // use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
181+ // For YAML
182+ // use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
183+
184+ $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
185+ // For XML
186+ // $classMetadataFactory = new ClassMetadataFactory(new XmlFileLoader('/path/to/your/definition.xml'));
187+ // For YAML
188+ // $classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader('/path/to/your/definition.yml'));
189+
190+ Then, create your groups definition:
191+
192+ .. configuration-block ::
193+
194+ .. code-block :: php-annotations
195+
196+ namespace Acme;
197+
198+ use Symfony\Component\Serializer\Annotation\Groups;
199+
200+ class MyObj
201+ {
202+ /**
203+ * @Groups({"group1", "group2"})
204+ */
205+ public $foo;
206+
207+ /**
208+ * @Groups({"group3"})
209+ */
210+ public $bar;
211+ }
212+
213+ .. code-block :: yaml
214+
215+ Acme\MyObj :
216+ attributes :
217+ foo :
218+ groups : ['group1', 'group2']
219+ bar :
220+ groups : ['group3']
221+
222+ .. code-block :: xml
223+
224+ <?xml version =" 1.0" ?>
225+ <serializer xmlns =" http://symfony.com/schema/dic/serializer-mapping"
226+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
227+ xsi : schemaLocation =" http://symfony.com/schema/dic/serializer-mapping
228+ http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
229+ >
230+ <class name =" Acme\MyObj" >
231+ <attribute name =" foo" >
232+ <group >group1</group >
233+ <group >group2</group >
234+ </attribute >
235+
236+ <attribute name =" bar" >
237+ <group >group3</group >
238+ </attribute >
239+ </class >
240+ </serializer >
241+
242+ You are now able to serialize only attributes in the groups you want::
243+
244+ use Symfony\Component\Serializer\Serializer;
245+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
246+
247+ $obj = new MyObj();
248+ $obj->foo = 'foo';
249+ $obj->bar = 'bar';
250+
251+ $normalizer = new ObjectNormalizer($classMetadataFactory);
252+ $serializer = new Serializer(array($normalizer));
253+
254+ $data = $serializer->normalize($obj, null, array('groups' => array('group1')));
255+ // $data = ['foo' => 'foo'];
256+
257+ $obj2 = $serializer->denormalize(
258+ array('foo' => 'foo', 'bar' => 'bar'),
259+ 'MyObj',
260+ null,
261+ array('groups' => array('group1', 'group3'))
262+ );
263+ // $obj2 = MyObj(foo: 'foo', bar: 'bar')
264+
265+ .. _ignoring-attributes-when-serializing :
266+
267+ Ignoring Attributes
268+ -------------------
269+
270+ .. versionadded :: 2.3
271+ The :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes `
272+ method was introduced in Symfony 2.3.
273+
274+ .. versionadded :: 2.7
275+ Prior to Symfony 2.7, attributes were only ignored while serializing. Since Symfony
276+ 2.7, they are ignored when deserializing too.
277+
278+ As an option, there's a way to ignore attributes from the origin object. To remove
279+ those attributes use the
280+ :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes `
281+ method on the normalizer definition::
282+
283+ use Symfony\Component\Serializer\Serializer;
284+ use Symfony\Component\Serializer\Encoder\JsonEncoder;
285+ use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
286+
287+ $normalizer = new GetSetMethodNormalizer();
288+ $normalizer->setIgnoredAttributes(array('age'));
289+ $encoder = new JsonEncoder();
290+
291+ $serializer = new Serializer(array($normalizer), array($encoder));
292+ $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman":false}
293+
171294Converting Property Names when Serializing and Deserializing
172295------------------------------------------------------------
173296
@@ -434,14 +557,10 @@ having unique identifiers::
434557 echo $serializer->serialize($org, 'json');
435558 // {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
436559
437- JMSSerializer
438- -------------
560+ .. seealso ::
439561
440- A popular third-party library, `JMS serializer `_, provides a more
441- sophisticated albeit more complex solution. This library includes the
442- ability to configure how your objects should be serialized/deserialized via
443- annotations (as well as YAML, XML and PHP), integration with the Doctrine ORM,
444- and handling of other complex cases.
562+ A popular alternative to the Symfony Serializer Component is the third-party
563+ library, `JMS serializer `_ (released under the Apache license, so incompatible with GPLv2 projects).
445564
446565.. _`JMS serializer` : https://github.com/schmittjoh/serializer
447566.. _Packagist : https://packagist.org/packages/symfony/serializer
0 commit comments