`

yaml简介

 
阅读更多

1,介绍

  YAML是一种标记语言, 全称是Ain't Markup Language。

  YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言包括XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言另外Ingy dtNet与Oren Ben-Kiki也是这语言的共同设计者。 

 

2,特点

   YAML的可读性好 

   YAML和脚本语言的交互性好 

   YAML使用实现语言的数据类型 

   YAML有一个一致的信息模型 

   YAML易于实现 

   YAML可以基于流来处理 

   YAML表达能力强扩展性好

 

3,语法

 1)规则

   缩进yaml使用一个固定的缩进风格表示数据层结构关系,Saltstack需要每个缩进级别由两个空格组成。 一定不能使用tab键

       冒号: 每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格)

       短横线:想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分

 

       注意事项

      1),如果参数是以空格开始或结束的字符串,应使用单引号把他包进来。如果一个字符串参数包含特殊字符,也要用单引号包起来。

    e.g.

  1. bat:  
  2.  website:{  baidu: '    http://www.bai''u.com'}  

  2), 当字符串较长时,可以使用特殊字符头外加一个缩进表示长字符串,可以换行

  3),在yaml里,用on、1、true来表示true,off、0、false来表示false   

  4), 在yaml里,用#做注释

  5),  如果你在yaml文件中看到了.开头的字符串,这个字符串代表的是分类头

    e.g.

           

  1. all:  
  2.  .general:  
  3.   tax: 19.6  
  4.   
  5.  mail:  
  6.   webmaster: webmaster@example.com  

     

4,一些示例

# > 的作用,以缩进对齐来判断是否为一段文字,也就是说,一旦缩进与上一行不一致,则认为是一个新行。

# node1的例子中,第一行“Ther... door”,

#                第二行“  "Please... floor"”,

#                第三行“So...So2”

node1: >

  Ther once was a man from Darjeeling

  Who got on a bus bound for Ealing

  It said on the door

    "Please don't spit on the floor"

  So he carefully spat on the ceiling

  So2

 

# | 的作用,它表示之后的文字,每一行均为一个新行

node2: |

  Ther once was a man from Darjeeling

  Who got on a bus bound for Ealing

  It said on the door

  "Please don't spit on the floor"

  So he carefully spat on the ceiling

 

# & 的作用,它表示一个“锚点标记”,其它节点可以使用“*”或“<<: *”来引用它的值

node3: &node3

  a: 001

  b: 002

 

# * 的作用,指node4的内容与node3完全一致

node4:

  *node3

  

# <<: * 的作用,指node5的内容包含但不完全相同于node3的值

node5:

  <<: *node3

  c: 003

 

# !! 的作用,强迫转换类型。

#输出:

#{"node6"=>{

#    "a"=>#<YAML::PrivateType:0x9df6d40 @value="123", @type_id="float">,

#    "b"=>#<YAML::PrivateType:0x9df6ae8 @value="true", @type_id="str">,

#    "c"=>true

#}

#注意:c的值为布尔型。

node6:

  a: !!float 123

  b: !!str true

  c: True

 

# 二进制内容的表示

node7: !!binary |

  xxxxxxxxxxxxx

  xxxxxxxxx

  xxxxx

 

node8_value: &node8_value {id: 10000, code: item_manager, name: 项目经理}

 

#自定解析类型,YAML某Key的Value一般为Array或Hash,但如果需要将Value解析为其它的自定义类型,可以使用该方法。

#步骤:

# 1、首先定义 MyCustClass 类,如:

#    class MyCustClass

#      attr_accessor :id

#      attr_accessor :code

#      def initialize v_hash

#        @id = v_hash["id"]

#        @code = v_hash["code"]

#      end

#    end

# 2、向YAML注册解释类型,如:

#  YAML::add_domain_type("yaml.org,2002", 'MyCustClass') do |type, val|

#    MyCustClass.new(val)

#  end

# 3、OK,当YAML文件加载时,YAML将自动将“node8”的值解析为MyCustClass类型。

# 4、测试一下,x["node8"] >> #<MyCustClass:0x9df1c88 @code="item_manager", @id=10000>

#              x["node8"].code >> "item_manager"

node8: !MyCustClass 

  <<: *node8_value

 

# ? 的作用,用来明确的表示多个词汇组成的键值

# a["node9"] => {{"a"=>1, "b"=>2}=>[1, 2], "c"=>3}

node9:

  ? {a: 01, b: 02}

  : [1, 2]

 

  c: 3

 

5, 参考

http://www.yaml.org/

http://blog.chinaunix.net/uid-22776959-id-5749385.html

http://www.cnblogs.com/moonandstar08/p/6435640.html

语法跟用法可参考: http://sqycyl.iteye.com/blog/859589

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics