Combine component and framework docs for Serializer

This commit is contained in:
Wouter de Jong
2023-01-21 16:42:58 +01:00
parent f644b5c3a0
commit 8a1497b1f2
15 changed files with 2455 additions and 2381 deletions

View File

@@ -525,7 +525,7 @@
/testing/functional_tests_assertions /testing#testing-application-assertions
/components https://symfony.com/components
/components/index https://symfony.com/components
/serializer/normalizers /components/serializer#normalizers
/serializer/normalizers /serializer#serializer-built-in-normalizers
/logging/monolog_regex_based_excludes /logging/monolog_exclude_http_codes
/security/named_encoders /security/named_hashers
/components/inflector /components/string#inflector
@@ -566,3 +566,5 @@
/messenger/dispatch_after_current_bus /messenger#messenger-transactional-messages
/messenger/multiple_buses /messenger#messenger-multiple-buses
/frontend/encore/server-data /frontend/server-data
/components/serializer /serializer
/serializer/custom_encoder /serializer/encoders#serializer-custom-encoder

View File

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View File

@@ -26,6 +26,8 @@ default configuration::
$propertyAccessor = PropertyAccess::createPropertyAccessor();
.. _property-access-reading-arrays:
Reading from Arrays
-------------------

View File

@@ -458,9 +458,9 @@ SerializerExtractor
This extractor depends on the `symfony/serializer`_ library.
Using :ref:`groups metadata <serializer-using-serialization-groups-attributes>`
from the :doc:`Serializer component </components/serializer>`,
the :class:`Symfony\\Component\\PropertyInfo\\Extractor\\SerializerExtractor`
Using :ref:`groups metadata <serializer-groups-attribute>` from the
:doc:`Serializer component </serializer>`, the
:class:`Symfony\\Component\\PropertyInfo\\Extractor\\SerializerExtractor`
provides list information. This extractor is *not* registered automatically
with the ``property_info`` service in the Symfony Framework::

File diff suppressed because it is too large Load Diff

View File

@@ -99,16 +99,18 @@ Security
* :ref:`CurrentUser <security-json-login>`
* :ref:`IsGranted <security-securing-controller-attributes>`
.. _reference-attributes-serializer:
Serializer
~~~~~~~~~~
* :ref:`Context <serializer_serializer-context>`
* :ref:`Context <serializer-context>`
* :ref:`DiscriminatorMap <serializer_interfaces-and-abstract-classes>`
* :ref:`Groups <component-serializer-attributes-groups-attributes>`
* :ref:`Groups <serializer-groups-attribute>`
* :ref:`Ignore <serializer_ignoring-attributes>`
* :ref:`MaxDepth <serializer_handling-serialization-depth>`
* :ref:`SerializedName <serializer_name-conversion>`
* :ref:`SerializedPath <serializer-enabling-metadata-cache>`
* :ref:`SerializedName <serializer-name-conversion>`
* :ref:`SerializedPath <serializer-nested-structures>`
Twig
~~~~

View File

@@ -2981,7 +2981,7 @@ enable_annotations
**type**: ``boolean`` **default**: ``true``
If this option is enabled, serialization groups can be defined using annotations or attributes.
Enables support for annotations or attributes in the serializer component.
.. deprecated:: 6.4
@@ -2993,11 +2993,11 @@ enable_attributes
**type**: ``boolean`` **default**: ``true``
If this option is enabled, serialization groups can be defined using `PHP attributes`_.
Enables support for `PHP attributes`_ in the serializer component.
.. seealso::
For more information, see :ref:`serializer-using-serialization-groups-attributes`.
See :ref:`the reference <reference-attributes-serializer>` for a list of supported annotations.
.. _reference-serializer-name_converter:
@@ -3013,8 +3013,7 @@ value.
.. seealso::
For more information, see
:ref:`component-serializer-converting-property-names-when-serializing-and-deserializing`.
For more information, see :ref:`serializer-name-conversion`.
.. _reference-serializer-circular_reference_handler:

View File

@@ -713,6 +713,8 @@ project's root directory:
If the given file path is out of the project directory, a ``null`` value
will be returned.
.. _reference-twig-filter-serialize:
serialize
~~~~~~~~~

File diff suppressed because it is too large Load Diff

View File

