@@ -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,50 @@ 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 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
+
55
75
Adding Normalizers and Encoders
56
76
-------------------------------
57
77
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
+
58
83
Once enabled, the ``serializer `` service will be available in the container
59
84
and will be loaded with two :ref: `encoders<component-serializer-encoders> `
60
85
(:class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder ` and
61
86
: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> ` .
64
89
65
90
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
68
93
possible to set the priority of the tag in order to decide the matching order.
69
94
70
95
Here is an example on how to load the
@@ -101,4 +126,95 @@ Here is an example on how to load the
101
126
$definition->addTag('serializer.normalizer');
102
127
$container->setDefinition('get_set_method_normalizer', $definition);
103
128
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