yaml开始使用yaml

备注

YAML是“ Y AML A in't M arkup L anguage”的递归缩写。它是适用于所有编程语言的人类可读数据序列化标准。

版本

发布日期
1.0 2004-01-29
1.1 2005-01-18
1.2 2009-10-01

基本的Yaml语法

YAML是一种基于文本的格式,允许在层次结构中存储结构化数据。 YAML旨在以人为和机器可读,并且开销最小。 YAML规范可以在yaml.org找到。还有一张参考卡

注释以#开头,直到换行,注释必须通过空格与其他标记分开。空格不是空闲的,缩进必须是空格,而不是制表符。 YAML会考虑在其中包含前缀比父键更多空格的行。此外,所有行必须以相同数量的空格作为前缀,以属于同一个地图。

YAML具有序列和映射作为集合类型,两者都可以用流和块样式表示。

YAML中的一系列标量字符串如下所示:

[ one, two, three ]   # flow style

# or block style

- one
- two
- three

映射由键/值对组成:

index: 4  # block style
name: nali

# or 

{ index: 4, name: nali }   # flow style

# or nested (equivalent of { level: { one: { two: fun } } }):

level:

  one:

    two: fun

基本的YAML类型

integer: 25
string: "25"
float: 25.0
boolean: true
null type: null

YAML顺序数据

相同的列表级别:

- Cat
- Dog
- Goldfish

嵌套列表:

-
 - Cat
 - Dog
 - Goldfish

评论

# This comment occupies a whole line
- some item # This comment succeeds content of a line
- http://example.com/#nocomment
- "This # does not introduce a comment."
- |
    This is a block scalar.
    A # inside it does not introduce a comment.
   # unless it is less indented than the first line (this is one)

请注意,对于#引入注释,它必须要么

  • 发生在一行的开头,或
  • 之前是空格。

#必须始终后跟空格。 #里面引用标量不会开始评论。 #可能会在块标量的末尾引入注释,但因此,它必须比块标量的基本缩进(通常由其第一个非空行的缩进确定)缩进。

块样式映射

使用隐式键:

key: value
another key:
  - some
  - more
  - values
[1, 2, 3]: last value, which has a flow style key

使用隐式和显式键:

? key
: value
another key:
  - some
  - more
  - values
? [1, 2, 3]
: last value, which has a flow style key

keyanother key[1, 2, 3]是相同映射的键,尽管它们使用不同的键样式。


嵌套映射:

first level:
  second level:
    ? third level
    :
      forth level: value of implicit key
    ? third level, second key
    : value of explicit key
  ?
    mapping as: key of
    another: mapping
  : scalar value of mapping key
first level, second key:
  last value

在多行上拆分文本字符串

- Without quotes:
   You can just
   split a long piece of text like this.
- With quotes:
    "[But be careful:
     if you \"need\" punctuation, put double quotes around it. You can ev\
     en split without spaces by using backslashes."
- Or single quotes:
    'This works
     but isn''t as flexible'
- If you want to keep those new line characters: | 
    Then do
    it this way with 
    a pipe (|) character. (This string has three \n characters)
- Or you can have just the one final new line: >
    This string has
    just one \n character, at the very end.
- Block indicators:
    Look up >-, >+, |- and |+ for fine tuning.

逃脱角色

YAML支持三种转义表示法:

  1. 实体逃脱

    一个。空间:“ ”

    湾冒号:“:”

    C。 &符号:“&”

  2. Unicode Escapes

    一个。空间:“\ u0020”

    湾单引号:“\ u0027”

    C。双引号:“\ u0022”

  3. 引用逃脱

    一个。单引号中的双引号:'是'我总是撒谎“一个真实的陈述?”

    湾嵌套双引号:“她说,”我退出了“”

    C。嵌套的单引号:'他无言以对:'''