Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaut22200 committed Aug 27, 2024
1 parent dae3dfe commit 1a9afff
Showing 1 changed file with 34 additions and 0 deletions.
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'

.. _serializer_name-conversion:

Configure name conversion using metadata
Expand Down

0 comments on commit 1a9afff

Please sign in to comment.