Skip to content

[YAML] Question about array dumping #16266

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

Closed
nerdstein opened this issue Oct 16, 2015 · 4 comments
Closed

[YAML] Question about array dumping #16266

nerdstein opened this issue Oct 16, 2015 · 4 comments
Labels

Comments

@nerdstein
Copy link

I have a YML file that I parse, which has the following syntax for an empty array:

some_field: [ ]

Upon dumping the same YML file, and not adjusting some_field, the value is dumped as:

some_field: { }

My understanding is that the { } syntax is intended for objects, not arrays. As such, I isolated the code found in the dumpArray function which was introducing this behavior:

https://github.com/symfony/yaml/blob/2.8/Inline.php#L191

return sprintf('{ %s }', implode(', ', $output));

.... adjusted to ....

return sprintf('[ %s ]', implode(', ', $output));

... fixes this issue.

Is there a reason why this is using the object-based syntax and not the array syntax?

@wouterj
Copy link
Member

wouterj commented Oct 16, 2015

See #16267 for a fix. The problem was that the method filtered the array keys on whether it has only numeric keys (in which case a Yaml sequence should be dumped) or some string keys as well (in which case a Yaml mapping ({}) should be dumped). When an empty array was passed, the filter algorithm decided it always was a mapping. In the PR, I changed it to always consider it a sequence when an empty array is passed.

@nerdstein
Copy link
Author

@wouterj, I am currently using 2.8. Will your patch also be applied to that version?

@xabbuh xabbuh added the Yaml label Oct 18, 2015
@xabbuh
Copy link
Member

xabbuh commented Oct 18, 2015

This looks like a duplicate of #9870 to me.

@xabbuh
Copy link
Member

xabbuh commented Feb 8, 2016

see #17728

fabpot added a commit that referenced this issue Feb 28, 2016
This PR was merged into the 3.1-dev branch.

Discussion
----------

[Yaml] add option to dump objects as maps

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9870, #12860, #15781, #15937, #16266
| License       | MIT
| Doc PR        | TODO

Commits
-------

3941d2e [Yaml] add option to dump objects as maps
fabpot added a commit that referenced this issue Feb 19, 2017
…0657)

This PR was squashed before being merged into the 3.3-dev branch (closes #21471).

Discussion
----------

[Yaml] Allow dumping empty array as YAML sequence

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #9870, #15937, #16266
| License       | MIT
| Doc PR        |

PHP arrays are dumped as either YAML sequences or mappings, depending on whether the array has continuos integer keys or not.

An empty array is always dumped as a YAML mapping. Sometimes you want it dumped as a YAML sequence instead.

Commits
-------

87ffaf2 Bump version number
af7067c Dump empty object as mapping
a6d94c1 [Yaml] Allow dumping empty array as YAML sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants