@@ -164,31 +164,29 @@ Improving Performance of Normalizers/Denormalizers
164
164
165
165
The ``getSupportedTypes() `` method was introduced in Symfony 6.3.
166
166
167
- Both :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ NormalizerInterface `
168
- and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ DenormalizerInterface `
169
- contain a new method ``getSupportedTypes() ``. This method allows normalizers or
170
- denormalizers to declare the type of objects they can handle, and whether they
171
- are cacheable. With this info, even if the ``supports*() `` call is not cacheable,
172
- the Serializer can skip a ton of method calls to ``supports*() `` improving
173
- performance substantially in some cases.
167
+ Both :class: Symfony\\ Component\\ Serializer\\ Normalizer\\ NormalizerInterface
168
+ and :class: Symfony\\ Component\\ Serializer\\ Normalizer\\ DenormalizerInterface
169
+ define a ``getSupportedTypes() `` method to declare which types they support and
170
+ whether their ``supports*() `` result can be cached.
171
+
172
+ This **does not ** cache the actual normalization or denormalization result. It
173
+ only **caches the decision ** of whether a normalizer supports a given type, allowing
174
+ the Serializer to skip unnecessary ``supports*() `` calls and improve performance.
174
175
175
176
The ``getSupportedTypes() `` method should return an array where the keys
176
- represent the supported types, and the values indicate whether the result of
177
- the ``supports*() `` method call can be cached or not. The format of the
178
- returned array is as follows:
177
+ represent the supported types, and the values indicate whether the result of the
178
+ corresponding ``supports*() `` call can be cached. The array format is as follows:
179
179
180
180
#. The special key ``object `` can be used to indicate that the normalizer or
181
181
denormalizer supports any classes or interfaces.
182
182
#. The special key ``* `` can be used to indicate that the normalizer or
183
- denormalizer might support any types.
184
- #. The other keys in the array should correspond to specific types that the
185
- normalizer or denormalizer supports.
186
- #. The values associated with each type should be a boolean indicating if the
187
- result of the ``supports*() `` method call for that type can be cached or not.
188
- A value of ``true `` means that the result is cacheable, while ``false `` means
189
- that the result is not cacheable.
190
- #. A ``null `` value for a type means that the normalizer or denormalizer does
191
- not support that type.
183
+ denormalizer might support any type.
184
+ #. Other keys should correspond to specific types that the normalizer or
185
+ denormalizer supports.
186
+ #. The values should be booleans indicating whether the result of the
187
+ ``supports*() `` call for that type is cacheable. Use ``true `` if the result
188
+ can be cached, ``false `` if it cannot.
189
+ #. A ``null `` value means the normalizer or denormalizer does not support that type.
192
190
193
191
Here is an example of how to use the ``getSupportedTypes() `` method::
194
192
@@ -201,9 +199,9 @@ Here is an example of how to use the ``getSupportedTypes()`` method::
201
199
public function getSupportedTypes(?string $format): array
202
200
{
203
201
return [
204
- 'object' => null, // Doesn 't support any classes or interfaces
205
- '*' => false, // Supports any other types, but the result is not cacheable
206
- MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
202
+ 'object' => null, // doesn 't support any classes or interfaces
203
+ '*' => false, // supports any other types, but the decision is not cacheable
204
+ MyCustomClass::class => true, // supports MyCustomClass and decision is cacheable
207
205
];
208
206
}
209
207
}
0 commit comments