Skip to content

Commit 97b1181

Browse files
committed
[Serializer] Updated the cookbook.
1 parent 8b0c026 commit 97b1181

File tree

2 files changed

+122
-6
lines changed

2 files changed

+122
-6
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: 120 additions & 6 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,19 +46,44 @@ 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 used in a controller like the following::
58+
59+
namespace AppBundle\Controller;
60+
61+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
62+
63+
class DefaultController extends Controller
64+
{
65+
public function indexAction()
66+
{
67+
$serializer = $this->get('serializer');
68+
69+
// ...
70+
}
71+
}
72+
5573
Adding Normalizers and Encoders
5674
-------------------------------
5775

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

6588
You can load normalizers and/or encoders by tagging them as
6689
:ref:`serializer.normalizer<reference-dic-tags-serializer-normalizer>` and
@@ -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+
The serialization group system has been added 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 ``Doctrine\Common\Cache\Cache``
172+
interface can be used.
173+
174+
A service leveraging `APCu`_ (and APC for PHP < 5.5) is builtin.
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 hypermedia
208+
formats. It is built on top of the Symfony framework and its Serializer component.
209+
It provides custom normalizers and a custom encoder, custom metadata and a caching
210+
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`: http://hydra-cg.com

0 commit comments

Comments
 (0)