Skip to content

Documented the characters that provoke a YAML escaping string #4650

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

Merged
merged 7 commits into from
Jan 3, 2015
Merged
Changes from 4 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
106 changes: 92 additions & 14 deletions components/yaml/yaml_format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,112 @@ The syntax for scalars is similar to the PHP syntax.
Strings
~~~~~~~

Strings in YAML can be wrapped both in single and double quotes. In some cases,
they can also be unquoted:

.. code-block:: yaml

A string in YAML

.. code-block:: yaml

'A singled-quoted string in YAML'

.. tip::
"A double-quoted string in YAML"

In a single quoted string, a single quote ``'`` must be doubled:

.. code-block:: yaml
Quoted styles are useful when a string starts or ends with one or more
relevant spaces, because unquoted strings are trimmed on both ends when parsing
their contents.
Copy link
Member

Choose a reason for hiding this comment

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

and also when they contain special chars

Copy link
Member

Choose a reason for hiding this comment

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

@javiereguiluz can you please add something about that in this sentence. E.g. "[...] and also if the string contains special or reserved characters."

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 don't understand your comments. When you say: "... and also if it contains special chars" ... are you referring to "quoted styles are useful in that case too" or "they are trimmed on both ends in that case too". I think that the resulting phrase would be too long and complex. Could we reword it in two or three short sentences? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

What about?

Quoted styles are useful when a string starts or end with one or more relevant spaces, because unquoted strings are trimmed on both end when parsing their contents. Quotes are required when the string contains special or reserved characters.


'A single quote '' in a single-quoted string'
When using single-quoted strings, any single quote ``'`` inside its contents
must be doubled to escape it:

.. code-block:: yaml

"A double-quoted string in YAML\n"
'A single quote '' inside a single-quoted string'

Strings containing any of the following characters must be quoted. Although you
can use double quotes, for these characters is more convenient to use single
Copy link
Member

Choose a reason for hiding this comment

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

[...] characters it is more convenient [...]

quotes, which avoids having to escape any backslash ``\``:

* ``:``
* ``{``
* ``}``
* ``[``
* ``]``
* ``,``
* ``&``
* ``*``
* ``#``
* ``?``
* ``|``
* ``-``
* ``<``
* ``>``
* ``=``
* ``!``
* ``%``
* ``@``
* ``\```
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to use a single line list here.

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 think it's confusing to put all these chars in a single line and separate them with a comma. You never know if the comma is a comma or a special char or part of other special char. That's why I used a table originally ;) In any case, tables, single-item lists or multiple-items lists all look awful to me in this case. I can't think of anything that makes this look OK.

Copy link
Contributor

Choose a reason for hiding this comment

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

i think if you wrap every item as a literal using "``" and seperate them with a comma its much more readable


The double-quoted style provides a way to express arbitrary strings, by
using ``\`` to escape characters and sequences. For instance, it is very useful
when you need to embed a ``\n`` or a Unicode character in a string.

Quoted styles are useful when a string starts or ends with one or more
relevant spaces.
.. code-block:: yaml

.. tip::
"A double-quoted string in YAML\n"

The double-quoted style provides a way to express arbitrary strings, by
using ``\`` escape sequences. It is very useful when you need to embed a
``\n`` or a unicode character in a string.
If the string contains any of the following control characters, it must be
escaped with double quotes:

* ``\0``
* ``\x01``
* ``\x02``
* ``\x03``
* ``\x04``
* ``\x05``
* ``\x06``
* ``\a``
* ``\b``
* ``\t``
* ``\n``
* ``\v``
* ``\f``
* ``\r``
* ``\x0e``
* ``\x0f``
* ``\x10``
* ``\x11``
* ``\x12``
* ``\x13``
* ``\x14``
* ``\x15``
* ``\x16``
* ``\x17``
* ``\x18``
* ``\x19``
* ``\x1a``
* ``\e``
* ``\x1c``
* ``\x1d``
* ``\x1e``
* ``\x1f``
* ``\N``
* ``\_``
* ``\L``
* ``\P``
Copy link
Member

Choose a reason for hiding this comment

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

same here


Finally, there are other cases when the strings must be quoted, no matter if
using single or double quotes:
Copy link
Member

Choose a reason for hiding this comment

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

"no matter if you're using single [...]" ?


* when the string is ``true`` or ``false`` (otherwise, it would be treated as a
Copy link
Member

Choose a reason for hiding this comment

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

I think I would prefer if all list items started with an uppercase character

boolean value);
* when the string is ``null`` or ``~`` (otherwise, it would be considered as a
``null`` value);
* when the string looks like a number, such as integers (e.g. ``2``, ``14``, etc.),
floats (e.g. ``2.6``, ``14.9``) and exponential numbers (e.g. ``12e7``, etc.)
(otherwise, it would be treated as a numeric value);
* when the string looks like a date (e.g. ``2014-12-31``) (otherwise it would be
automatically converted into a Unix timestamp).

When a string contains line breaks, you can use the literal style, indicated
by the pipe (``|``), to indicate that the string will span several lines. In
Expand Down