Source: official specification , w3c
Definition
YAML is the abbreviation of “YAML Ain’t Markup Language” . Different from XML(eXtensible Markup Language) , YAML is a kind of data serialization language , not Markup Language.
- Different from Markup Language and Data Serialization Language
AML was designed from the start to be useful and friendly to people working with data.
Importion points
- It is case sensitive
- file extension is .yaml
- Tabs are not allowed
Two parts of YAML
- structural information
- The advantage of YAML is the structural information compressed to the minimalism;
- data itself
- can’t not be compressed
自我理解:任何形式的数据标识语言,YAML,XML,HTML,JSON等,都包含有这两部分:结构化信息与数据本身。像上面提到的数据标记语言与数据序列语言,就是对这两部分侧重不同,侧重与结构就是标记语言,侧重于内容就是序列语言。各种语言标识仅仅侧重点不同,一般都具备这两项功能,这也是理解XML,YAML,JSON等语言的切入点之一。
Language Goals
- YAML is easily readable by humans.
- YAML data is portable between programming languages.
- YAML matches the [native data structures](https://yaml.org/spec/1.2/spec.html#native data structure//) of agile languages.
- YAML has a consistent model to support generic tools.
- YAML supports one-pass processing.
- YAML is expressive and extensible.
- YAML is easy to implement and use.
Usage
- Configuration files
- Internet messaging
- Object persistence
- Data auditing.
Relation with JSON
YAML is the superSet of JSON , that means we can transfer ALL YAML file to JSON file ,Whereas no.
Data Structure in YAML
In YAML, data structures can be adequately represented with three basic primitives (consist with python):
- mapping: hashes、dictionaries,etc
- Mapping use a colon and space(“: “) to mark each “key: value “pair.
- seqences: array、list , etc
- sequences indicate each item with a dash and space(“- “)
- scalars: string、number , etc
- basic element
- comments: begin with an octothorpe(also called a “hash”, “sharp”, “pound”, or “number sign” - [“**
#
**”](https://yaml.org/spec/1.2/spec.html## comment//)).- not support multline comment
Syntax
Collections Scope
There are two ways to represent the scope of collection:
1.indent
similar with python language, that the reason way YAML is minimalist data representation.
examples:
1 | # Sequence of Scalars |
2. flow style
using explicit indicators rather than indentation to denote scope.
example:
1 | # The flow sequence is written as a comma separated list within square brackets. |
Structure
document start and end
YAML use three dashes (“—“) to separate directives from document content :
for example : two documents in a stream , each with a leading comment
1 | # Ranking of 1998 home runs |
- “—“ : start signal of document , “…” : end signal of document:
1 | --- |
ref in YAML
Repeated nodes (objects) are first identified by an anchor (marked with the ampersand - [“**&
**”](https://yaml.org/spec/1.2/spec.html#& anchor//)), and are then aliased (referenced with an asterisk - [“**\*
**”](https://yaml.org/spec/1.2/spec.html#* alias//)) thereafter.
1 | # Single Document with Two Comments |
complex mapping key
A question mark and space ([“**?
**”](https://yaml.org/spec/1.2/spec.html#? mapping key//)) indicate a complex mapping key. Within a block collection, [key: value pairs](https://yaml.org/spec/1.2/spec.html#key: value pair//) can start immediately following the [dash](https://yaml.org/spec/1.2/spec.html#- block sequence entry//), [colon](https://yaml.org/spec/1.2/spec.html#: mapping value//), or [question mark](https://yaml.org/spec/1.2/spec.html#? mapping key//).
1 | # Mapping between Sequences |
Scalar
scalar with block notation
More Details shown in this blog.
literial Style
using denotation(“|”) , all the line breaks are significant;
1 | # In literial style , all the newlines are preserved |
folded Style
Alternatively , they can be written with the folded style (denoted with “>”),each line break is folded to a space unless it ends an empty or a more indented line.
1 | # In the folded scalars, newlines become spaces |
flow scalars
There are three ways to express flow scalars:
1.Plain text 2. Double-quoted 3.Single Quoted
Features: All flow scalars can span multiple lines; [line breaks](https://yaml.org/spec/1.2/spec.html#line break//) are always [folded](https://yaml.org/spec/1.2/spec.html#line folding//).
Plain Text
1 | # Multi-line Flow Scalars |
Quoted Scalars
The double-quoted style provides [escape sequences](https://yaml.org/spec/1.2/spec.html#escaping/in double-quoted scalars/). The single-quoted style is useful when [escaping](https://yaml.org/spec/1.2/spec.html#escaping/in double-quoted scalars/) is not needed.
双引号用于强制类型转换??
For examples:
1 | # Quoted Scalars |
Tags
In YAML, untagged nodes are given a type depending on the application. The examples in this specification generally use the seq
, map
and str
types from the fail safe schema. A few examples also use the int
, float
, and null
types from the JSON schema. The repository includes additional types such as binary
, omap
, set
and others.
1 | # integers |
Explicit typing is denoted with a tag using the exclamation point ([“**!
**”](https://yaml.org/spec/1.2/spec.html#! tag indicator//)) symbol. Global tags are URIs and may be specified in a tag shorthand notation using a handle. Application-specific local tags may also be used.
1 | # Various Explicit Tags |