@@ -169,16 +169,14 @@ Converting Property Names when Serializing and Deserializing
169
169
The :class: `Symfony\\ Component\\ Serializer\\ NameConverter\\ NameConverterInterface `
170
170
interface was introduced in Symfony 2.7.
171
171
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 .
174
174
175
175
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.
177
177
178
178
Given you have the following object::
179
179
180
- namespace Acme;
181
-
182
180
class Company
183
181
{
184
182
public name;
@@ -193,8 +191,6 @@ the following::
193
191
194
192
A custom Name Converter can handle such cases::
195
193
196
- namespace MySerializer;
197
-
198
194
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
199
195
200
196
class OrgPrefixNameConverter implements NameConverterInterface
@@ -206,7 +202,7 @@ A custom Name Converter can handle such cases::
206
202
207
203
public function denormalize($propertyName)
208
204
{
209
- return substr($propertyName, 4)
205
+ return substr($propertyName, 4);
210
206
}
211
207
}
212
208
@@ -215,11 +211,9 @@ class extending :class:`Symfony\\Component\\Serializer\\Normalizer\\AbstractNorm
215
211
including :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ GetSetMethodNormalizer `
216
212
and :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ PropertyNormalizer `::
217
213
218
- use Acme\Company;
219
214
use Symfony\Component\Serializer\Encoder\JsonEncoder
220
215
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
221
216
use Symfony\Component\Serializer\Serializer;
222
- use MySerializer\OrgPrefixNameConverter;
223
217
224
218
$nameConverter = new OrgPrefixNameConverter();
225
219
$normalizer = new PropertyNormalizer(null, $nameConverter);
@@ -231,21 +225,25 @@ and :class:`Symfony\\Component\\Serializer\\Normalizer\\PropertyNormalizer`::
231
225
$obj->address = 'Euratechnologies, 2 rue Hegel, 59160 Lomme, France';
232
226
233
227
$json = $serializer->serialize($obj);
228
+ // {"org_name": "Les-Tilleuls.coop", "org_address": "Euratechnologies, 2 rue Hegel, 59160 Lomme, France"}
234
229
$objCopy = $serializer->deserialize($json);
230
+ // Same data as $obj
231
+
232
+ .. _using-camelized-method-names-for-underscored-attributes :
235
233
236
- CamelCase to Underscore
234
+ CamelCase to snake_case
237
235
~~~~~~~~~~~~~~~~~~~~~~~
238
236
239
237
.. versionadded :: 2.7
240
- The :class: `Symfony\\ Component\\ Serializer\\ NameConverter\\ CamelCaseToUnderscoreNameConverter `
238
+ The :class: `Symfony\\ Component\\ Serializer\\ NameConverter\\ CamelCaseToUnderscoreNameConverter `
241
239
interface was introduced in Symfony 2.7.
242
240
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.
246
244
247
245
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
249
247
processes::
250
248
251
249
use Symfony\Component\Serializer\NameConverter\CamelCaseToUnderscoreNameConverter;
@@ -273,6 +271,7 @@ processes::
273
271
// ['given_name' => 'Kévin'];
274
272
275
273
$anne = $normalizer->denormalize(array('given_name' => 'Anne'), 'Person');
274
+ // Person object with givenName: 'Anne'
276
275
277
276
Serializing Boolean Attributes
278
277
------------------------------
0 commit comments