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
- ~
File modes
mode: 0755
flags: 0o755
Base 60
port: 22:22
time: 1:30
Digit groups
big: 1_000
Numbers JSON would not write
- 0x1F
- +12
- 1e3
- .5
- 5.
- -0
Keys
A key left out
: value
Keys that start with an indicator
?foo: bar
:baz: :qux
A key written as JSON
{"a":b, "c" :d}
A literal key
? |
multi
line
: value
Anchors and aliases
An alias as a key
base: &k name
*k : value
A colon in an anchor
key: &an:chor value
again: *an:chor
An anchor named !
a: &! x
b: *!
Flow collections
A pair in a list
[a: 1, b]
Keys without values
{a, b: c, d: }
Explicit keys in flow
{ ? a : b, ? c }
A trailing comma
[a, b, ]
Dashes
Compact nesting
- - a
- b
- c: 1
d: 2
A dash and a space
- -a
- -1
- - 1
A dash on a continued line
- a
-b
Plain scalars and comments
One scalar, three lines
motto: this is
one plain
scalar
A hash in the text
key: a#b # c
A comment before the value
key: # nothing here
value
Quoted scalars
A quoted scalar over several lines
text: "one
two
three"
An escaped line break
text: "abc\
def"
Escapes
- "\x41\u00e9"
- "\/"
- ''''
Documents and layout
Nothing, twice
---
---
# nothing
...
A scalar on the marker line
--- a
--- |
b
--- >
c
d
An indented document
a: 1
b: 2
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
A directive nobody knows
%YA]L 1.2
---
True # c1
Explicit keys under a swapped directive
%AYML 1.2
---
{? &a1 '': !!null Null, ? ! '? a'}
Four dashes
----
&a1 !!float 55.12738563616954
A marker glued to a mapping
---*{D: False} # c1
An anchor that ate a tag
---
&a1! <tag:yaml.org,2002:int> -23 # c1
An anchor and nothing
&a1-~
A tag that was a word
%YAML 1.2
---
&a1 !true
Cut short
---
!!str base: &m1A lost comma
! [FALSE ~]
An entry over three lines
&a2
# c1
-
c2
- &a1 {}
A bracket left open
---
# c1
- &a1
# c2
? "+0b11"
: NULL[A signed key
# c1
+212.83274740247398: &a1 null # c2
...
"#" # c3
An indentation indicator at the root
%YAML 1.2
---
|3-
|a