šŸ“– 12 min read (~ 2400 words).

The YAML bestiary

Every entry below is legal YAML. Read it, decide what it holds, then open the answer: the JSON go-yaml reads from it.

Quizz: test your YAML knowledge (impossible level).

Words and numbers

Case counts, partly

- True
- TRUE
- tRUE
- Null
- NULL
- nULL
- ~
What does it hold?
[
  true,
  true,
  "tRUE",
  null,
  null,
  "nULL",
  null
]

The core schema knows three spellings of each word: all lower case, a capital first letter, all capitals. tRUE and nULL are none of these, so they are strings.

File modes

mode: 0755
flags: 0o755
What does it hold?
{
  "mode": 755,
  "flags": 493
}

YAML 1.2 writes an octal number with 0o. A leading zero alone means nothing, so 0755 is the decimal 755.

YAML 1.1 reads a leading zero as octal, and has no 0o:

{
  "mode": 493,
  "flags": "0o755"
}

A file mode written for one version reads as another number under the other.

Base 60

port: 22:22
time: 1:30
What does it hold?
{
  "port": "22:22",
  "time": "1:30"
}

Two strings, under YAML 1.2. YAML 1.1 reads digits split by colons as a number in base 60, for times and angles:

{
  "port": 1342,
  "time": 90
}

22:22 is 22 Ɨ 60 + 22, so a port mapping reads as 1342.

Digit groups

big: 1_000
What does it hold?
{
  "big": "1_000"
}

YAML 1.2 has no digit separator, so 1_000 is a string. YAML 1.1 skips the _ in a number:

{
  "big": 1000
}

Numbers JSON would not write

- 0x1F
- +12
- 1e3
- .5
- 5.
- -0
What does it hold?
[
  31,
  12,
  1e3,
  0.5,
  5.0,
  -0
]

All six are numbers under YAML 1.2: a hexadecimal integer, a signed one, a float with an exponent and no dot, and floats with nothing before or after the dot. go-yaml writes 5. as 5.0, because JSON wants a digit after the dot.

YAML 1.1 wants a dot in every float, and a sign on its exponent, so 1e3 is a string there:

[
  31,
  12,
  "1e3",
  0.5,
  5.0,
  -0
]

Keys

A key left out

: value
What does it hold?
{
  "null": "value"
}

A key may be empty, and an empty node is null. JSON keys are strings, so the key reads as "null".

Keys that start with an indicator

?foo: bar
:baz: :qux
What does it hold?
{
  "?foo": "bar",
  ":baz": ":qux"
}

? opens an explicit key and : a value only when a space follows them. Followed by a letter, each starts a plain scalar.

A key written as JSON

{"a":b, "c" :d}
What does it hold?
{
  "a": "b",
  "c": "d"
}

A : needs a space after it, except after a quoted key inside a flow collection. YAML 1.2 allows "a":b there, so that JSON written without spaces still reads. A space before the : is allowed everywhere. In a block mapping, "a":b is an error.

A literal key

? |
  multi
  line
: value
What does it hold?
{
  "multi\nline\n": "value"
}

An explicit key may be any node, a block scalar included. It keeps its final line break, as a block scalar does by default.

Anchors and aliases

An alias as a key

base: &k name
*k : value
What does it hold?
{
  "base": "name",
  "name": "value"
}

*k stands for name, and the mapping uses it as a key. The space before : is needed: without it, the : belongs to the alias name, as the next entry shows.

A colon in an anchor

key: &an:chor value
again: *an:chor
What does it hold?
{
  "key": "value",
  "again": "value"
}

An anchor name runs to the next space. It may hold any printable character except ,, [, ], { and }, so an:chor is one name.

An anchor named !

a: &! x
b: *!
What does it hold?
{
  "a": "x",
  "b": "x"
}

&! is not an anchor followed by a tag: it is the anchor named !, and *! is its alias.

Flow collections

A pair in a list

[a: 1, b]
What does it hold?
[
  {
    "a": 1
  },
  "b"
]

Inside a flow sequence, a: 1 is a mapping of one pair, written without its braces. The sequence holds that mapping and the string b.

Keys without values