@@ -5,11 +5,9 @@ How to Create your Custom Context Builder
Context builders were introduced in Symfony 6.1.
The :doc:`Serializer Component </components/serializer>` uses Normalizers
and Encoders to transform any data to any data-structure (e.g. JSON).
That serialization process can be configured thanks to a
:ref:`serialization context <serializer_serializer-context>`, which can be built thanks to
:ref:`context builders <component-serializer-context-builders>`.
That serialization process of the :doc:`Serializer Component </serializer>`
can be configured by the :ref:`serialization context <serializer-context>`,
which can be built thanks to :ref:`context builders <serializer-using-context-builders>`.
Each built-in normalizer/encoder has its related context builder. However, you
may want to create a custom context builder for your

View File

@@ -1,61 +0,0 @@
How to Create your Custom Encoder
=================================
The :doc:`Serializer Component </components/serializer>` uses Normalizers
to transform any data to an array. Then, by leveraging *Encoders*, that data can
be converted into any data-structure (e.g. JSON).
The Component provides several built-in encoders that are described
:doc:`in the serializer component </components/serializer>` but you may want
to use another structure that's not supported.
Creating a new encoder
----------------------
Imagine you want to serialize and deserialize YAML. For that you'll have to
create your own encoder that uses the
:doc:`Yaml Component </components/yaml>`::
// src/Serializer/YamlEncoder.php
namespace App\Serializer;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Yaml\Yaml;
class YamlEncoder implements EncoderInterface, DecoderInterface
{
public function encode($data, string $format, array $context = []): string
{
return Yaml::dump($data);
}
public function supportsEncoding(string $format, array $context = []): bool
{
return 'yaml' === $format;
}
public function decode(string $data, string $format, array $context = []): array
{
return Yaml::parse($data);
}
public function supportsDecoding(string $format, array $context = []): bool
{
return 'yaml' === $format;
}
}
Registering it in your app
--------------------------
If you use the Symfony Framework, then you probably want to register this encoder
as a service in your app. If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
that's done automatically!
.. tip::
If you're not using :ref:`autoconfigure <services-autoconfigure>`, make sure
to register your class as a service and tag it with ``serializer.encoder``.
Now you'll be able to serialize and deserialize YAML!

View File

@@ -0,0 +1,105 @@
How to Create your Custom Name Converter
========================================
The Serializer Component uses :ref:`name converters <serializer-name-conversion>`
to transform the attribute names (e.g. from snake_case in JSON to CamelCase
for PHP properties).
Imagine you have the following object::
namespace App\Model;
class Company
{
public string $name;
public string $address;
}
And in the serialized form, all attributes must be prefixed by ``org_`` like
the following:
.. code-block:: json
{"org_name": "Acme Inc.", "org_address": "123 Main Street, Big City"}
A custom name converter can handle such cases::
namespace App\Serializer;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
class OrgPrefixNameConverter implements NameConverterInterface
{
public function normalize(string $propertyName): string
{
// during normalization, add the prefix
return 'org_'.$propertyName;
}
public function denormalize(string $propertyName): string
{
// remove the 'org_' prefix on denormalizing
return str_starts_with($propertyName, 'org_') ? substr($propertyName, 4) : $propertyName;
}
}
.. note::
You can also implement
:class:`Symfony\\Component\\Serializer\\NameConverter\\AdvancedNameConverterInterface`
to access the current class name, format and context.
Then, configure the serializer to use your name converter:
.. configuration-block::
.. code-block:: yaml
# config/packages/serializer.yaml
framework:
serializer:
# pass the service ID of your name converter
name_converter: 'App\Serializer\OrgPrefixNameConverter'
.. code-block:: xml
<!-- config/packages/serializer.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<!-- pass the service ID of your name converter -->
<framework:serializer
name-converter="App\Serializer\OrgPrefixNameConverter"
/>
</framework:config>
</container>
.. code-block:: php
// config/packages/serializer.php
use App\Serializer\OrgPrefixNameConverter;
use Symfony\Config\FrameworkConfig;
return static function (FrameworkConfig $framework) {
$framework->serializer()
// pass the service ID of your name converter
->nameConverter(OrgPrefixNameConverter::class)
;
};
Now, when using the serializer in the application, all attributes will be
prefixed by ``org_``::
// ...
$company = new Company('Acme Inc.', '123 Main Street, Big City');
$json = $serializer->serialize($company, 'json');
// {"org_name": "Acme Inc.", "org_address": "123 Main Street, Big City"}
$companyCopy = $serializer->deserialize($json, Company::class, 'json');
// Same data as $company

View File

