Skip to content

Commit 88dc6eb

Browse files
committed
[Serializer] Updated the cookbook.
1 parent 8b0c026 commit 88dc6eb

File tree

2 files changed

+125
-9
lines changed

2 files changed

+125
-9
lines changed

components/serializer.rst

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ needs three parameters:
145145
#. The name of the class this information will be decoded to
146146
#. The encoder used to convert that information into an array
147147

148+
.. _component-serializer-attributes-groups:
149+
148150
Attributes Groups
149151
-----------------
150152

cookbook/serializer.rst

+123-9
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ tools that you can leverage for your solution.
1111

1212
In fact, before you start, get familiar with the serializer, normalizers
1313
and encoders by reading the :doc:`Serializer Component</components/serializer>`.
14-
You should also check out the `JMSSerializerBundle`_, which expands on the
15-
functionality offered by Symfony's core serializer.
1614

1715
Activating the Serializer
1816
-------------------------
@@ -48,23 +46,48 @@ it in your configuration:
4846
$container->loadFromExtension('framework', array(
4947
// ...
5048
'serializer' => array(
51-
'enabled' => true
49+
'enabled' => true,
5250
),
5351
));
5452
53+
Using the Serializer Service
54+
----------------------------
55+
56+
Once enabled, the ``serializer`` service can be injected in any service where
57+
you need it or it can be used in a controller like the following::
58+
59+
// src/AppBundle/Controller/DefaultController.php
60+
namespace AppBundle\Controller;
61+
62+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
63+
64+
class DefaultController extends Controller
65+
{
66+
public function indexAction()
67+
{
68+
$serializer = $this->get('serializer');
69+
70+
// ...
71+
}
72+
}
73+
5574
Adding Normalizers and Encoders
5675
-------------------------------
5776

77+
.. versionadded:: 2.7
78+
The :class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`
79+
is enabled by default in Symfony 2.7. In prior versions, you need to load
80+
your own normalizer.
81+
5882
Once enabled, the ``serializer`` service will be available in the container
5983
and will be loaded with two :ref:`encoders<component-serializer-encoders>`
6084
(:class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder` and
61-
:class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder`)
62-
but no :ref:`normalizers<component-serializer-normalizers>`, meaning you'll
63-
need to load your own.
85+
:class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder`) and the
86+
:ref:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer normalizer <component-serializer-normalizers>`.
6487

6588
You can load normalizers and/or encoders by tagging them as
66-
:ref:`serializer.normalizer<reference-dic-tags-serializer-normalizer>` and
67-
:ref:`serializer.encoder<reference-dic-tags-serializer-encoder>`. It's also
89+
:ref:`serializer.normalizer <reference-dic-tags-serializer-normalizer>` and
90+
:ref:`serializer.encoder <reference-dic-tags-serializer-encoder>`. It's also
6891
possible to set the priority of the tag in order to decide the matching order.
6992

7093
Here is an example on how to load the
@@ -101,4 +124,95 @@ Here is an example on how to load the
101124
$definition->addTag('serializer.normalizer');
102125
$container->setDefinition('get_set_method_normalizer', $definition);
103126
104-
.. _JMSSerializerBundle: http://jmsyst.com/bundles/JMSSerializerBundle
127+
Using Serialization Groups Annotations
128+
--------------------------------------
129+
130+
.. versionadded:: 2.7
131+
Support for serialization groups was introduced in Symfony 2.7.
132+
133+
Enable :ref:`serialization groups annotation <component-serializer-attributes-groups>`
134+
with the following configuration:
135+
136+
.. configuration-block::
137+
138+
.. code-block:: yaml
139+
140+
# app/config/config.yml
141+
framework:
142+
# ...
143+
serializer:
144+
enable_annotations: true
145+
146+
.. code-block:: xml
147+
148+
<!-- app/config/config.xml -->
149+
<framework:config>
150+
<!-- ... -->
151+
<framework:serializer enable-annotations="true" />
152+
</framework:config>
153+
154+
.. code-block:: php
155+
156+
// app/config/config.php
157+
$container->loadFromExtension('framework', array(
158+
// ...
159+
'serializer' => array(
160+
'enable_annotations' => true,
161+
),
162+
));
163+
164+
Enabling the Metadata Cache
165+
---------------------------
166+
167+
.. versionadded:: 2.7
168+
Serializer metadata has been added in Symfony 2.7.
169+
170+
Metadata used by the Serializer component such as groups can be cached to
171+
enhance application performance. Any service implementing the ``Doctrine\Common\Cache\Cache``
172+
interface can be used.
173+
174+
A service leveraging `APCu`_ (and APC for PHP < 5.5) is built-in.
175+
176+
.. configuration-block::
177+
178+
.. code-block:: yaml
179+
180+
# app/config/config_prod.yml
181+
framework:
182+
# ...
183+
serializer:
184+
cache: serializer.mapping.cache.apc
185+
186+
.. code-block:: xml
187+
188+
<!-- app/config/config_prod.xml -->
189+
<framework:config>
190+
<!-- ... -->
191+
<framework:serializer cache="serializer.mapping.cache.apc" />
192+
</framework:config>
193+
194+
.. code-block:: php
195+
196+
// app/config/config_prod.php
197+
$container->loadFromExtension('framework', array(
198+
// ...
199+
'serializer' => array(
200+
'cache' => 'serializer.mapping.cache.apc',
201+
),
202+
));
203+
204+
Going Further with the Serializer Component
205+
-------------------------------------------
206+
207+
`DunglasApiBundle`_ provides an API system supporting `JSON-LD`_ and `Hydra Core Vocabulary`_
208+
hypermedia formats. It is built on top of the Symfony Framework and its Serializer
209+
component. It provides custom normalizers and a custom encoder, custom metadata
210+
and a caching system.
211+
212+
If you want to leverage the full power of the Symfony Serializer component,
213+
take a look at how this bundle works.
214+
215+
.. _`APCu`: https://github.com/krakjoe/apcu
216+
.. _`DunglasApiBundle`: https://github.com/dunglas/DunglasApiBundle
217+
.. _`JSON-LD`: http://json-ld.org
218+
.. _`Hydra Core Vocabulary`: http://hydra-cg.com

0 commit comments

Comments
 (0)