Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
b59b7524ecc70624d032057e6c940442752d335005File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -162,38 +162,116 @@ 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 | ||
| Converting PropertyNameswhen Serializing and Deserializing | ||
Member 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. could you please add an anchor above this line, to make sure it's BC? (oh dear, doc starts to worry about BC too :(...
MemberAuthor 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've added the anchor when speaking about snake_case and CamelCase. | ||
| ------------------------------------------------------------ | ||
| .. versionadded:: 2.7 | ||
| The :class:`Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface` | ||
| interface was introduced in Symfony 2.7. | ||
| Sometimes serialized attributes must be named differently than properties | ||
| or getter/setter methods of PHP classes. | ||
| The Serializer Component provides a handy way to translate or map PHP field | ||
| names to serialized names: The Name Converter System. | ||
| Given you have the following object:: | ||
| class Company | ||
| { | ||
| public name; | ||
| public address; | ||
| } | ||
| 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:: | ||
| 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; | ||
| } | ||
| } | ||
| The custom normalizer can be used by passing it as second parameter of any | ||
| class extending :class:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNormalizer`, | ||
Member 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. MemberAuthor 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); | ||
Member 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 | ||
| { | ||
| private $firstName; | ||
| public function __construct($firstName) | ||
| { | ||
| $this->firstName = $firstName; | ||
| } | ||
| public function getFirstName() | ||
| { | ||
| return $this->firstName; | ||
| } | ||
| } | ||
| $kevin = new Person('Kévin'); | ||
| $normalizer->normalize($kevin); | ||
| // ['first_name' => 'Kévin']; | ||
| $anne = $normalizer->denormalize(array('first_name' => 'Anne'), 'Person'); | ||
| // Person object with firstName: 'Anne' | ||
| Serializing Boolean Attributes | ||
| ------------------------------ | ||