Skip to content
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

[Serializer] Add SnakeCaseToCamelCaseNameConverter #20160

Open
wants to merge 1 commit into
base: 7.2
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,40 @@ processes::
$anne = $normalizer->denormalize(['first_name' => 'Anne'], 'Person');
// Person object with firstName: 'Anne'

.. _using-underscored-method-names-for-camelized-attributes:

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

Symfony provides a built-in name converter designed to transform between
snake_case and CamelCased styles during serialization and deserialization
processes::

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

$normalizer = new ObjectNormalizer(null, new SnakeCaseToCamelCaseNameConverter());

class Person
{
public function __construct(
private string $fullName,
) {
}

public function getFullName(): string
{
return $this->fullName;
}
}

$john = new Person('john_doe');
$normalizer->normalize($john);
// ['full_name' => 'johnDoe'];

$john = $normalizer->denormalize(['full_name' => 'johnDoe'], 'Person');
// Person object with fullName: 'jonh_doe'
Comment on lines +645 to +663
Copy link
Contributor

@OskarStark OskarStark Sep 3, 2024

Choose a reason for hiding this comment

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

Suggested change
class Person
{
public function __construct(
private string $fullName,
) {
}
public function getFullName(): string
{
return $this->fullName;
}
}
$john = new Person('john_doe');
$normalizer->normalize($john);
// ['full_name' => 'johnDoe'];
$john = $normalizer->denormalize(['full_name' => 'johnDoe'], 'Person');
// Person object with fullName: 'jonh_doe'
class Person
{
public function __construct(
private string $full_name,
) {
}
public function getFullName(): string
{
return $this->full_name;
}
}
$john = new Person('john');
$normalizer->normalize($john);
// ['fullName' => 'john'];
$john = $normalizer->denormalize(['fullName' => 'john'], 'Person');
// $john->getFullName() -> 'john'

cc @xabbuh

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if it works this way when the property is not public and the getter has a camel case name.


.. _serializer_name-conversion:

Configure name conversion using metadata
Expand Down
Loading