Zachary Tong Nicolas Grekas
Contributed by Zachary Tong and Nicolas Grekas in #10552

Symfony 2.6 will introduce support for a new YAML feature: object maps. In previous versions, the parser returned associative PHP arrays for maps ({ 'key1': 'value1', 'key2': 'value2' }). This could cause some troubles when YAML is serialized into other formats such as JSON. Now, when the user enables object-map support, maps are represented by stdClass() objects instead of regular arrays.

In order to enable the support for object maps, you must pass the true value to the new fourth argument of the parse() method. Its default value is false to maintain backwards compatibility. Here are some examples of the new parse() method behavior:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use Symfony\Component\Yaml\Parser;
$yaml = new Parser();

$yaml->parse('[  foo, [  bar, foo  ]  ]', false, false, true);
// output: array('foo', array('bar', 'foo'))

$yaml->parse('[ { foo: { bar: foo } } ]', false, false, true);
// output: array((object) array('foo' => (object) array('bar' => 'foo')))

$yaml->parse('{ foo: bar, bar: { } }', false, false, true);
// output: (object) array('foo' => 'bar', 'bar' => new \stdClass())

$yaml->parse('{ foo: bar, bar: { } }', false, false, false);
// output: (object) array('foo' => 'bar', 'bar' => array())

$yaml->parse('{ foo: [ ], bar: { } }', false, false, true);
// output: (object) array('foo' => array(), 'bar' => new \stdClass())

$yaml->parse('{ foo: [ ], bar: { } }', false, false, false);
// output: (object) array('foo' => array(), 'bar' => array())

This new feature was introduced by Zachary Tong and Nicolas Grekas in the PR #10552.

Published in #Living on the edge