{a, b: c, d: }
What does it hold?
{
  "a": null,
  "b": "c",
  "d": null
}

In a flow mapping, an entry may be a key alone, or a key and a : with nothing after it. Both values are null.

Explicit keys in flow

{ ? a : b, ? c }
What does it hold?
{
  "a": "b",
  "c": null
}

? works in flow too. ? c is a key with no value, so c holds null.

A trailing comma

[a, b, ]
What does it hold?
[
  "a",
  "b"
]

Two entries. A flow collection may end on a comma, which JSON forbids.

Dashes

Compact nesting

- - a
  - b
- c: 1
  d: 2
What does it hold?
[
  [
    "a",
    "b"
  ],
  {
    "c": 1,
    "d": 2
  }
]

A sequence entry may open a collection on the line of its dash: - - a starts a sequence inside a sequence, and - c: 1 a mapping. The lines below line up with the inner collection’s first entry.

A dash and a space

- -a
- -1
- - 1
What does it hold?
[
  "-a",
  -1,
  [
    1
  ]
]

A - opens an entry only when a space follows it. -a is a string, -1 a negative number, and - - 1 an entry holding the sequence [1].

A dash on a continued line

- a
  -b
What does it hold?
[
  "a -b"
]

One entry. -b has no space after its dash, so it opens no entry: the line continues the plain scalar a.

Plain scalars and comments

One scalar, three lines

motto: this is
  one plain
  scalar
What does it hold?
{
  "motto": "this is one plain scalar"
}

A plain scalar may run over several lines, as long as each continues further right than its key. YAML folds the line breaks into spaces.

A hash in the text

key: a#b # c
What does it hold?
{
  "key": "a#b"
}

A comment starts at a # after a space. The first # has none, so it belongs to the text, and the comment starts at the second.

A comment before the value

key: # nothing here
  value
What does it hold?
{
  "key": "value"
}

The comment ends the line, not the entry. The value on the next line, indented under the key, still belongs to key.

Quoted scalars

A quoted scalar over several lines

text: "one
  two

  three"
What does it hold?
{
  "text": "one two\nthree"
}

A quoted scalar folds its lines as a plain scalar does: a line break becomes a space, and a blank line becomes one line break. The indentation of each line is dropped.

An escaped line break

text: "abc\
  def"
What does it hold?
{
  "text": "abcdef"
}

A \ at the end of a line in a double-quoted scalar escapes the line break. The two lines join with no space, and the next line’s indentation is dropped.

Escapes

- "\x41\u00e9"
- "\/"
- ''''
What does it hold?
[
  "AĆ©",
  "/",
  "'"
]

A double-quoted scalar takes \x with two hex digits and \u with four. YAML 1.2 added \/ for JSON’s sake: it is a /. A single-quoted scalar has no escapes: '' is one quote, so '''' holds a single '.

Documents and layout

Nothing, twice

---
---
# nothing
...
What does it hold?
# document 1
null
# document 2
null

Two documents, each null. Each --- opens a document, and a comment is not content.

A scalar on the marker line

--- a
--- |
  b
--- >
  c
  d
What does it hold?
# document 1
"a"
# document 2
"b\n"
# document 3
"c d\n"

Three documents, each one scalar. A document may start its content on the --- line, block scalar headers included.

An indented document

  a: 1
  b: 2
What does it hold?
{
  "a": 1,
  "b": 2
}

The root collection need not start in the first column. Its entries only have to line up with each other.

From the generated corpus

go-yaml tests itself against a corpus of generated documents, in internal/testintegration/yamlcorpus. The generator writes valid documents, then changes each one a little: it swaps two neighbours, substitutes or inserts an indicator, duplicates or deletes a byte, or cuts the document short. In the compact corpus, 16,239 of the 25,084 changed documents are still legal YAML. The entries below are some of them: legal documents that no person would write.

A directive that lost its %

substitute an indicator turned %YAML 1.2 into this:

&YAML 1.2
---
-1
What does it hold?
# document 1
1.2
# document 2
-1

Two documents. &YAML is an anchor named YAML on the number 1.2, and that is the first document. A directive must come before a ---, so the --- here opens a second document, holding -1.

