Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Serializer] Add Support for Encoding Arrays as Child <item> Elements in XML Encoder#60228

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

Open
Deltachaos wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromDeltachaos:feature/numeric-items

Conversation

Deltachaos
Copy link
Contributor

@DeltachaosDeltachaos commentedApr 16, 2025
edited
Loading

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
Issues
LicenseMIT

Feature: Add Support for Encoding Arrays as Child<item> Elements in XML Encoder

This PR introduces a new option to the Symfony Serializer'sXmlEncoder 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:

['person' => [    ['firstname' =>'Benjamin','lastname' =>'Alexandre'],    ['firstname' =>'Damien','lastname' =>'Clay'],]]

produces the following 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 newXmlEncoder::ARRAY_AS_ITEM option enabled, the same data now encodes to:

<response>    <person>        <itemkey="0">            <firstname>Benjamin</firstname>            <lastname>Alexandre</lastname>        </item>        <itemkey="1">            <firstname>Damien</firstname>            <lastname>Clay</lastname>        </item>    </person></response>

Usage

$xml =$encoder->encode($data,'xml', [    XmlEncoder::ARRAY_AS_ITEM =>true,]);

@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

3 participants
@Deltachaos@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp