Skip to content

Commit 9596db6

Browse files
committed
Tweaks
1 parent 4f9d9f2 commit 9596db6

File tree

1 file changed

+54
-47
lines changed

1 file changed

+54
-47
lines changed

serializer/custom_normalizer.rst

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,52 +48,6 @@ to customize the normalized data. To do that, leverage the ``ObjectNormalizer``:
4848
}
4949
}
5050

51-
Caching the result of supportsNormalization
52-
-------------------------------------------
53-
54-
.. versionadded:: 6.3
55-
56-
The ``getSupportedTypes()`` method was introduced in Symfony 6.3.
57-
58-
Both :class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface` and
59-
:class:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface` contain a new method ``getSupportedTypes()``.
60-
This method allows normalizers or denormalizers to declare the type of objects they can handle, and whether they are
61-
cacheable. With this info, even if the ``supports*()`` call is not cacheable, the Serializer can skip a ton of method calls
62-
to ``supports*()`` improving performance substantially in some cases.
63-
64-
The ``getSupportedTypes()`` method should return an array where the keys represent the supported types, and the values
65-
indicate whether the result of the ``supports*()`` method call can be cached or not. The format of the returned array is as
66-
follows:
67-
68-
- The special key ``object`` can be used to indicate that the normalizer or denormalizer supports any classes or
69-
interfaces.
70-
- The special key ``*`` can be used to indicate that the normalizer or denormalizer might support any types.
71-
- The other keys in the array should correspond to specific types that the normalizer or denormalizer supports.
72-
- The values associated with each type should be a boolean indicating if the result of the ``supports*()`` method call for
73-
that type can be cached or not. A value of ``true`` means that the result is cacheable, while ``false`` means that the
74-
result is not cacheable.
75-
- A ``null`` value for a type means that the normalizer or denormalizer does not support that type.
76-
77-
Here is an example of how to use the ``getSupportedTypes()`` method::
78-
79-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
80-
81-
class MyNormalizer implements NormalizerInterface
82-
{
83-
// ...
84-
85-
public function getSupportedTypes(?string $format): array
86-
{
87-
return [
88-
'object' => null, // Doesn't supports any classes or interfaces
89-
'*' => false, // Supports any other types, but the result is not cacheable
90-
MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
91-
];
92-
}
93-
}
94-
95-
Note that ``supports*()`` method implementations should not assume that ``getSupportedTypes()`` has been called before.
96-
9751
Registering it in your Application
9852
----------------------------------
9953

@@ -128,5 +82,58 @@ is called.
12882
All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
12983
as well the ones included in `API Platform`_ natively implement this interface.
13084

131-
.. _`API Platform`: https://api-platform.com
85+
Improving Performance of Normalizers/Denormalizers
86+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13287

88+
.. versionadded:: 6.3
89+
90+
The ``getSupportedTypes()`` method was introduced in Symfony 6.3.
91+
92+
Both :class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface`
93+
and :class:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface`
94+
contain a new method ``getSupportedTypes()``. This method allows normalizers or
95+
denormalizers to declare the type of objects they can handle, and whether they
96+
are cacheable. With this info, even if the ``supports*()`` call is not cacheable,
97+
the Serializer can skip a ton of method calls to ``supports*()`` improving
98+
performance substantially in some cases.
99+
100+
The ``getSupportedTypes()`` method should return an array where the keys
101+
represent the supported types, and the values indicate whether the result of
102+
the ``supports*()`` method call can be cached or not. The format of the
103+
returned array is as follows:
104+
105+
#. The special key ``object`` can be used to indicate that the normalizer or
106+
denormalizer supports any classes or interfaces.
107+
#. The special key ``*`` can be used to indicate that the normalizer or
108+
denormalizer might support any types.
109+
#. The other keys in the array should correspond to specific types that the
110+
normalizer or denormalizer supports.
111+
#. The values associated with each type should be a boolean indicating if the
112+
result of the ``supports*()`` method call for that type can be cached or not.
113+
A value of ``true`` means that the result is cacheable, while ``false`` means
114+
that the result is not cacheable.
115+
#. A ``null`` value for a type means that the normalizer or denormalizer does
116+
not support that type.
117+
118+
Here is an example of how to use the ``getSupportedTypes()`` method::
119+
120+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
121+
122+
class MyNormalizer implements NormalizerInterface
123+
{
124+
// ...
125+
126+
public function getSupportedTypes(?string $format): array
127+
{
128+
return [
129+
'object' => null, // Doesn't support any classes or interfaces
130+
'*' => false, // Supports any other types, but the result is not cacheable
131+
MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
132+
];
133+
}
134+
}
135+
136+
Note that ``supports*()`` method implementations should not assume that
137+
``getSupportedTypes()`` has been called before.
138+
139+
.. _`API Platform`: https://api-platform.com

0 commit comments

Comments
 (0)