A directive nobody knows

%YA]L 1.2
---
True # c1
What does it hold?
true

%YA]L is a directive with a name YAML does not define. The specification reserves such names, and a parser ignores them, so the document reads as if the line were not there.

Explicit keys under a swapped directive

%AYML 1.2
---
{? &a1 '': !!null Null, ? ! '? a'}
What does it hold?
{
  "": null,
  "? a": null
}

swap neighbors turned %YAML into %AYML, an unknown directive, so it is ignored. The first key is the empty string '', anchored, and its value Null is tagged !!null. The second key is the quoted '? a' under the non-specific tag !, with no value.

Four dashes

----
&a1 !!float 55.12738563616954
What does it hold?
"---- &a1 !!float 55.12738563616954"

duplicate a byte added a fourth dash. --- marks a document only when a space or a line break follows it, so ---- is text. The next line continues that plain scalar, and the anchor, the tag and the number join the text.

A marker glued to a mapping

---*{D: False} # c1
What does it hold?
{
  "---*{D": "False}"
}

substitute a byte replaced the line break after --- with *. With no space after it, ---*{D is a plain scalar, and the first : makes it a key. Inside a plain scalar of a block mapping, { and } are ordinary characters, so the value is the string False}.

An anchor that ate a tag

---
&a1! <tag:yaml.org,2002:int> -23 # c1
What does it hold?
"<tag:yaml.org,2002:int> -23"

swap neighbors moved the ! before the space. An anchor name runs to the next space, so the anchor is a1!, and <tag:yaml.org,2002:int> -23 is no longer a tag and a number: it is a string.

An anchor and nothing

&a1-~
What does it hold?
null

The ~ would be null, but it belongs to the anchor name: a1-~. The anchor stands on an empty node, which is null anyway.

A tag that was a word

%YAML 1.2
---
&a1 !true
What does it hold?
""

insert an indicator put a ! before true. !true is now a local tag, and the node it stands on is empty. go-yaml has no type for the tag, so it keeps the node’s text: the empty string, not a boolean and not null.

Cut short

---
!!str base: &m1
What does it hold?
{
  "base": null
}

truncate cut the document after the anchor. !!str tags the key base, and the value is an anchor on an empty node, which is null.

A lost comma

! [FALSE ~]
What does it hold?
[
  "FALSE ~"
]

delete a byte removed the comma. A plain scalar may hold spaces, so FALSE ~ is one entry, and it reads as neither a boolean nor a null. With the comma, the same sequence holds two:

[
  false,
  null
]

An entry over three lines

&a2
# c1
-
      
 c2
      - &a1 {}
What does it hold?
[
  "c2 - &a1 {}"
]

The - has nothing after it on its line, so its value starts at c2, after a blank line. The next line is indented further than the dash, so it continues the plain scalar: its - opens no entry, and &a1 {} is text.

A bracket left open

---
# c1
- &a1
   # c2
   ? "+0b11"
   : NULL[
What does it hold?
[
  {
    "+0b11": "NULL["
  }
]

The entry’s anchor stands on the mapping below it, past a comment. The mapping holds one explicit key, the quoted "+0b11". [ opens a flow sequence only at the start of a node, so NULL[ is a plain scalar.

A signed key

# c1
+212.83274740247398: &a1 null # c2
...
"#" # c3
What does it hold?
# document 1
{
  "212.83274740247398": null
}
# document 2
"#"

Two documents: ... ends the first. Its key is a float, and go-yaml names a number key in JSON by its value, so the + is gone. The second document is the string #: a quoted # starts no comment.

An indentation indicator at the root

%YAML 1.2
---
|3-
  |a


What does it hold?
"|a"

|3 sets the content’s indentation to the block scalar’s own level plus 3. Under a key or a -, the level is 0 and the content starts in column 3. A root node stands at level -1, so here the content starts in column 2, and |a keeps no space.

indent a line further moved the line four columns right. The four extra spaces are content:

"    |a"

Readers disagree on this one. go-yaml and the YAML reference parser follow the grammar’s level -1. libyaml, and PyYAML with it, count a root block scalar from level 0: it rejects the first document and reads three spaces in the second.