@@ -11,8 +11,6 @@ tools that you can leverage for your solution.
11
11
12
12
In fact, before you start, get familiar with the serializer, normalizers
13
13
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.
16
14
17
15
Activating the Serializer
18
16
-------------------------
@@ -48,23 +46,48 @@ it in your configuration:
48
46
$container->loadFromExtension('framework', array(
49
47
// ...
50
48
'serializer' => array(
51
- 'enabled' => true
49
+ 'enabled' => true,
52
50
),
53
51
));
54
52
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
+
55
74
Adding Normalizers and Encoders
56
75
-------------------------------
57
76
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
+
58
82
Once enabled, the ``serializer `` service will be available in the container
59
83
and will be loaded with two :ref: `encoders<component-serializer-encoders> `
60
84
(: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 >`.
64
87
65
88
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
68
91
possible to set the priority of the tag in order to decide the matching order.
69
92
70
93
Here is an example on how to load the
@@ -101,4 +124,95 @@ Here is an example on how to load the
101
124
$definition->addTag('serializer.normalizer');
102
125
$container->setDefinition('get_set_method_normalizer', $definition);
103
126
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