@@ -1,10 +1,11 @@
How to Create your Custom Normalizer
====================================
The :doc:`Serializer component </components/serializer>` uses
normalizers to transform any data into an array. The component provides several
:ref:`built-in normalizers <component-serializer-normalizers>` but you may need to create
your own normalizer to transform an unsupported data structure.
The :doc:`Serializer component </serializer>` uses normalizers to transform
any data into an array. The component provides several
ref:`built-in normalizers <serializer-built-in-normalizers>` but you may
need to create your own normalizer to transform an unsupported data
structure.
Creating a New Normalizer
-------------------------
@@ -67,6 +68,63 @@ a service and :doc:`tagged </service_container/tags>` with ``serializer.normaliz
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
this is done automatically!
If you're not using ``autoconfigure``, you have to tag the service with
``serializer.normalizer``. You can also use this method to set a priority
(higher means it's called earlier in the process):
.. configuration-block::
.. code-block:: yaml
# config/services.yaml
services:
# ...
App\Serializer\TopicNormalizer:
tags:
# register the normalizer with a high priority (called earlier)
- { name: 'serializer.normalizer', priority: 500 }
.. code-block:: xml
<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<!-- ... -->
<service id="App\Serializer\TopicNormalizer">
<!-- register the normalizer with a high priority (called earlier) -->
<tag name="serializer.normalizer"
priority="500"
/>
</service>
</services>
</container>
.. code-block:: php
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use App\Serializer\TopicNormalizer;
return function(ContainerConfigurator $container) {
// ...
// if you're using autoconfigure, the tag will be automatically applied
$services->set(TopicNormalizer::class)
// register the normalizer with a high priority (called earlier)
->tag('serializer.normalizer', [
'priority' => 500,
])
;
};
Performance
-----------
@@ -90,7 +148,7 @@ is called.
.. note::
All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
All built-in :ref:`normalizers and denormalizers <serializer-built-in-normalizers>`
as well the ones included in `API Platform`_ natively implement this interface.
.. deprecated:: 6.3

371
serializer/encoders.rst Normal file
View File

@@ -0,0 +1,371 @@
Serializer Encoders
===================
The Serializer component provides several built-in encoders:
:class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder`
This class encodes and decodes data in `JSON`_.
:class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder`
This class encodes and decodes data in `XML`_.
:class:`Symfony\\Component\\Serializer\\Encoder\\YamlEncoder`
This encoder encodes and decodes data in `YAML`_. This encoder requires the
:doc:`Yaml Component </components/yaml>`.
:class:`Symfony\\Component\\Serializer\\Encoder\\CsvEncoder`
This encoder encodes and decodes data in `CSV`_.
.. note::
You can also create your own encoder to use another structure. Read more at
:ref:`Creating a Custom Encoder <serializer-custom-encoder>` below.
All these encoders are enabled by default when using the Serializer component
in a Symfony application.
The ``JsonEncoder``
-------------------
The ``JsonEncoder`` encodes to and decodes from JSON strings, based on the PHP
:phpfunction:`json_encode` and :phpfunction:`json_decode` functions.
It can be useful to modify how these functions operate in certain instances
by providing options such as ``JSON_PRESERVE_ZERO_FRACTION``. You can use
the serialization context to pass in these options using the key
``json_encode_options`` or ``json_decode_options`` respectively::
$this->serializer->serialize($data, 'json', [
'json_encode_options' => \JSON_PRESERVE_ZERO_FRACTION,
]);
All context options available for the JSON encoder are:
``json_decode_associative`` (default: ``false``)
If set to ``true`` returns the result as an array, returns a nested ``stdClass`` hierarchy otherwise.
``json_decode_detailed_errors`` (default: ``false``)
If set to ``true`` exceptions thrown on parsing of JSON are more specific. Requires `seld/jsonlint`_ package.
.. versionadded:: 6.4
The ``json_decode_detailed_errors`` option was introduced in Symfony 6.4.
``json_decode_options`` (default: ``0``)
Flags passed to :phpfunction:`json_decode` function.
``json_encode_options`` (default: ``\JSON_PRESERVE_ZERO_FRACTION``)
Flags passed to :phpfunction:`json_encode` function.
``json_decode_recursion_depth`` (default: ``512``)
Sets maximum recursion depth.
The ``CsvEncoder``
------------------
The ``CsvEncoder`` encodes to and decodes from CSV. Serveral :ref:`context options <serializer-context>`
are available to customize the behavior of the encoder:
``csv_delimiter`` (default: ``,``)
Sets the field delimiter separating values (one character only).
``csv_enclosure`` (default: ``"``)
Sets the field enclosure (one character only).
``csv_end_of_line`` (default: ``\n``)
Sets the character(s) used to mark the end of each line in the CSV file.
``csv_escape_char`` (default: empty string)
Sets the escape character (at most one character).
``csv_key_separator`` (default: ``.``)
Sets the separator for array's keys during its flattening
``csv_headers`` (default: ``[]``, inferred from input data's keys)
Sets the order of the header and data columns.
E.g. if you set it to ``['a', 'b', 'c']`` and serialize
``['c' => 3, 'a' => 1, 'b' => 2]``, the order will be ``a,b,c`` instead
of the input order (``c,a,b``).
``csv_escape_formulas`` (default: ``false``)
Escapes fields containing formulas by prepending them with a ``\t`` character.
``as_collection`` (default: ``true``)
Always returns results as a collection, even if only one line is decoded.
``no_headers`` (default: ``false``)
Setting to ``false`` will use first row as headers when denormalizing,
``true`` generates numeric headers.
``output_utf8_bom`` (default: ``false``)
Outputs special `UTF-8 BOM`_ along with encoded data.
The ``XmlEncoder``
------------------
This encoder transforms PHP values into XML and vice versa.
For example, take an object that is normalized as following::
$normalizedArray = ['foo' => [1, 2], 'bar' => true];
The ``XmlEncoder`` will encode this object like:
.. code-block:: xml
<?xml version="1.0" encoding="UTF-8" ?>
<response>
<foo>1</foo>
<foo>2</foo>
<bar>1</bar>
</response>
The special ``#`` key can be used to define the data of a node::
['foo' => ['@bar' => 'value', '#' => 'baz']];
/* is encoded as follows:
<?xml version="1.0"?>
<response>
<foo bar="value">
baz
</foo>
</response>
*/
Furthermore, keys beginning with ``@`` will be considered attributes, and
the key ``#comment`` can be used for encoding XML comments::
$encoder = new XmlEncoder();
$xml = $encoder->encode([
'foo' => ['@bar' => 'value'],
'qux' => ['#comment' => 'A comment'],
], 'xml');
/* will return:
<?xml version="1.0"?>
<response>
<foo bar="value"/>
<qux><!-- A comment --!><qux>
</response>
*/
You can pass the context key ``as_collection`` in order to have the results
always as a collection.
.. note::
You may need to add some attributes on the root node::
$encoder = new XmlEncoder();
$encoder->encode([
'@attribute1' => 'foo',
'@attribute2' => 'bar',
'#' => ['foo' => ['@bar' => 'value', '#' => 'baz']]
], 'xml');
// will return:
// <?xml version="1.0"?>
// <response attribute1="foo" attribute2="bar">
// <foo bar="value">baz</foo>
// </response>
.. tip::
XML comments are ignored by default when decoding contents, but this
behavior can be changed with the optional context key ``XmlEncoder::DECODER_IGNORED_NODE_TYPES``.
Data with ``#comment`` keys are encoded to XML comments by default. This can be
changed by adding the ``\XML_COMMENT_NODE`` option to the ``XmlEncoder::ENCODER_IGNORED_NODE_TYPES``
key of the ``$defaultContext`` of the ``XmlEncoder`` constructor or
directly to the ``$context`` argument of the ``encode()`` method::
$xmlEncoder->encode($array, 'xml', [XmlEncoder::ENCODER_IGNORED_NODE_TYPES => [\XML_COMMENT_NODE]]);
The ``XmlEncoder`` Context Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
These are the options available on the :ref:`serializer context <serializer-context>`:
``xml_format_output`` (default: ``false``)
If set to true, formats the generated XML with line breaks and indentation.
``xml_version`` (default: ``1.0``)
Sets the XML version attribute.
``xml_encoding`` (default: ``utf-8``)
Sets the XML encoding attribute.
``xml_standalone`` (default: ``true``)
Adds standalone attribute in the generated XML.
``xml_type_cast_attributes`` (default: ``true``)
This provides the ability to forget the attribute type casting.
``xml_root_node_name`` (default: ``response``)
Sets the root node name.
``as_collection`` (default: ``false``)
Always returns results as a collection, even if only one line is decoded.
``decoder_ignored_node_types`` (default: ``[\XML_PI_NODE, \XML_COMMENT_NODE]``)
Array of node types (`DOM XML_* constants`_) to be ignored while decoding.
``encoder_ignored_node_types`` (default: ``[]``)
Array of node types (`DOM XML_* constants`_) to be ignored while encoding.
``load_options`` (default: ``\LIBXML_NONET | \LIBXML_NOBLANKS``)
XML loading `options with libxml`_.
``save_options`` (default: ``0``)
XML saving `options with libxml`_.
.. versionadded:: 6.3
The ``save_options`` option was introduced in Symfony 6.3.
``remove_empty_tags`` (default: ``false``)
If set to ``true``, removes all empty tags in the generated XML.
``cdata_wrapping`` (default: ``true``)
If set to ``false``, will not wrap any value containing one of the
following characters ( ``<``, ``>``, ``&``) in `a CDATA section`_ like
following: ``<![CDATA[...]]>``.
.. versionadded:: 6.4
The ``cdata_wrapping`` option was introduced in Symfony 6.4.
Example with a custom ``context``::
use Symfony\Component\Serializer\Encoder\XmlEncoder;
$data = [
'id' => 'IDHNQIItNyQ',
'date' => '2019-10-24',
];
$xmlEncoder->encode($data, 'xml', ['xml_format_output' => true]);
// outputs:
// <?xml version="1.0"?>
// <response>
// <id>IDHNQIItNyQ</id>
// <date>2019-10-24</date>
// </response>
$xmlEncoder->encode($data, 'xml', [
'xml_format_output' => true,
'xml_root_node_name' => 'track',
'encoder_ignored_node_types' => [
\XML_PI_NODE, // removes XML declaration (the leading xml tag)
],
]);
// outputs:
// <track>
// <id>IDHNQIItNyQ</id>
// <date>2019-10-24</date>
// </track>
The ``YamlEncoder``
-------------------
This encoder requires the :doc:`Yaml Component </components/yaml>` and
transforms from and to Yaml.
Like other encoder, several :ref:`context options <serializer-context>` are
available:
``yaml_inline`` (default: ``0``)
The level where you switch to inline YAML.
``yaml_indent`` (default: ``0``)
The level of indentation (used internally).
``yaml_flags`` (default: ``0``)
A bit field of ``Yaml::DUMP_*``/``Yaml::PARSE_*`` constants to
customize the encoding/decoding YAML string.
.. _serializer-custom-encoder:
Creating a Custom Encoder
-------------------------
Imagine you want to serialize and deserialize `NEON`_. For that you'll have to
create your own encoder::
// src/Serializer/YamlEncoder.php
namespace App\Serializer;
use Nette\Neon\Neon;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
class NeonEncoder implements EncoderInterface, DecoderInterface
{
public function encode($data, string $format, array $context = [])
{
return Neon::encode($data);
}
public function supportsEncoding(string $format)
{
return 'neon' === $format;
}
public function decode(string $data, string $format, array $context = [])
{
return Neon::decode($data);
}
public function supportsDecoding(string $format)
{
return 'neon' === $format;
}
}
.. tip::
If you need access to ``$context`` in your ``supportsDecoding`` or
``supportsEncoding`` method, make sure to implement
``Symfony\Component\Serializer\Encoder\ContextAwareDecoderInterface``
or ``Symfony\Component\Serializer\Encoder\ContextAwareEncoderInterface`` accordingly.
Registering it in Your App
~~~~~~~~~~~~~~~~~~~~~~~~~~
If you use the Symfony Framework, then you probably want to register this encoder
as a service in your app. If you're using the
:ref:`default services.yaml configuration <service-container-services-load-example>`,
that's done automatically!
If you're not using :ref:`autoconfigure <services-autoconfigure>`, make sure
to register your class as a service and tag it with ``serializer.encoder``:
.. configuration-block::
.. code-block:: yaml
# config/services.yaml
services:
# ...
App\Serializer\NeonEncoder:
tags: ['serializer.encoder']
.. code-block:: xml
<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<!-- ... -->
<service id="App\Serializer\NeonEncoder">
<tag name="serializer.encoder"/>
</service>
</services>
</container>
.. code-block:: php
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use App\Serializer\NeonEncoder;
return function(ContainerConfigurator $container) {
// ...
$services->set(NeonEncoder::class)
->tag('serializer.encoder')
;
};
Now you'll be able to serialize and deserialize NEON!
.. _JSON: https://www.json.org/json-en.html
.. _XML: https://www.w3.org/XML/
.. _YAML: https://yaml.org/
.. _CSV: https://tools.ietf.org/html/rfc4180
.. _seld/jsonlint: https://github.com/Seldaek/jsonlint
.. _`UTF-8 BOM`: https://en.wikipedia.org/wiki/Byte_order_mark
.. _`DOM XML_* constants`: https://www.php.net/manual/en/dom.constants.php
.. _`options with libxml`: https://www.php.net/manual/en/libxml.constants.php
.. _`a CDATA section`: https://en.wikipedia.org/wiki/CDATA
.. _NEON: https://ne-on.org/