8000 [Serializer] Name Converter by dunglas · Pull Request #4692 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Name Converter #4692

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 6 commits into from
Mar 13, 2015
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Serializer] Use firtName and snake_case
  • Loading branch information
dunglas committed Dec 28, 2014
commit 24d0320301caa22eebe80b4130d05b33f33c67ae
23 changes: 12 additions & 11 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ A custom Name Converter can handle such cases::

public function denormalize($propertyName)
{
return substr($propertyName, 4);
// remove org_ prefix
return 'org_' === substr($propertyName, 0, 4) ? substr($propertyName, 4) : $propertyName;
}
}

Expand Down Expand Up @@ -246,32 +247,32 @@ Symfony provides a built-in Name Converter designed to translate between
snake_case and CamelCased styles during serialization and deserialization
processes::

use Symfony\Component\Serializer\NameConverter\CamelCaseToUnderscoreNameConverter;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;

$normalizer = new GetSetMethodNormalizer(null, new CamelCaseToUnderscoreNameConverter());
$normalizer = new GetSetMethodNormalizer(null, new CamelCaseToSnakeCaseNameConverter());

class Person
{
private $givenName;
private $firstName;

public function __construct($givenName)
public function __construct($firstName)
{
$this->givenName = $givenName;
$this->firstName = $firstName;
}

public function getGivenName()
public function getFirstName()
{
return $this->givenName;
return $this->firstName;
}
}

$kevin = new Person('Kévin');
$normalizer->normalize($kevin);
// ['given_name' => 'Kévin'];
// ['first_name' => 'Kévin'];

$anne = $normalizer->denormalize(array('given_name' => 'Anne'), 'Person');
// Person object with givenName: 'Anne'
$anne = $normalizer->denormalize(array('first_name' => 'Anne'), 'Person');
// Person object with firstName: 'Anne'

Serializing Boolean Attributes
------------------------------
Expand Down
0