-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[YAML] Blank Array without Hash #9870
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
Comments
I'm afraid there's no good solution for your problem. How can you tell that an empty array is suppose to be treated as a hash or an array? Furthermore, whatever we do we have to be consistent - so either dump an empty array as |
I disagree on the consistency part since this a new feature request and not a change. I realize there is not a perfect solution for this so I am hoping we can come up with a way to make it work. I think the default empty array should be a hash So I think if you want an empty hash then pass an empty array or if you want an empty list pass something in. Here are some ideas:
Adding detection for one of these types to output it a different way should be possible right? It would simply detect if |
What if you actually wanted the following: $dump = array('test' => array('0' => '')); to be dumped to: test:
- '' The same question applies to the remaining proposals. |
If you wanted that its the same thing as
So I would just suggest to anyone using On the other examples I mentioned it is possible with The other suggestion from either of these is to pass an option to dump that can be used to tell it to use Another potential idea:
That would currently output YAML I believe like this:
I don't know if that it is valid YAML anyway so it could be leveraged to create |
I had the same problem. maybe using standard objects for empty objects instead of arrays, like: $dump = ['array' => [], 'object'=>(object)[]]; and the output array: []
object: {} |
@developingo good idea. That would be perfect. |
implemented as of #10552 |
Sorry for the n00b question, but it seems to me that referenced issue (#10552) only updates the parse() method, and not the dump() method as specified in this ticket (#9870). I indeed also encounter this issue: dump() should check if empty array is actually an empty object, and dump accordingly. It is important for some requirements (like, for example, Swagger) Could you please reopen this issue? |
How would you determine if an array should actually be an empty object? |
Just like it was proposed earlier. Use an empty stdClass instance. I Le mar. 30 juin 2015 09:06, Christian Flothmann [email protected]
|
The issue is that the dumper only handles PHP objects when you enable object support. If the support is not enabled, the dumper will always return ping @symfony/deciders What do you think? |
What about adding a new parameter (false by default for BC) just like it Le mar. 30 juin 2015 13:21, Christian Flothmann [email protected]
|
see #17728 |
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
…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
Right now YAML component dumps an empty array like this:
$dump = array('test' => array());
astest: { }
. In some applications that use the YAML it needs to betest: [ ]
a list instead of a hash.In the symfony codebase on this line (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Yaml/Inline.php#L160) I found:
It looks to me if I pass
$dump = array('test' => array('0' => ''))
It should work? However this code outputs:test: - ''
. Its successfully using the list format but its passing the blank value. I think another conditional is missing to be able to create:test: [ ]
. I checked the unit tests and I can't find anywhere that explains what(1 == count($keys) && '0' == $keys[0])
was intended for other than this purpose.EDIT: It doesn't look to be hitting that section of code at all since my indent level is really high. However I am still tring to figure out how to get it to output a
[ ]
instead of hash{ }
since it is invalid yaml for what I am using it for and a blank array defaults to the hash.The text was updated successfully, but these errors were encountered: