@@ -116,29 +116,6 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
116116is the object to be serialized and the second is used to choose the proper encoder,
117117in this case :class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder `.
118118
119- Ignoring Attributes when Serializing
120- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121-
122- .. versionadded :: 2.3
123- The :method: `GetSetMethodNormalizer::setIgnoredAttributes<Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes> `
124- method was introduced in Symfony 2.3.
125-
126- As an option, there's a way to ignore attributes from the origin object when
127- serializing. To remove those attributes use the
128- :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes `
129- method on the normalizer definition::
130-
131- use Symfony\Component\Serializer\Serializer;
132- use Symfony\Component\Serializer\Encoder\JsonEncoder;
133- use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
134-
135- $normalizer = new GetSetMethodNormalizer();
136- $normalizer->setIgnoredAttributes(array('age'));
137- $encoder = new JsonEncoder();
138-
139- $serializer = new Serializer(array($normalizer), array($encoder));
140- $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman":false}
141-
142119Deserializing an Object
143120-----------------------
144121
@@ -162,6 +139,145 @@ needs three parameters:
1621392. The name of the class this information will be decoded to
1631403. The encoder used to convert that information into an array
164141
142+ Attributes Groups
143+ -----------------
144+
145+ .. versionadded :: 2.7
146+ The support of serialization and deserialization groups has been added
147+ in Symfony 2.7.
148+
149+ Sometimes, you want to serialize different set of attributes from your
150+ entities. Groups are a handy way to achieve this need.
151+
152+ Lets start with a simple Plain Old PHP object.
153+
154+ namespace Acme;
155+
156+ class MyObj
157+ {
158+ public $foo;
159+ public $bar;
160+ }
161+
162+ The definition of serialization can be specified using annotations, XML
163+ and YAML. The :class: Symfony\C omponent\S erializer\M apping\F actory\C lassMetadataFactory
164+ that will be used by the normalizer must be aware of the format to use.
165+
166+ If you want to use annotations to define groups, initialize the :class: Symfony\C omponent\S erializer\M apping\F actory\C lassMetadataFactory
167+ like the following:
168+
169+ use Symfony\C omponent\S erializer\M apping\L oader\A nnotationLoader;
170+ use Symfony\C omponent\S erializer\M apping\F actory\C lassMetadataFactory;
171+
172+ $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
173+
174+ If you prefer XML:
175+
176+ use Symfony\C omponent\S erializer\M apping\F actory\C lassMetadataFactory;
177+ use Symfony\C omponent\S erializer\M apping\L oader\X mlFileLoader;
178+
179+ $classMetadataFactory = new ClassMetadataFactory(new XmlFileLoader('/path/to/your/definition.xml'));
180+
181+ And for YAML:
182+
183+ use Symfony\C omponent\S erializer\M apping\F actory\C lassMetadataFactory;
184+ use Symfony\C omponent\S erializer\M apping\L oader\Y amlFileLoader;
185+
186+ $classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader('/path/to/your/definition.yml'));
187+
188+ Then, create your groups definition:
189+
190+ .. configuration-block ::
191+
192+ .. code-block :: php-annotations
193+
194+ namespace Acme;
195+
196+ use Symfony\Component\Serializer\Annotation\Groups;
197+
198+ class MyObj
199+ {
200+ /**
201+ * @Groups({"group1", "group2"})
202+ */
203+ public $foo;
204+ /**
205+ * @Groups({"group3"})
206+ */
207+ public $bar;
208+ }
209+
210+ .. code-block :: yaml
211+
212+ Acme\MyObj :
213+ attributes :
214+ foo :
215+ groups : ['group1', 'group2']
216+ bar :
217+ groups : ['group3']
218+
219+ .. code-block :: xml
220+
221+ <?xml version =" 1.0" ?>
222+ <serializer xmlns =" http://symfony.com/schema/dic/serializer"
223+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
224+ xsi : schemaLocation =" http://symfony.com/schema/dic/serializer http://symfony.com/schema/dic/constraint-mapping/serializer-1.0.xsd" >
225+ <class name =" Acme\MyObj" >
226+ <attribute name =" foo" >
227+ <group name =" group1" />
228+ <group name =" group2" />
229+ </attribute >
230+
231+ <attribute name =" bar" >
232+ <group name =" group3" />
233+ </attribute >
234+ </class >
235+ </serializer >
236+
237+ You are now able to serialize only attributes in the groups you want:
238+
239+ use Symfony\C omponent\S erializer\S erializer;
240+ use Symfony\C omponent\S erializer\N ormalizer\G etSetMethodNormalizer;
241+
242+ $obj = new MyObj();
243+ $obj->foo = 'foo';
244+ $obj->bar = 'bar';
245+
246+ $normalizer = new PropertyNormalizer($classMetadataFactory);
247+ $serializer = new Serializer(array($normalizer));
248+
249+ $data = $serializer->normalize($obj, null, array('groups' => array('group1')));
250+ // $data = ['foo' => 'foo'];
251+
252+ $obj2 = $serializer->denormalize(array('foo' => 'foo', 'bar' => 'bar'), 'MyObj', null, array('groups' => array('group1', 'group3')));
253+ // $obj2 = MyObj(foo: 'foo', bar: 'bar')
254+
255+ Ignoring Attributes
256+ -------------------
257+
258+ .. versionadded :: 2.3
259+ The :method: `GetSetMethodNormalizer::setIgnoredAttributes<Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes> `
260+ method was introduced in Symfony 2.3.
261+
262+ .. versionadded :: 2.7
263+ Before Symfony 2.7, attributes was only ignored while serializing. Since Symfony
264+ 2.7, they are ignored when deserializing too.
265+
266+ As an option, there's a way to ignore attributes from the origin object. To remove
267+ those attributes use the :method: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer::setIgnoredAttributes `
268+ method on the normalizer definition::
269+
270+ use Symfony\Component\Serializer\Serializer;
271+ use Symfony\Component\Serializer\Encoder\JsonEncoder;
272+ use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
273+
274+ $normalizer = new GetSetMethodNormalizer();
275+ $normalizer->setIgnoredAttributes(array('age'));
276+ $encoder = new JsonEncoder();
277+
278+ $serializer = new Serializer(array($normalizer), array($encoder));
279+ $serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman":false}
280+
165281Using Camelized Method Names for Underscored Attributes
166282-------------------------------------------------------
167283
@@ -333,14 +449,4 @@ having unique identifiers::
333449 echo $serializer->serialize($org, 'json');
334450 // {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"]}
335451
336- JMSSerializer
337- -------------
338-
339- A popular third-party library, `JMS serializer `_, provides a more
340- sophisticated albeit more complex solution. This library includes the
341- ability to configure how your objects should be serialized/deserialized via
342- annotations (as well as YAML, XML and PHP), integration with the Doctrine ORM,
343- and handling of other complex cases.
344-
345- .. _`JMS serializer` : https://github.com/schmittjoh/serializer
346452.. _Packagist : https://packagist.org/packages/symfony/serializer
0 commit comments