Skip to content

Commit 7d83726

Browse files
committed
feature #4675 [Serializer] Doc for groups support (dunglas)
This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Doc for groups support | Q | A | --------------- | --- | Doc fix? | no | New docs? | yes symfony/symfony#12092 | Applies to | 2.7 This is the documentation for serialization groups support. Commits ------- e417395 [Serializer] Fix CS ae2b78c [Serializer] Add getter group example 745f412 [Serializer] Doc for groups support
2 parents 1fa0659 + e417395 commit 7d83726

File tree

1 file changed

+165
-30
lines changed

1 file changed

+165
-30
lines changed

components/serializer.rst

+165-30
Original file line numberDiff line numberDiff line change
@@ -122,29 +122,6 @@ The first parameter of the :method:`Symfony\\Component\\Serializer\\Serializer::
122122
is the object to be serialized and the second is used to choose the proper encoder,
123123
in 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-
148125
Deserializing an Object
149126
-----------------------
150127

@@ -168,6 +145,168 @@ 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+
166+
private $bar;
167+
168+
public function getBar()
169+
{
170+
return $this->bar;
171+
}
172+
173+
public function setBar($bar)
174+
{
175+
return $this->bar = $bar;
176+
}
177+
}
178+
179+
The definition of serialization can be specified using annotations, XML
180+
or YAML. The :class:`Symfony\\Component\\Serializer\\Mapping\\Factory\\ClassMetadataFactory`
181+
that will be used by the normalizer must be aware of the format to use.
182+
183+
Initialize the :class:`Symfony\\Component\\Serializer\\Mapping\\Factory\\ClassMetadataFactory`
184+
like the following::
185+
186+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
187+
// For annotations
188+
usr Doctrine\Common\Annotations\AnnotationReader;
189+
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
190+
// For XML
191+
// use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
192+
// For YAML
193+
// use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
194+
195+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
196+
// For XML
197+
// $classMetadataFactory = new ClassMetadataFactory(new XmlFileLoader('/path/to/your/definition.xml'));
198+
// For YAML
199+
// $classMetadataFactory = new ClassMetadataFactory(new YamlFileLoader('/path/to/your/definition.yml'));
200+
201+
Then, create your groups definition:
202+
203+
.. configuration-block::
204+
205+
.. code-block:: php-annotations
206+
207+
namespace Acme;
208+
209+
use Symfony\Component\Serializer\Annotation\Groups;
210+
211+
class MyObj
212+
{
213+
/**
214+
* @Groups({"group1", "group2"})
215+
*/
216+
public $foo;
217+
218+
/**
219+
* @Groups({"group3"})
220+
*/
221+
public function getBar() // is* methods are also supported
222+
{
223+
return $this->bar;
224+
}
225+
226+
// ...
227+
}
228+
229+
.. code-block:: yaml
230+
231+
Acme\MyObj:
232+
attributes:
233+
foo:
234+
groups: ['group1', 'group2']
235+
bar:
236+
groups: ['group3']
237+
238+
.. code-block:: xml
239+
240+
<?xml version="1.0" ?>
241+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
242+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
243+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
244+
http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
245+
>
246+
<class name="Acme\MyObj">
247+
<attribute name="foo">
248+
<group>group1</group>
249+
<group>group2</group>
250+
</attribute>
251+
252+
<attribute name="bar">
253+
<group>group3</group>
254+
</attribute>
255+
</class>
256+
</serializer>
257+
258+
You are now able to serialize only attributes in the groups you want::
259+
260+
use Symfony\Component\Serializer\Serializer;
261+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
262+
263+
$obj = new MyObj();
264+
$obj->foo = 'foo';
265+
$obj->setBar('bar');
266+
267+
$normalizer = new ObjectNormalizer($classMetadataFactory);
268+
$serializer = new Serializer(array($normalizer));
269+
270+
$data = $serializer->normalize($obj, null, array('groups' => array('group1')));
271+
// $data = ['foo' => 'foo'];
272+
273+
$obj2 = $serializer->denormalize(
274+
array('foo' => 'foo', 'bar' => 'bar'),
275+
'MyObj',
276+
null,
277+
array('groups' => array('group1', 'group3'))
278+
);
279+
// $obj2 = MyObj(foo: 'foo', bar: 'bar')
280+
281+
.. _ignoring-attributes-when-serializing:
282+
283+
Ignoring Attributes
284+
-------------------
285+
286+
.. versionadded:: 2.3
287+
The :method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
288+
method was introduced in Symfony 2.3.
289+
290+
.. versionadded:: 2.7
291+
Prior to Symfony 2.7, attributes were only ignored while serializing. Since Symfony
292+
2.7, they are ignored when deserializing too.
293+
294+
As an option, there's a way to ignore attributes from the origin object. To remove
295+
those attributes use the
296+
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setIgnoredAttributes`
297+
method on the normalizer definition::
298+
299+
use Symfony\Component\Serializer\Serializer;
300+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
301+
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
302+
303+
$normalizer = new GetSetMethodNormalizer();
304+
$normalizer->setIgnoredAttributes(array('age'));
305+
$encoder = new JsonEncoder();
306+
307+
$serializer = new Serializer(array($normalizer), array($encoder));
308+
$serializer->serialize($person, 'json'); // Output: {"name":"foo","sportsman":false}
309+
171310
Converting Property Names when Serializing and Deserializing
172311
------------------------------------------------------------
173312

@@ -434,14 +573,10 @@ having unique identifiers::
434573
echo $serializer->serialize($org, 'json');
435574
// {"name":"Les-Tilleuls.coop","members":[{"name":"K\u00e9vin", organization: "Les-Tilleuls.coop"}]}
436575

437-
JMSSerializer
438-
-------------
576+
.. seealso::
439577

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.
578+
A popular alternative to the Symfony Serializer Component is the third-party
579+
library, `JMS serializer`_ (released under the Apache license, so incompatible with GPLv2 projects).
445580

446581
.. _`JMS serializer`: https://github.com/schmittjoh/serializer
447582
.. _Packagist: https://packagist.org/packages/symfony/serializer

0 commit comments

Comments
 (0)