Skip to content

Commit 4ecc706

Browse files
committed
[Serializer] Name Converter corrections.
1 parent b59b752 commit 4ecc706

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

components/serializer.rst

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,14 @@ Converting Property Names when Serializing and Deserializing
169169
The :class:`Symfony\\Component\\Serializer\\NameConverter\\NameConverterInterface`
170170
interface was introduced in Symfony 2.7.
171171

172-
Sometimes serialized attributes must be named differently than PHP class'
173-
properties or getter and setter methods.
172+
Sometimes serialized attributes must be named differently than properties
173+
or getter and setter methods of PHP classes.
174174

175175
The Serializer Component provides a handy way to translate or map PHP field
176-
names to serialized names: the Name Converter System.
176+
names to serialized names: The Name Converter System.
177177

178178
Given you have the following object::
179179

180-
namespace Acme;
181-
182180
class Company
183181
{
184182
public name;
@@ -193,8 +191,6 @@ the following::
193191

194192
A custom Name Converter can handle such cases::
195193

196-
namespace MySerializer;
197-
198194
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
199195

200196
class OrgPrefixNameConverter implements NameConverterInterface
@@ -206,7 +202,7 @@ A custom Name Converter can handle such cases::
206202

207203
public function denormalize($propertyName)
208204
{
209-
return substr($propertyName, 4)
205+
return substr($propertyName, 4);
210206
}
211207
}
212208

@@ -215,11 +211,9 @@ class extending :class:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNorm
215211
including :class:`Symfony\\Component\\Serializer\\Normalizer\\GetSetMethodNormalizer`
216212
and :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`::
217213

218-
use Acme\Company;
219214
use Symfony\Component\Serializer\Encoder\JsonEncoder
220215
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
221216
use Symfony\Component\Serializer\Serializer;
222-
use MySerializer\OrgPrefixNameConverter;
223217

224218
$nameConverter = new OrgPrefixNameConverter();
225219
$normalizer = new PropertyNormalizer(null, $nameConverter);
@@ -231,21 +225,25 @@ and :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`::
231225
$obj->address = 'Euratechnologies, 2 rue Hegel, 59160 Lomme, France';
232226

233227
$json = $serializer->serialize($obj);
228+
// {"org_name": "Les-Tilleuls.coop", "org_address": "Euratechnologies, 2 rue Hegel, 59160 Lomme, France"}
234229
$objCopy = $serializer->deserialize($json);
230+
// Same data as $obj
231+
232+
.. _using-camelized-method-names-for-underscored-attributes:
235233

236-
CamelCase to Underscore
234+
CamelCase to snake_case
237235
~~~~~~~~~~~~~~~~~~~~~~~
238236

239237
.. versionadded:: 2.7
240-
The :class:`Symfony\\Component\\Serializer\\NameConverter\\CamelCaseToUnderscoreNameConverter`
238+
The :class:`Symfony\\Component\\Serializer\\NameConverter\\CamelCaseToUnderscoreNameConverter`
241239
interface was introduced in Symfony 2.7.
242240

243-
It's common in many formats to use underscores to separate words. However,
244-
PSR-2 specify that the preferred style for PHP properties and methods is
245-
CamelCase.
241+
In many formats, it's common to use underscores to separate words (also know
242+
as snake_case). However, PSR-1 specifies that the preferred style for PHP
243+
properties and methods is CamelCase.
246244

247245
Symfony provides a built-in Name Converter designed to translate between
248-
underscored and CamelCased styles during serialization and deserialization
246+
snake_case and CamelCased styles during serialization and deserialization
249247
processes::
250248

251249
use Symfony\Component\Serializer\NameConverter\CamelCaseToUnderscoreNameConverter;
@@ -273,6 +271,7 @@ processes::
273271
// ['given_name' => 'Kévin'];
274272

275273
$anne = $normalizer->denormalize(array('given_name' => 'Anne'), 'Person');
274+
// Person object with givenName: 'Anne'
276275

277276
Serializing Boolean Attributes
278277
------------------------------

0 commit comments

Comments
 (0)