Skip to content

Commit b9c101f

Browse files
committed
minor symfony#17680 [Yaml] Merge YAML docs into one (fabpot)
This PR was merged into the 5.4 branch. Discussion ---------- [Yaml] Merge YAML docs into one <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `6.x` for features of unreleased versions). --> Commits ------- 956de74 Merge YAML docs into one
2 parents cd9e68a + 956de74 commit b9c101f

File tree

3 files changed

+336
-353
lines changed

3 files changed

+336
-353
lines changed

components/yaml.rst

Lines changed: 335 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ the `YAML 1.2 version specification`_.
2323

2424
.. tip::
2525

26-
Learn more about the Yaml component in the
27-
:doc:`/components/yaml/yaml_format` article.
26+
Learn more about :ref:`YAML specifications <yaml-format>`.
2827

2928
Installation
3029
------------
@@ -452,14 +451,342 @@ Add the ``--format`` option to get the output in JSON format:
452451
YAML files. This may for example be useful for recognizing deprecations of
453452
contents of YAML files during automated tests.
454453

455-
Learn More
456-
----------
454+
.. _yaml-format:
457455

458-
.. toctree::
459-
:maxdepth: 1
460-
:glob:
456+
.. index::
457+
single: Yaml; YAML Format
458+
459+
The YAML Format
460+
---------------
461+
462+
Scalars
463+
~~~~~~~
464+
465+
The syntax for scalars is similar to the PHP syntax.
466+
467+
Strings
468+
.......
469+
470+
Strings in YAML can be wrapped both in single and double quotes. In some cases,
471+
they can also be unquoted:
472+
473+
.. code-block:: yaml
474+
475+
A string in YAML
476+
477+
'A single-quoted string in YAML'
478+
479+
"A double-quoted string in YAML"
480+
481+
Quoted styles are useful when a string starts or end with one or more relevant
482+
spaces, because unquoted strings are trimmed on both end when parsing their
483+
contents. Quotes are required when the string contains special or reserved characters.
484+
485+
When using single-quoted strings, any single quote ``'`` inside its contents
486+
must be doubled to escape it:
487+
488+
.. code-block:: yaml
489+
490+
'A single quote '' inside a single-quoted string'
491+
492+
Strings containing any of the following characters must be quoted. Although you
493+
can use double quotes, for these characters it is more convenient to use single
494+
quotes, which avoids having to escape any backslash ``\``:
495+
496+
* ``:``, ``{``, ``}``, ``[``, ``]``, ``,``, ``&``, ``*``, ``#``, ``?``, ``|``,
497+
``-``, ``<``, ``>``, ``=``, ``!``, ``%``, ``@``, `````
498+
499+
The double-quoted style provides a way to express arbitrary strings, by
500+
using ``\`` to escape characters and sequences. For instance, it is very useful
501+
when you need to embed a ``\n`` or a Unicode character in a string.
502+
503+
.. code-block:: yaml
504+
505+
"A double-quoted string in YAML\n"
506+
507+
If the string contains any of the following control characters, it must be
508+
escaped with double quotes:
509+
510+
* ``\0``, ``\x01``, ``\x02``, ``\x03``, ``\x04``, ``\x05``, ``\x06``, ``\a``,
511+
``\b``, ``\t``, ``\n``, ``\v``, ``\f``, ``\r``, ``\x0e``, ``\x0f``, ``\x10``,
512+
``\x11``, ``\x12``, ``\x13``, ``\x14``, ``\x15``, ``\x16``, ``\x17``, ``\x18``,
513+
``\x19``, ``\x1a``, ``\e``, ``\x1c``, ``\x1d``, ``\x1e``, ``\x1f``, ``\N``,
514+
``\_``, ``\L``, ``\P``
515+
516+
Finally, there are other cases when the strings must be quoted, no matter if
517+
you're using single or double quotes:
518+
519+
* When the string is ``true`` or ``false`` (otherwise, it would be treated as a
520+
boolean value);
521+
* When the string is ``null`` or ``~`` (otherwise, it would be considered as a
522+
``null`` value);
523+
* When the string looks like a number, such as integers (e.g. ``2``, ``14``, etc.),
524+
floats (e.g. ``2.6``, ``14.9``) and exponential numbers (e.g. ``12e7``, etc.)
525+
(otherwise, it would be treated as a numeric value);
526+
* When the string looks like a date (e.g. ``2014-12-31``) (otherwise it would be
527+
automatically converted into a Unix timestamp).
528+
529+
When a string contains line breaks, you can use the literal style, indicated
530+
by the pipe (``|``), to indicate that the string will span several lines. In
531+
literals, newlines are preserved:
532+
533+
.. code-block:: yaml
534+
535+
|
536+
\/ /| |\/| |
537+
/ / | | | |__
538+
539+
Alternatively, strings can be written with the folded style, denoted by ``>``,
540+
where each line break is replaced by a space:
541+
542+
.. code-block:: yaml
543+
544+
>
545+
This is a very long sentence
546+
that spans several lines in the YAML.
547+
548+
# This will be parsed as follows: (notice the trailing \n)
549+
# "This is a very long sentence that spans several lines in the YAML.\n"
550+
551+
>-
552+
This is a very long sentence
553+
that spans several lines in the YAML.
554+
555+
# This will be parsed as follows: (without a trailing \n)
556+
# "This is a very long sentence that spans several lines in the YAML."
557+
558+
.. note::
559+
560+
Notice the two spaces before each line in the previous examples. They
561+
won't appear in the resulting PHP strings.
562+
563+
Numbers
564+
.......
565+
566+
.. code-block:: yaml
567+
568+
# an integer
569+
12
570+
571+
.. code-block:: yaml
572+
573+
# an octal
574+
0o14
575+
576+
.. deprecated:: 5.1
577+
578+
In YAML 1.1, octal numbers use the notation ``0...``, whereas in YAML 1.2
579+
the notation changes to ``0o...``. Symfony 5.1 added support for YAML 1.2
580+
notation and deprecated support for YAML 1.1 notation.
581+
582+
.. code-block:: yaml
583+
584+
# an hexadecimal
585+
0xC
586+
587+
.. code-block:: yaml
588+
589+
# a float
590+
13.4
591+
592+
.. code-block:: yaml
593+
594+
# an exponential number
595+
1.2e+34
596+
597+
.. code-block:: yaml
598+
599+
# infinity
600+
.inf
601+
602+
Nulls
603+
.....
604+
605+
Nulls in YAML can be expressed with ``null`` or ``~``.
606+
607+
Booleans
608+
........
609+
610+
Booleans in YAML are expressed with ``true`` and ``false``.
611+
612+
Dates
613+
.....
614+
615+
YAML uses the `ISO-8601`_ standard to express dates:
616+
617+
.. code-block:: yaml
618+
619+
2001-12-14T21:59:43.10-05:00
620+
621+
.. code-block:: yaml
622+
623+
# simple date
624+
2002-12-14
625+
626+
.. _yaml-format-collections:
627+
628+
Collections
629+
~~~~~~~~~~~
630+
631+
A YAML file is rarely used to describe a simple scalar. Most of the time, it
632+
describes a collection. YAML collections can be a sequence (indexed arrays in PHP)
633+
or a mapping of elements (associative arrays in PHP).
634+
635+
Sequences use a dash followed by a space:
636+
637+
.. code-block:: yaml
638+
639+
- PHP
640+
- Perl
641+
- Python
642+
643+
The previous YAML file is equivalent to the following PHP code::
644+
645+
['PHP', 'Perl', 'Python'];
646+
647+
Mappings use a colon followed by a space (``:`` ) to mark each key/value pair:
648+
649+
.. code-block:: yaml
650+
651+
PHP: 5.2
652+
MySQL: 5.1
653+
Apache: 2.2.20
654+
655+
which is equivalent to this PHP code::
656+
657+
['PHP' => 5.2, 'MySQL' => 5.1, 'Apache' => '2.2.20'];
658+
659+
.. note::
660+
661+
In a mapping, a key can be any valid scalar.
662+
663+
The number of spaces between the colon and the value does not matter:
664+
665+
.. code-block:: yaml
666+
667+
PHP: 5.2
668+
MySQL: 5.1
669+
Apache: 2.2.20
670+
671+
YAML uses indentation with one or more spaces to describe nested collections:
672+
673+
.. code-block:: yaml
674+
675+
'symfony 1.0':
676+
PHP: 5.0
677+
Propel: 1.2
678+
'symfony 1.2':
679+
PHP: 5.2
680+
Propel: 1.3
681+
682+
The above YAML is equivalent to the following PHP code::
683+
684+
[
685+
'symfony 1.0' => [
686+
'PHP' => 5.0,
687+
'Propel' => 1.2,
688+
],
689+
'symfony 1.2' => [
690+
'PHP' => 5.2,
691+
'Propel' => 1.3,
692+
],
693+
];
694+
695+
There is one important thing you need to remember when using indentation in a
696+
YAML file: *Indentation must be done with one or more spaces, but never with
697+
tabulators*.
698+
699+
You can nest sequences and mappings as you like:
700+
701+
.. code-block:: yaml
702+
703+
'Chapter 1':
704+
- Introduction
705+
- Event Types
706+
'Chapter 2':
707+
- Introduction
708+
- Helpers
709+
710+
YAML can also use flow styles for collections, using explicit indicators
711+
rather than indentation to denote scope.
712+
713+
A sequence can be written as a comma separated list within square brackets
714+
(``[]``):
715+
716+
.. code-block:: yaml
717+
718+
[PHP, Perl, Python]
719+
720+
A mapping can be written as a comma separated list of key/values within curly
721+
braces (``{}``):
722+
723+
.. code-block:: yaml
724+
725+
{ PHP: 5.2, MySQL: 5.1, Apache: 2.2.20 }
726+
727+
You can mix and match styles to achieve a better readability:
728+
729+
.. code-block:: yaml
730+
731+
'Chapter 1': [Introduction, Event Types]
732+
'Chapter 2': [Introduction, Helpers]
733+
734+
.. code-block:: yaml
735+
736+
'symfony 1.0': { PHP: 5.0, Propel: 1.2 }
737+
'symfony 1.2': { PHP: 5.2, Propel: 1.3 }
738+
739+
Comments
740+
~~~~~~~~
741+
742+
Comments can be added in YAML by prefixing them with a hash mark (``#``):
743+
744+
.. code-block:: yaml
745+
746+
# Comment on a line
747+
"symfony 1.0": { PHP: 5.0, Propel: 1.2 } # Comment at the end of a line
748+
"symfony 1.2": { PHP: 5.2, Propel: 1.3 }
749+
750+
.. note::
751+
752+
Comments are ignored by the YAML parser and do not need to be indented
753+
according to the current level of nesting in a collection.
754+
755+
Explicit Typing
756+
~~~~~~~~~~~~~~~
757+
758+
The YAML specification defines some tags to set the type of any data explicitly:
759+
760+
.. code-block:: yaml
461761
462-
yaml/*
762+
data:
763+
# this value is parsed as a string (it's not transformed into a DateTime)
764+
start_date: !!str 2002-12-14
765+
766+
# this value is parsed as a float number (it will be 3.0 instead of 3)
767+
price: !!float 3
768+
769+
# this value is parsed as binary data encoded in base64
770+
picture: !!binary |
771+
R0lGODlhDAAMAIQAAP//9/X
772+
17unp5WZmZgAAAOfn515eXv
773+
Pz7Y6OjuDg4J+fn5OTk6enp
774+
56enmleECcgggoBADs=
775+
776+
Unsupported YAML Features
777+
~~~~~~~~~~~~~~~~~~~~~~~~~
778+
779+
The following YAML features are not supported by the Symfony Yaml component:
780+
781+
* Multi-documents (``---`` and ``...`` markers);
782+
* Complex mapping keys and complex values starting with ``?``;
783+
* Tagged values as keys;
784+
* The following tags and types: ``!!set``, ``!!omap``, ``!!pairs``, ``!!seq``,
785+
``!!bool``, ``!!int``, ``!!merge``, ``!!null``, ``!!timestamp``, ``!!value``, ``!!yaml``;
786+
* Tags (``TAG`` directive; example: ``%TAG ! tag:example.com,2000:app/``)
787+
and tag references (example: ``!<tag:example.com,2000:app/foo>``);
788+
* Using sequence-like syntax for mapping elements (example: ``{foo, bar}``; use
789+
``{foo: ~, bar: ~}`` instead).
463790

464791
.. _`YAML`: https://yaml.org/
465792
.. _`YAML 1.2 version specification`: https://yaml.org/spec/1.2/spec.html

0 commit comments

Comments
 (0)