2014-03-02 05:31:54 +04:00
|
|
|
---
|
|
|
|
language: yaml
|
|
|
|
filename: learnyaml.yaml
|
|
|
|
contributors:
|
2022-10-17 18:51:39 +03:00
|
|
|
|
2020-02-13 03:08:29 +03:00
|
|
|
- [Leigh Brenecki, 'https://leigh.net.au']
|
2018-01-26 00:51:15 +03:00
|
|
|
- [Suhas SG, 'https://github.com/jargnar']
|
2022-10-17 18:51:39 +03:00
|
|
|
|
2014-03-02 05:31:54 +04:00
|
|
|
---
|
|
|
|
|
|
|
|
YAML is a data serialisation language designed to be directly writable and
|
|
|
|
readable by humans.
|
|
|
|
|
|
|
|
It's a strict superset of JSON, with the addition of syntactically
|
|
|
|
significant newlines and indentation, like Python. Unlike Python, however,
|
2018-01-26 00:51:15 +03:00
|
|
|
YAML doesn't allow literal tab characters for indentation.
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
```yaml
|
2018-10-26 00:55:48 +03:00
|
|
|
--- # document start
|
|
|
|
|
2014-03-02 05:31:54 +04:00
|
|
|
# Comments in YAML look like this.
|
2023-04-07 09:23:20 +03:00
|
|
|
# YAML supports single-line comments.
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
################
|
|
|
|
# SCALAR TYPES #
|
|
|
|
################
|
|
|
|
|
|
|
|
# Our root object (which continues for the entire document) will be a map,
|
|
|
|
# which is equivalent to a dictionary, hash or object in other languages.
|
|
|
|
key: value
|
|
|
|
another_key: Another value goes here.
|
|
|
|
a_number_value: 100
|
|
|
|
scientific_notation: 1e+12
|
2022-10-17 18:51:39 +03:00
|
|
|
hex_notation: 0x123 # evaluates to 291
|
|
|
|
octal_notation: 0123 # evaluates to 83
|
|
|
|
|
|
|
|
# The number 1 will be interpreted as a number, not a boolean.
|
|
|
|
# If you want it to be interpreted as a boolean, use true.
|
2014-03-02 05:31:54 +04:00
|
|
|
boolean: true
|
|
|
|
null_value: null
|
2022-11-21 16:24:01 +03:00
|
|
|
another_null_value: ~
|
2014-03-02 05:31:54 +04:00
|
|
|
key with spaces: value
|
2022-10-17 18:51:39 +03:00
|
|
|
|
|
|
|
# Yes and No (doesn't matter the case) will be evaluated to boolean
|
|
|
|
# true and false values respectively.
|
|
|
|
# To use the actual value use single or double quotes.
|
2023-09-08 08:35:53 +03:00
|
|
|
no: no # evaluates to "no": false
|
|
|
|
yes: No # evaluates to "yes": false
|
2022-10-17 18:51:39 +03:00
|
|
|
not_enclosed: yes # evaluates to "not_enclosed": true
|
|
|
|
enclosed: "yes" # evaluates to "enclosed": yes
|
|
|
|
|
2014-03-02 05:31:54 +04:00
|
|
|
# Notice that strings don't need to be quoted. However, they can be.
|
2018-01-26 00:51:15 +03:00
|
|
|
however: 'A string, enclosed in quotes.'
|
|
|
|
'Keys can be quoted too.': "Useful if you want to put a ':' in your key."
|
|
|
|
single quotes: 'have ''one'' escape pattern'
|
|
|
|
double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and more."
|
2019-07-05 18:09:05 +03:00
|
|
|
# UTF-8/16/32 characters need to be encoded
|
|
|
|
Superscript two: \u00B2
|
2014-03-02 05:31:54 +04:00
|
|
|
|
2022-10-17 18:51:39 +03:00
|
|
|
# Special characters must be enclosed in single or double quotes
|
|
|
|
special_characters: "[ John ] & { Jane } - <Doe>"
|
|
|
|
|
|
|
|
# Multiple-line strings can be written either as a 'literal block' (using |),
|
2014-06-01 05:26:12 +04:00
|
|
|
# or a 'folded block' (using '>').
|
2022-10-17 18:51:39 +03:00
|
|
|
# Literal block turn every newline within the string into a literal newline (\n).
|
|
|
|
# Folded block removes newlines within the string.
|
2014-03-02 05:31:54 +04:00
|
|
|
literal_block: |
|
2022-10-17 18:51:39 +03:00
|
|
|
This entire block of text will be the value of the 'literal_block' key,
|
|
|
|
with line breaks being preserved.
|
2014-03-02 05:31:54 +04:00
|
|
|
|
2022-10-17 18:51:39 +03:00
|
|
|
The literal continues until de-dented, and the leading indentation is
|
|
|
|
stripped.
|
2014-03-02 05:31:54 +04:00
|
|
|
|
2022-10-17 18:51:39 +03:00
|
|
|
Any lines that are 'more-indented' keep the rest of their indentation -
|
|
|
|
these lines will be indented by 4 spaces.
|
2014-03-02 05:31:54 +04:00
|
|
|
folded_style: >
|
2022-10-17 18:51:39 +03:00
|
|
|
This entire block of text will be the value of 'folded_style', but this
|
|
|
|
time, all newlines will be replaced with a single space.
|
2014-03-02 05:31:54 +04:00
|
|
|
|
2022-10-17 18:51:39 +03:00
|
|
|
Blank lines, like above, are converted to a newline character.
|
2014-03-02 05:31:54 +04:00
|
|
|
|
2022-10-17 18:51:39 +03:00
|
|
|
'More-indented' lines keep their newlines, too -
|
|
|
|
this text will appear over two lines.
|
|
|
|
|
|
|
|
# |- and >- removes the trailing blank lines (also called literal/block "strip")
|
|
|
|
literal_strip: |-
|
|
|
|
This entire block of text will be the value of the 'literal_block' key,
|
|
|
|
with trailing blank line being stripped.
|
|
|
|
block_strip: >-
|
|
|
|
This entire block of text will be the value of 'folded_style', but this
|
|
|
|
time, all newlines will be replaced with a single space and
|
|
|
|
trailing blank line being stripped.
|
|
|
|
|
|
|
|
# |+ and >+ keeps trailing blank lines (also called literal/block "keep")
|
|
|
|
literal_keep: |+
|
|
|
|
This entire block of text will be the value of the 'literal_block' key,
|
|
|
|
with trailing blank line being kept.
|
|
|
|
|
|
|
|
block_keep: >+
|
|
|
|
This entire block of text will be the value of 'folded_style', but this
|
|
|
|
time, all newlines will be replaced with a single space and
|
|
|
|
trailing blank line being kept.
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
####################
|
|
|
|
# COLLECTION TYPES #
|
|
|
|
####################
|
|
|
|
|
2018-01-26 00:51:15 +03:00
|
|
|
# Nesting uses indentation. 2 space indent is preferred (but not required).
|
2014-03-02 05:31:54 +04:00
|
|
|
a_nested_map:
|
2018-01-26 00:51:15 +03:00
|
|
|
key: value
|
|
|
|
another_key: Another Value
|
|
|
|
another_nested_map:
|
|
|
|
hello: hello
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
# Maps don't have to have string keys.
|
|
|
|
0.25: a float key
|
|
|
|
|
2015-11-04 17:44:45 +03:00
|
|
|
# Keys can also be complex, like multi-line objects
|
|
|
|
# We use ? followed by a space to indicate the start of a complex key.
|
2014-03-02 05:31:54 +04:00
|
|
|
? |
|
2018-01-26 00:51:15 +03:00
|
|
|
This is a key
|
|
|
|
that has multiple lines
|
2014-03-02 05:31:54 +04:00
|
|
|
: and this is its value
|
|
|
|
|
2015-11-04 17:44:45 +03:00
|
|
|
# YAML also allows mapping between sequences with the complex key syntax
|
|
|
|
# Some language parsers might complain
|
|
|
|
# An example
|
|
|
|
? - Manchester United
|
|
|
|
- Real Madrid
|
2022-10-17 18:51:39 +03:00
|
|
|
: [ 2001-01-01, 2002-02-02 ]
|
2014-03-02 05:31:54 +04:00
|
|
|
|
2018-01-26 00:51:15 +03:00
|
|
|
# Sequences (equivalent to lists or arrays) look like this
|
|
|
|
# (note that the '-' counts as indentation):
|
2014-03-02 05:31:54 +04:00
|
|
|
a_sequence:
|
2018-10-26 00:55:48 +03:00
|
|
|
- Item 1
|
|
|
|
- Item 2
|
|
|
|
- 0.5 # sequences can contain disparate types.
|
|
|
|
- Item 4
|
|
|
|
- key: value
|
|
|
|
another_key: another_value
|
2022-10-17 18:51:39 +03:00
|
|
|
- - This is a sequence
|
2018-10-26 00:55:48 +03:00
|
|
|
- inside another sequence
|
|
|
|
- - - Nested sequence indicators
|
|
|
|
- can be collapsed
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
# Since YAML is a superset of JSON, you can also write JSON-style maps and
|
|
|
|
# sequences:
|
2022-10-17 18:51:39 +03:00
|
|
|
json_map: { "key": "value" }
|
|
|
|
json_seq: [ 3, 2, 1, "takeoff" ]
|
|
|
|
and quotes are optional: { key: [ 3, 2, 1, takeoff ] }
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
#######################
|
|
|
|
# EXTRA YAML FEATURES #
|
|
|
|
#######################
|
|
|
|
|
|
|
|
# YAML also has a handy feature called 'anchors', which let you easily duplicate
|
2022-10-17 18:51:39 +03:00
|
|
|
# content across your document.
|
|
|
|
# Anchors identified by & character which define the value.
|
|
|
|
# Aliases identified by * character which acts as "see above" command.
|
|
|
|
# Both of these keys will have the same value:
|
2014-03-02 05:31:54 +04:00
|
|
|
anchored_content: &anchor_name This string will appear as the value of two keys.
|
|
|
|
other_anchor: *anchor_name
|
|
|
|
|
2015-11-04 17:44:45 +03:00
|
|
|
# Anchors can be used to duplicate/inherit properties
|
|
|
|
base: &base
|
2018-01-26 00:51:15 +03:00
|
|
|
name: Everyone has same name
|
2015-11-04 17:44:45 +03:00
|
|
|
|
2024-04-04 14:26:14 +03:00
|
|
|
# The expression << is called 'Merge Key Language-Independent Type'. It is used to
|
2018-10-27 15:46:06 +03:00
|
|
|
# indicate that all the keys of one or more specified maps should be inserted
|
|
|
|
# into the current map.
|
2022-10-17 18:51:39 +03:00
|
|
|
# NOTE: If key already exists alias will not be merged
|
2020-10-20 11:56:44 +03:00
|
|
|
foo:
|
2022-10-17 18:51:39 +03:00
|
|
|
<<: *base # doesn't merge the anchor
|
2018-01-26 00:51:15 +03:00
|
|
|
age: 10
|
2022-10-17 18:51:39 +03:00
|
|
|
name: John
|
2020-10-20 11:56:44 +03:00
|
|
|
bar:
|
2022-10-17 18:51:39 +03:00
|
|
|
<<: *base # base anchor will be merged
|
2018-01-26 00:51:15 +03:00
|
|
|
age: 20
|
2015-11-04 17:44:45 +03:00
|
|
|
|
2024-05-13 22:32:33 +03:00
|
|
|
# foo name won't be changed and it will be: John. On the other hand, bar's name will be changed to the base one: Everyone has same name
|
2015-11-04 17:44:45 +03:00
|
|
|
|
2014-03-02 05:31:54 +04:00
|
|
|
# YAML also has tags, which you can use to explicitly declare types.
|
2022-10-17 18:51:39 +03:00
|
|
|
# Syntax: !![typeName] [value]
|
|
|
|
explicit_boolean: !!bool true
|
|
|
|
explicit_integer: !!int 42
|
|
|
|
explicit_float: !!float -42.24
|
2014-03-02 05:31:54 +04:00
|
|
|
explicit_string: !!str 0.5
|
2022-10-17 18:51:39 +03:00
|
|
|
explicit_datetime: !!timestamp 2022-11-17 12:34:56.78 +9
|
|
|
|
explicit_null: !!null null
|
|
|
|
|
2014-03-02 05:31:54 +04:00
|
|
|
# Some parsers implement language specific tags, like this one for Python's
|
|
|
|
# complex number type.
|
|
|
|
python_complex_number: !!python/complex 1+2j
|
|
|
|
|
2015-11-04 17:44:45 +03:00
|
|
|
# We can also use yaml complex keys with language specific tags
|
2022-10-17 18:51:39 +03:00
|
|
|
? !!python/tuple [ 5, 7 ]
|
2015-11-04 17:44:45 +03:00
|
|
|
: Fifty Seven
|
2017-08-23 11:14:39 +03:00
|
|
|
# Would be {(5, 7): 'Fifty Seven'} in Python
|
2015-11-04 17:44:45 +03:00
|
|
|
|
2014-03-02 05:31:54 +04:00
|
|
|
####################
|
|
|
|
# EXTRA YAML TYPES #
|
|
|
|
####################
|
|
|
|
|
|
|
|
# Strings and numbers aren't the only scalars that YAML can understand.
|
|
|
|
# ISO-formatted date and datetime literals are also parsed.
|
2022-10-17 18:51:39 +03:00
|
|
|
datetime_canonical: 2001-12-15T02:59:43.1Z
|
2022-12-10 18:05:34 +03:00
|
|
|
datetime_space_separated_with_time_zone: 2001-12-14 21:59:43.10 -5
|
2022-10-17 18:51:39 +03:00
|
|
|
date_implicit: 2002-12-14
|
|
|
|
date_explicit: !!timestamp 2002-12-14
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
# The !!binary tag indicates that a string is actually a base64-encoded
|
|
|
|
# representation of a binary blob.
|
|
|
|
gif_file: !!binary |
|
2018-01-26 00:51:15 +03:00
|
|
|
R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
|
|
|
|
OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
|
|
|
|
+f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
|
|
|
|
AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
|
2014-03-02 05:31:54 +04:00
|
|
|
|
|
|
|
# YAML also has a set type, which looks like this:
|
|
|
|
set:
|
2018-01-26 00:51:15 +03:00
|
|
|
? item1
|
|
|
|
? item2
|
|
|
|
? item3
|
2022-10-17 18:51:39 +03:00
|
|
|
or: { item1, item2, item3 }
|
2014-03-02 05:31:54 +04:00
|
|
|
|
2018-10-27 16:02:40 +03:00
|
|
|
# Sets are just maps with null values; the above is equivalent to:
|
2014-03-02 05:31:54 +04:00
|
|
|
set2:
|
2018-01-26 00:51:15 +03:00
|
|
|
item1: null
|
|
|
|
item2: null
|
|
|
|
item3: null
|
2018-10-26 00:55:48 +03:00
|
|
|
|
|
|
|
... # document end
|
2014-03-02 05:31:54 +04:00
|
|
|
```
|
2016-04-28 10:06:12 +03:00
|
|
|
|
|
|
|
### More Resources
|
|
|
|
|
2020-10-20 11:56:44 +03:00
|
|
|
+ [YAML official website](https://yaml.org/)
|
2018-10-26 00:55:48 +03:00
|
|
|
+ [Online YAML Validator](http://www.yamllint.com/)
|
2022-10-17 18:51:39 +03:00
|
|
|
+ [JSON ⇆ YAML](https://www.json2yaml.com/)
|