Skip to content

[Serializer] CsvEncoder: allow the nested array parsing to be disabled #49692

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 7.4
Choose a base branch
from

Conversation

Deuchnord
Copy link

@Deuchnord Deuchnord commented Mar 15, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR symfony/symfony-docs#18127

By default, CsvEncoder decoding process explodes the headers automatically based on the . character to generate nested arrays, the separation character being configurable based on our needs. For instance:

username,birth.day,birth.month,birth.year
deuchnord,9,6,1993

Would result in the following PHP array:

[
    "username" => "deuchnord",
    "birth" => [
        "day" => 9,
        "month => 6,
        "year" => 1993
    ]
]

This is a very nice feature, but in some cases, we prefer to skip the generation of those nested arrays.
This PR provides a way to disable it by simply setting the already existent CsvEncoder::KEY_SEPARATOR_KEY option to false, so the same CSV above now results to:

[
    "username" => "deuchnord",
    "birth.day" => 9,
    "birth.month => 6,
    "birth.year" => 1993
]

@dunglas
Copy link
Member

dunglas commented May 3, 2023

I'm 👍 for the feature, but what about encoding? You should update CsvEncoder::flatten() to take the new allowed value into account.

@dunglas
Copy link
Member

dunglas commented May 3, 2023

Also, using false as a magic value will remove the ability to choose the character to use as a separator. What about introducing a new csv_flatten option instead?

@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@OskarStark
Copy link
Contributor

@Deuchnord open to finish this PR? I would really appreciate this new feature!

Please rebase on 7.1 branch, thanks

@Deuchnord
Copy link
Author

I'm sorry, I didn't have much time lastly
I will try to finish it as soon as I can

@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants