@@ -1544,13 +1544,13 @@ Named Serializers
1544
1544
1545
1545
Named serializers were introduced in Symfony 7.2.
1546
1546
1547
- Sometimes, you may need multiple configurations for the serializer, such
1548
- as different default contexts, name converters, or sets of normalizers and
1549
- encoders, depending on the use case. For example, when your application
1550
- communicates with multiple APIs, each with its own set of rules.
1547
+ Sometimes, you may need multiple configurations for the serializer, such as
1548
+ different default contexts, name converters, or sets of normalizers and encoders,
1549
+ depending on the use case. For example, when your application communicates with
1550
+ multiple APIs, each of which follows its own set of serialization rules.
1551
1551
1552
- This can be achieved by configuring multiple instances of the serializer
1553
- using the ``named_serializers `` option:
1552
+ You can achieve this by configuring multiple serializer instances using
1553
+ the ``named_serializers `` option:
1554
1554
1555
1555
.. configuration-block ::
1556
1556
@@ -1633,7 +1633,7 @@ using :ref:`named aliases <autowiring-multiple-implementations-same-type>`::
1633
1633
class PersonController extends AbstractController
1634
1634
{
1635
1635
public function index(
1636
- SerializerInterface $serializer, // Default serializer
1636
+ SerializerInterface $serializer, // default serializer
1637
1637
SerializerInterface $apiClient1Serializer, // api_client1 serializer
1638
1638
#[Target('apiClient2.serializer')] // api_client2 serializer
1639
1639
SerializerInterface $customName,
@@ -1642,10 +1642,10 @@ using :ref:`named aliases <autowiring-multiple-implementations-same-type>`::
1642
1642
}
1643
1643
}
1644
1644
1645
- Named serializers are configured with the default set of normalizers and encoders.
1646
-
1647
- You can register additional normalizers and encoders with a specific named
1648
- serializer by adding a ``serializer `` attribute to
1645
+ By default, named serializers use the built-in set of normalizers and encoders,
1646
+ just like the main serializer service. However, you can customize them by
1647
+ registering additional normalizers or encoders for a specific named serializer.
1648
+ To do that, add a ``serializer `` attribute to
1649
1649
the :ref: `serializer.normalizer <reference-dic-tags-serializer-normalizer >`
1650
1650
or :ref: `serializer.encoder <reference-dic-tags-serializer-encoder >` tags:
1651
1651
@@ -1658,14 +1658,14 @@ or :ref:`serializer.encoder <reference-dic-tags-serializer-encoder>` tags:
1658
1658
# ...
1659
1659
1660
1660
Symfony\Component\Serializer\Normalizer\CustomNormalizer :
1661
- # Prevent this normalizer from automatically being included in the default serializer
1661
+ # prevent this normalizer from being automatically added to the default serializer
1662
1662
autoconfigure : false
1663
1663
tags :
1664
- # Include this normalizer in a single serializer
1664
+ # add this normalizer only to a specific named serializer
1665
1665
- serializer.normalizer : { serializer: 'api_client1' }
1666
- # Include this normalizer in multiple serializers
1666
+ # add this normalizer to several named serializers
1667
1667
- serializer.normalizer : { serializer: [ 'api_client1', 'api_client2' ] }
1668
- # Include this normalizer in all serializers ( including the default one)
1668
+ # add this normalizer to all serializers, including the default one
1669
1669
- serializer.normalizer : { serializer: '*' }
1670
1670
1671
1671
.. code-block :: xml
@@ -1680,20 +1680,19 @@ or :ref:`serializer.encoder <reference-dic-tags-serializer-encoder>` tags:
1680
1680
<services >
1681
1681
<!-- ... -->
1682
1682
1683
- <!-- Disable autoconfigure to prevent this normalizer from automatically -->
1684
- <!-- being included in the default serializer -->
1683
+ <!-- prevent this normalizer from being automatically added to the default serializer -->
1685
1684
<service
1686
1685
id =" Symfony\Component\Serializer\Normalizer\CustomNormalizer"
1687
1686
autoconfigure =" false"
1688
1687
>
1689
- <!-- Include this normalizer in a single serializer -->
1688
+ <!-- add this normalizer only to a specific named serializer -->
1690
1689
<tag name =" serializer.normalizer" serializer =" api_client1" />
1691
1690
1692
- <!-- Include this normalizer in multiple serializers -->
1691
+ <!-- add this normalizer to several named serializers -->
1693
1692
<tag name =" serializer.normalizer" serializer =" api_client1" />
1694
1693
<tag name =" serializer.normalizer" serializer =" api_client2" />
1695
1694
1696
- <!-- Include this normalizer in all serializers ( including the default one) -->
1695
+ <!-- add this normalizer to all serializers, including the default one -->
1697
1696
<tag name =" serializer.normalizer" serializer =" *" />
1698
1697
</service >
1699
1698
</services >
@@ -1710,32 +1709,32 @@ or :ref:`serializer.encoder <reference-dic-tags-serializer-encoder>` tags:
1710
1709
// ...
1711
1710
1712
1711
$services->set(CustomNormalizer::class)
1713
- // Prevent this normalizer from automatically being included in the default serializer
1712
+ // prevent this normalizer from being automatically added to the default serializer
1714
1713
->autoconfigure(false)
1715
1714
1716
- // Include this normalizer in a single serializer
1715
+ // add this normalizer only to a specific named serializer
1717
1716
->tag('serializer.normalizer', ['serializer' => 'api_client1'])
1718
- // Include this normalizer in multiple serializers
1717
+ // add this normalizer to several named serializers
1719
1718
->tag('serializer.normalizer', ['serializer' => ['api_client1', 'api_client2']])
1720
- // Include this normalizer in all serializers ( including the default one)
1719
+ // add this normalizer to all serializers, including the default one
1721
1720
->tag('serializer.normalizer', ['serializer' => '*'])
1722
1721
;
1723
1722
};
1724
1723
1725
- When the ``serializer `` attribute is not set, the service is registered with
1724
+ When the ``serializer `` attribute is not set, the service is registered only with
1726
1725
the default serializer.
1727
1726
1728
- Each normalizer and encoder used in a named serializer is tagged with
1729
- a ``serializer.normalizer.<name> `` or ``serializer.encoder.<name> `` tag,
1730
- which can be used to list their priorities using the following command:
1727
+ Each normalizer or encoder used in a named serializer is tagged with a
1728
+ ``serializer.normalizer.<name> `` or ``serializer.encoder.<name> `` tag.
1729
+ You can inspect their priorities using the following command:
1731
1730
1732
1731
.. code-block :: terminal
1733
1732
1734
1733
$ php bin/console debug:container --tag serializer.<normalizer|encoder>.<name>
1735
1734
1736
- Additionally, you can exclude the default set of normalizers and encoders by
1737
- setting the ``include_built_in_normalizers `` and `` include_built_in_encoders ``
1738
- options to ``false ``:
1735
+ Additionally, you can exclude the default set of normalizers and encoders from a
1736
+ named serializer by setting the ``include_built_in_normalizers `` and
1737
+ `` include_built_in_encoders `` options to ``false ``:
1739
1738
1740
1739
.. configuration-block ::
1741
1740
0 commit comments