Skip to content

Commit 10e5203

Browse files
committed
[Serializer] Updated the cookbook.
1 parent 8b0c026 commit 10e5203

File tree

2 files changed

+126
-8
lines changed

2 files changed

+126
-8
lines changed

components/serializer.rst

Lines changed: 2 additions & 0 deletions
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

Lines changed: 124 additions & 8 deletions
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,50 @@ 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 or it can be used in a controller like the following::
58+
59+
// src/AppBundle/Controller/DefaultController.php
60+
61+
namespace AppBundle\Controller;
62+
63+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
64+
65+
class DefaultController extends Controller
66+
{
67+
public function indexAction()
68+
{
69+
$serializer = $this->get('serializer');
70+
71+
// ...
72+
}
73+
}
74+
5575
Adding Normalizers and Encoders
5676
-------------------------------
5777

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

6590
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
91+
:ref:`serializer.normalizer <reference-dic-tags-serializer-normalizer>` and
92+
:ref:`serializer.encoder <reference-dic-tags-serializer-encoder>`. It's also
6893
possible to set the priority of the tag in order to decide the matching order.
6994

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

0 commit comments

Comments
 (0)