Skip to content

[Serializer] Add SnakeCaseToCamelCaseNameConverter #20774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,67 @@ setting the ``name_converter`` setting to
];
$serializer = new Serializer($normalizers, $encoders);

snake_case to CamelCase
~~~~~~~~~~~~~~~~~~~~~~~

In Symfony applications is common to use camelCase to name properties. However
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In Symfony applications is common to use camelCase to name properties. However
In Symfony applications it is common to use camelCase to name properties. However

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done while merging ... and added the versionadded directive too. Thanks!

some packages can use snake_case as convention.

Symfony provides a built-in name converter designed to transform between
CamelCase and snake_cased styles during serialization and deserialization
processes. You can use it instead of the metadata aware name converter by
setting the ``name_converter`` setting to
``serializer.name_converter.snake_case_to_camel_case``:

.. configuration-block::

.. code-block:: yaml

# config/packages/serializer.yaml
framework:
serializer:
name_converter: 'serializer.name_converter.snake_case_to_camel_case'

.. 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>
<framework:serializer
name-converter="serializer.name_converter.snake_case_to_camel_case"
/>
</framework:config>
</container>

.. code-block:: php

// config/packages/serializer.php
use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework): void {
$framework->serializer()
->nameConverter('serializer.name_converter.snake_case_to_camel_case')
;
};

.. code-block:: php-standalone

use Symfony\Component\Serializer\NameConverter\SnakeCaseToCamelCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

// ...
$normalizers = [
new ObjectNormalizer(null, new SnakeCaseToCamelCaseNameConverter()),
];
$serializer = new Serializer($normalizers, $encoders);

.. _serializer-built-in-normalizers:

Serializer Normalizers
Expand Down