Skip to content

Explained the explicit typing in YAML documents #8200

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
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions components/yaml/yaml_format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,25 @@ Comments can be added in YAML by prefixing them with a hash mark (``#``):
Comments are simply ignored by the YAML parser and do not need to be
indented according to the current level of nesting in a collection.

Explicit Typing
---------------

The YAML specification defines some tags to set the type of any data explicitly:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] to explicitly set [...]


.. code-block:: yaml

data:
# this value is parsed as a string (it's not transformed into a DateTime)
start_date: !str 2002-12-14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, using the !str tag is not valid YAML. I wonder if we should add support for !!str in Symfony 2.7 as a bugfix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created this issue: symfony/symfony#23607

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue closed. So let's keep it as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still reluctant to document invalid YAML. I suggest to just remove it here and only document the !!str in the 3.4 docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @xabbuh. !str is "a Symfony thing" and it's wrong. Let's pretend it doesn't exist 😁

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :D


# this value is parsed as a float number (it will be 3.0 instead of 3)
price: !!float 3

# this value is parsed as binary data encoded in base64
picture: !!binary |
R0lGODlhDAAMAIQAAP//9/X
17unp5WZmZgAAAOfn515eXv
Pz7Y6OjuDg4J+fn5OTk6enp
56enmleECcgggoBADs=

.. _YAML: http://yaml.org/