8000 Documented how to parse and dump custom YAML tags · symfony/symfony-docs@c1a24aa · GitHub
[go: up one dir, main page]

Skip to content

Commit c1a24aa

Browse files
committed
Documented how to parse and dump custom YAML tags
1 parent 3137a73 commit c1a24aa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

components/yaml.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ Binary data is automatically parsed if they include the ``!!binary`` YAML tag
377377
$parsed = Yaml::parse($dumped);
378378
$imageContents = $parsed['logo'];
379379

380+
Parsing and Dumping Custom Tags
381+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
382+
383+
.. versionadded:: 3.3
384+
Support for parsing and dumping custom tags was introduced in Symfony 3.3.
385+
386+
In addition to the built-in support of tags like ``!php/const`` and
387+
``!!binary``, you can define your own custom YAML tags and parse them with the
388+
``PARSE_CUSTOM_TAGS`` flag::
389+
390+
$data = "!my_tag { foo: bar }";
391+
$parsed = Yaml::parse($data, Yaml::PARSE_CUSTOM_TAGS);
392+
// $parsed = Symfony\Component\Yaml\Tag\TaggedValue('my_tag', array('foo' => 'bar'));
393+
$tagName = $parsed->getTag(); // $tagName = 'my_tag'
394+
$tagValue = $parsed->getValue(); // $tagV 917D alue = array('foo' => 'bar')
395+
396+
If the contents to dump contain :class:`Symfony\\Component\\Yaml\\Tag\\TaggedValue`
397+
objects, they are automatically transformed into YAML tags::
398+
399+
use Symfony\Component\Yaml\Tag\TaggedValue;
400+
401+
$data = new TaggedValue('my_tag', array('foo' => 'bar'));
402+
$dumped = Yaml::dump($data);
403+
// $dumped = '!my_tag { foo: bar }'
404+
380405
Syntax Validation
381406
~~~~~~~~~~~~~~~~~
382407

0 commit comments

Comments
 (0)
0