-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
Changes from 5 commits
b59b752
4ecc706
24d0320
57e6c94
0442752
d335005
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,38 +162,117 @@ needs three parameters: | |
2. The name of the class this information will be decoded to | ||
3. The encoder used to convert that information into an array | ||
|
||
Using Camelized Method Names for Underscored Attributes | ||
------------------------------------------------------- | ||
Converting Property Names when Serializing and Deserializing | ||
------------------------------------------------------------ | ||
|
||
.. versionadded:: 2.3 | ||
The :method:`GetSetMethodNormalizer::setCamelizedAttributes<Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes>` | ||
method was introduced in Symfony 2.3. | ||
.. versionadded:: 2.7 | ||
The :class:`Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface` | ||
interface was introduced in Symfony 2.7. | ||
|
||
Sometimes property names from the serialized content are underscored (e.g. | ||
``first_name``). Normally, these attributes will use get/set methods like | ||
``getFirst_name``, when ``getFirstName`` method is what you really want. To | ||
change that behavior use the | ||
:method:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer::setCamelizedAttributes` | ||
method on the normalizer definition:: | ||
Sometimes serialized attributes must be named differently than properties | ||
or getter / setter methods of PHP classes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the spaces around the |
||
|
||
$encoder = new JsonEncoder(); | ||
$normalizer = new GetSetMethodNormalizer(); | ||
$normalizer->setCamelizedAttributes(array('first_name')); | ||
The Serializer Component provides a handy way to translate or map PHP field | ||
names to serialized names: The Name Converter System. | ||
|
||
$serializer = new Serializer(array($normalizer), array($encoder)); | ||
Given you have the following object:: | ||
|
||
class Company | ||
{ | ||
public name; | ||
public address; | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove one blank line. |
||
And in the serialized form, all attributes must be prefixed by ``org_`` like | ||
the following:: | ||
|
||
{"org_name": "Acme Inc.", "org_address": "123 Main Street, Big City"} | ||
|
||
A custom Name Converter can handle such cases:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name converter (lowercased) |
||
|
||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
|
||
class OrgPrefixNameConverter implements NameConverterInterface | ||
{ | ||
public function normalize($propertyName) | ||
{ | ||
return 'org_'.$propertyName; | ||
} | ||
|
||
public function denormalize($propertyName) | ||
{ | ||
// remove org_ prefix | ||
return 'org_' === substr($propertyName, 0, 4) ? substr($propertyName, 4) : $propertyName; | ||
} | ||
} | ||
|
||
$json = <<<EOT | ||
The custom normalizer can be used by passing it as second parameter of any | ||
class extending :class:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I never like using "code names" much in docs. I think just using human language makes things easier to read. If "[...] by passing it as second parameter of any normalizer, including [...]" is correct, I'm in favor of using that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, the name converter system will only work if the custom serializer extends the |
||
including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer` | ||
and :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`:: | ||
|
||
use Symfony\Component\Serializer\Encoder\JsonEncoder | ||
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
$nameConverter = new OrgPrefixNameConverter(); | ||
$normalizer = new PropertyNormalizer(null, $nameConverter); | ||
|
||
$serializer = new Serializer(array(new JsonEncoder()), array($normalizer)); | ||
|
||
$obj = new Company(); | ||
$obj->name = 'Acme Inc.'; | ||
$obj->address = '123 Main Street, Big City'; | ||
|
||
$json = $serializer->serialize($obj); | ||
// {"org_name": "Acme Inc.", "org_address": "123 Main Street, Big City"} | ||
$objCopy = $serializer->deserialize($json); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add some comments showing the result. |
||
// Same data as $obj | ||
|
||
.. _using-camelized-method-names-for-underscored-attributes: | ||
|
||
CamelCase to snake_case | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. versionadded:: 2.7 | ||
The :class:`Symfony\\Component\\Serializer\\NameConverter\\CamelCaseToUnderscoreNameConverter` | ||
interface was introduced in Symfony 2.7. | ||
|
||
In many formats, it's common to use underscores to separate words (also known | ||
as snake_case). However, PSR-1 specifies that the preferred style for PHP | ||
properties and methods is 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\CamelCaseToSnakeCaseNameConverter; | ||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; | ||
|
||
$normalizer = new GetSetMethodNormalizer(null, new CamelCaseToSnakeCaseNameConverter()); | ||
|
||
class Person | ||
{ | ||
"name": "foo", | ||
"age": "19", | ||
"first_name": "bar" | ||
private $firstName; | ||
|
||
public function __construct($firstName) | ||
{ | ||
$this->firstName = $firstName; | ||
} | ||
|
||
public function getFirstName() | ||
{ | ||
return $this->firstName; | ||
} | ||
} | ||
EOT; | ||
|
||
$person = $serializer->deserialize($json, 'Acme\Person', 'json'); | ||
$kevin = new Person('Kévin'); | ||
$normalizer->normalize($kevin); | ||
// ['first_name' => 'Kévin']; | ||
|
||
As a final result, the deserializer uses the ``first_name`` attribute as if | ||
it were ``firstName`` and uses the ``getFirstName`` and ``setFirstName`` methods. | ||
$anne = $normalizer->denormalize(array('first_name' => 'Anne'), 'Person'); | ||
// Person object with firstName: 'Anne' | ||
|
||
Serializing Boolean Attributes | ||
------------------------------ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please add an anchor above this line, to make sure it's BC? (oh dear, doc starts to worry about BC too :(...
.. _using-camelized-method-names-for-underscored-attributes:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the anchor when speaking about snake_case and CamelCase.