4

I'm trying to use the Symfony Yaml dump function to output some nested php array data.

use \Symfony\Component\Yaml\Yaml;

echo Yaml::dump([
    'arr'=>[],
    'foo'=>'bar',
]);

However, the dumped YAML contains an empty object:

arr: {  }
foo: bar

while I want an empty list:

arr: []
foo: bar

I tried using the Yaml::DUMP_OBJECT_AS_MAP flag, using the ArrayObject instead of array literals, and using an EmptyIterator, all to no avail.

I found two closed bugs related to this: https://github.com/symfony/symfony/issues/9870 and https://github.com/symfony/symfony/issues/15781, but the solutions there don't seem to work, or are a bit too hacky and brittle for my taste (str_replace on the YAML output, brrrr)

I have a simple testcase with what I tried so far: https://github.com/ComaVN/php-yaml-empty-array

Any suggestions on how to fix this?

3
  • what version of Symfony? I think this may have been addressed in 3.1? Commented Jul 21, 2016 at 18:43
  • I encountered it in 2.3, but the problem still occurs with the latest version of symfony/yaml (v3.1.2, see my testcase) Commented Jul 22, 2016 at 7:03
  • 1
    see github.com/symfony/symfony/pull/17578 and symfony.com/blog/… Commented Jul 22, 2016 at 13:52

2 Answers 2

7

As of Symfony 3.3 (released May 2017) you can use the new Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE option:

Yaml::dump($object, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);

This feature was added in PR 21471.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! Adding "symfony/yaml": "^3.3@dev" to my composer.json allowed me to use this feature, and thus create swagger-ui compatible yaml.
-2

Try putting that in some form quotes, so that it can be extracted at a later time?

'arr'=>'[]',

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.