dyaml/docs/tutorials/yaml_syntax.md

242 lines
5.5 KiB
Markdown
Raw Normal View History

# YAML syntax
2011-08-16 12:53:13 +00:00
This is an introduction to the most common YAML constructs. For more detailed
information, see [PyYAML documentation](http://pyyaml.org/wiki/PyYAMLDocumentation),
2011-08-16 12:53:13 +00:00
which this article is based on,
[Chapter 2 of the YAML specification](http://yaml.org/spec/1.1/#id857168)
or the [Wikipedia page](http://en.wikipedia.org/wiki/YAML).
2011-08-16 12:53:13 +00:00
YAML is a data serialization format designed for human readability. YAML is a
recursive acronym for "YAML Ain't Markup Language".
2011-08-16 12:53:13 +00:00
YAML is similar to JSON, and in fact, JSON is a subset of YAML 1.2; but YAML has
some more advanced features and is easier to read. However, it is also more
2011-08-16 12:53:13 +00:00
difficult to parse (and probably somewhat slower). Data is stored in mappings
(associative arrays), sequences (lists) and scalars (single values). Data
structure hierarchy depends either on indentation (block context, similar to
2011-08-16 12:53:13 +00:00
Python code), or nesting of brackets and braces (flow context, similar to JSON).
YAML comments begin with `#` and continue until the end of line.
2011-08-16 12:53:13 +00:00
## Documents
2011-08-16 12:53:13 +00:00
A YAML stream consists of one or more documents starting with `---` and
optionally ending with `...` . `---` can be left out for the first document.
2011-08-16 12:53:13 +00:00
Single document with no explicit start or end:
```
2011-08-16 12:53:13 +00:00
- Red
- Green
- Blue
```
2011-08-16 12:53:13 +00:00
Same document with explicit start and end:
```
2011-08-16 12:53:13 +00:00
---
- Red
- Green
- Blue
...
```
2011-08-16 12:53:13 +00:00
A stream containing multiple documents:
```
2011-08-16 12:53:13 +00:00
---
- Red
- Green
- Blue
---
- Linux
- BSD
---
answer : 42
```
2011-08-16 12:53:13 +00:00
## Sequences
2011-08-16 12:53:13 +00:00
Sequences are arrays of nodes of any type, similar e.g. to Python lists.
In block context, each item begins with hyphen+space "- ". In flow context,
sequences have syntax similar to D arrays.
```
2011-08-16 12:53:13 +00:00
#Block context
- Red
- Green
- Blue
```
```
2011-08-16 12:53:13 +00:00
#Flow context
[Red, Green, Blue]
```
```
2011-08-16 12:53:13 +00:00
#Nested
-
- Red
- Green
- Blue
-
- Linux
- BSD
```
```
2011-08-16 12:53:13 +00:00
#Nested flow
[[Red, Green, Blue], [Linux, BSD]]
```
```
2011-08-16 12:53:13 +00:00
#Nested in a mapping
Colors:
- Red
- Green
- Blue
Operating systems:
- Linux
- BSD
```
2011-08-16 12:53:13 +00:00
## Mappings
2011-08-16 12:53:13 +00:00
Mappings are associative arrays where each key and value can be of any type,
2011-08-16 12:53:13 +00:00
similar e.g. to Python dictionaries. In block context, keys and values are
separated by colon+space ": ". In flow context, mappings have syntax similar
to D associative arrays, but with braces instead of brackets:
```
2011-08-16 12:53:13 +00:00
#Block context
CPU: Athlon
GPU: Radeon
OS: Linux
```
```
2011-08-16 12:53:13 +00:00
#Flow context
{CPU: Athlon, GPU: Radeon, OS: Linux}
```
```
2011-08-16 12:53:13 +00:00
#Nested
PC:
CPU: Athlon
GPU: Radeon
OS: Debian
Phone:
CPU: Cortex
GPU: PowerVR
OS: Android
```
```
2011-08-16 12:53:13 +00:00
#Nested flow
{PC: {CPU: Athlon, GPU: Radeon, OS: Debian},
2011-08-16 12:53:13 +00:00
Phone: {CPU: Cortex, GPU: PowerVR, OS: Android}}
```
```
2011-08-16 12:53:13 +00:00
#Nested in a sequence
- CPU: Athlon
GPU: Radeon
OS: Debian
- CPU: Cortex
GPU: PowerVR
OS: Android
```
2011-08-16 12:53:13 +00:00
Complex keys start with question mark+space "? ".
```
2011-08-16 12:53:13 +00:00
#Nested in a sequence
? [CPU, GPU]: [Athlon, Radeon]
OS: Debian
```
2011-08-16 12:53:13 +00:00
## Scalars
2011-08-16 12:53:13 +00:00
Scalars are simple values such as integers, strings, timestamps and so on.
There are multiple scalar styles.
Plain scalars use no quotes, start with the first non-space and end with the
2011-08-16 12:53:13 +00:00
last non-space character:
```
scalar: Plain scalar
```
2011-08-16 12:53:13 +00:00
Single quoted scalars start and end with single quotes. A single quote is
2011-08-16 12:53:13 +00:00
represented by a pair of single quotes ''.
```
2011-08-16 12:53:13 +00:00
scalar: 'Single quoted scalar ending with some spaces '
```
2011-08-16 12:53:13 +00:00
Double quoted scalars support C-style escape sequences.
```
2011-08-16 12:53:13 +00:00
scalar: "Double quoted scalar \n with some \\ escape sequences"
```
2011-08-16 12:53:13 +00:00
Block scalars are convenient for multi-line values. They start either with
`|` or with `>`. With `|`, the newlines in the scalar are preserved.
With `>`, the newlines between two non-empty lines are removed.
2011-08-16 12:53:13 +00:00
```
2011-08-16 12:53:13 +00:00
scalar: |
Newlines are preserved
First line
Second line
```
```
2011-08-16 12:53:13 +00:00
scalar: >
Newlines are folded
This is still the first paragraph
This is the second
paragraph
```
2011-08-16 12:53:13 +00:00
## Anchors and aliases
2011-08-16 12:53:13 +00:00
Anchors and aliases can reduce size of YAML code by allowing you to define a
value once, assign an anchor to it and use alias referring to that anchor
anywhere else you need that value. It is possible to use this to create
2011-08-16 12:53:13 +00:00
recursive data structures and some parsers support this; however, D:YAML does
not (this might change in the future, but it is unlikely).
```
2011-08-16 12:53:13 +00:00
Person: &AD
gender: male
name: Arthur Dent
Clone: *AD
```
2011-08-16 12:53:13 +00:00
## Tags
2011-08-16 12:53:13 +00:00
Tags are identifiers that specify data types of YAML nodes. Most default YAML
tags are resolved implicitly, so there is no need to specify them. D:YAML also
supports implicit resolution for custom, user specified tags.
Explicitly specified tags:
```
2011-08-16 12:53:13 +00:00
answer: !!int "42"
name: !!str "Arthur Dent"
```
2011-08-16 12:53:13 +00:00
Implicit tags:
```
2011-08-16 12:53:13 +00:00
answer: 42 #int
name: Arthur Dent #string
```
2011-08-16 12:53:13 +00:00
This table shows D types stored in *yaml.Node* default YAML tags are converted to.
Some of these might change in the future (especially !!map and !!set).
|YAML tag |D type |
|-----------------------|---------------------|
|!!null |yaml.YAMLNull |
|!!bool |bool |
|!!int |long |
|!!float |real |
|!!binary |ubyte[] |
|!!timestamp |std.datetime.SysTime |
|!!map, !!omap, !!pairs |yaml.Node.Pair[] |
|!!seq, !!set |yaml.Node[] |
|!!str |string |