文件格式-YAML

Yaml

一、简介

(一)YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。
(二)基本规则
  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进时不允许使用Tab键,只允许使用空格。
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
(三)支持的数据结构
  • 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
  • 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
  • 纯量(scalars):单个的、不可再分的值

二、对象

animal: pets
{ animal: 'pets' }

hash: { name: Steve, foo: bar } 
{ hash: { name: 'Steve', foo: 'bar' } }

三、数组

- Cat
- Dog
- Goldfish
[ 'Cat', 'Dog', 'Goldfish' ]

-
 - Cat
 - Dog
 - Goldfish
[ [ 'Cat', 'Dog', 'Goldfish' ] ]

animal: [Cat, Dog]
{ animal: [ 'Cat', 'Dog' ] }

四、复合结构

languages:
 - Ruby
 - Perl
 - Python 
websites:
 YAML: yaml.org 
 Ruby: ruby-lang.org 
 Python: python.org 
 Perl: use.perl.org 
{ languages: [ 'Ruby', 'Perl', 'Python' ],
  websites: 
   { YAML: 'yaml.org',
     Ruby: 'ruby-lang.org',
     Python: 'python.org',
     Perl: 'use.perl.org' } }

五、纯量

纯量是最基本的、不可再分的值。以下数据类型都属于 JavaScript 的纯量。
  • 字符串
  • 布尔值
  • 整数
  • 浮点数
  • Null
  • 时间
  • 日期

number: 12.30
{ number: 12.30 }

parent: ~ 
{ parent: null }

date: 1976-07-31
{ date: new Date('1976-07-31') }

使用!!相当于强行转换
e: !!str 123
f: !!str true
{ e: '123', f: 'true' }

六、字符串

str: 这是一行字符串
{ str: '这是一行字符串' }

str: 'labor''s day'
{ str: 'labor\'s day' }

str: 这是一段
  多行
  字符串
{ str: '这是一段 多行 字符串' }

this: |
  Foo
  Bar
that: >
  Foo
  Bar
{ this: 'Foo\nBar\n', that: 'Foo Bar\n' }

s1: |
  Foo

s2: |+
  Foo

s3: |-
  Foo
{ s1: 'Foo\n', s2: 'Foo\n\n\n', s3: 'Foo' }

七、引用

defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  <<: *defaults

test:
  database: myapp_test
  <<: *defaults
defaults:
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  adapter:  postgres
  host:     localhost

test:
  database: myapp_test
  adapter:  postgres
  host:     localhost

- &showell Steve 
- Clark 
- Brian 
- Oren 
- *showell
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]

版权声明:本文为weixin_40304570原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。