@@ -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,19 +46,44 @@ 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 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
+
55
73
Adding Normalizers and Encoders
56
74
-------------------------------
57
75
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
+
58
81
Once enabled, the ``serializer `` service will be available in the container
59
82
and will be loaded with two :ref: `encoders<component-serializer-encoders> `
60
83
(:class: `Symfony\\ Component\\ Serializer\\ Encoder\\ JsonEncoder ` and
61
84
: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> ` .
64
87
65
88
You can load normalizers and/or encoders by tagging them as
66
89
:ref: `serializer.normalizer<reference-dic-tags-serializer-normalizer> ` and
@@ -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
+ 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