Commit 84e2730
committed
feature symfony#60228 [Serializer] Support preserving array keys with
This PR was merged into the 7.4 branch.
Discussion
----------
[Serializer] Support preserving array keys with `XmlEncoder`
| Q | A
| ------------- | ---
| Branch? | 7.4
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues |
| License | MIT
### Feature: Add Support for preserving array keys as Child `<item>` Elements in XML Encoder
This PR introduces a new option to the Symfony Serializer's `XmlEncoder` that allows indexed arrays to be encoded as `<item>` elements under a single parent element, rather than repeating the parent element for each array item.
#### Motivation
Currently, encoding an array like:
```php
['person' => [
['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
['firstname' => 'Damien', 'lastname' => 'Clay'],
]]
```
produces the following XML:
```xml
<response>
<person>
<firstname>Benjamin</firstname>
<lastname>Alexandre</lastname>
</person>
<person>
<firstname>Damien</firstname>
<lastname>Clay</lastname>
</person>
</response>
```
This structure is not ideal when a clear container-child relationship is required, such as when interoperating with systems that expect such XML structures.
#### New Behavior
With the new `XmlEncoder::PRESERVE_NUMERIC_KEYS` option enabled, the same data now encodes to:
```xml
<response>
<person>
<item key="0">
<firstname>Benjamin</firstname>
<lastname>Alexandre</lastname>
</item>
<item key="1">
<firstname>Damien</firstname>
<lastname>Clay</lastname>
</item>
</person>
</response>
```
#### Usage
```php
$xml = $encoder->encode($data, 'xml', [
XmlEncoder::PRESERVE_NUMERIC_KEYS => true,
]);
```
Commits
-------
d89d08f [Serializer] Add `XmlEncoder::PRESERVE_NUMERIC_KEYS` context optionXmlEncoder (Maximilian Ruta)File tree
5 files changed
+62
-2
lines changed- src/Symfony/Component/Serializer
- Context/Encoder
- Encoder
- Mapping/Loader/schema
- Tests/Encoder
5 files changed
+62
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
168 | 168 | | |
169 | 169 | | |
170 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
171 | 179 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| 80 | + | |
79 | 81 | | |
80 | 82 | | |
81 | 83 | | |
| |||
347 | 349 | | |
348 | 350 | | |
349 | 351 | | |
| 352 | + | |
350 | 353 | | |
351 | 354 | | |
352 | 355 | | |
| |||
373 | 376 | | |
374 | 377 | | |
375 | 378 | | |
376 | | - | |
| 379 | + | |
377 | 380 | | |
378 | | - | |
| 381 | + | |
379 | 382 | | |
380 | 383 | | |
381 | 384 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
329 | 333 | | |
330 | 334 | | |
331 | 335 | | |
| |||
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1033 | 1033 | | |
1034 | 1034 | | |
1035 | 1035 | | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
1036 | 1080 | | |
0 commit comments