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