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