加载中...

Yaml


其中 Y=yaml

源代码下载: learnyaml-cn.yaml

YAML是一个数据序列化语言,被设计成人类直接可写可读的。

它是JSON的严格超集,增加了语法显著换行符和缩进,就像Python。但和Python不一样, YAML根本不容许文字制表符。

  1. # YAML中的注解看起来像这样。
  2. ################
  3. # 标量类型 #
  4. ################
  5. # 我们的根对象 (它们在整个文件里延续) 将会是一个地图,
  6. # 它等价于在别的语言里的一个字典,哈西表或对象。
  7. key: value
  8. another_key: Another value goes here.
  9. a_number_value: 100
  10. scientific_notation: 1e+12
  11. boolean: true
  12. null_value: null
  13. key with spaces: value
  14. # 注意到字符串不需要被引用。但是,它们可以被引用。
  15. "Keys can be quoted too.": "Useful if you want to put a ':' in your key."
  16. # 多行字符串既可以写成像一个'文字块'(使用 |),
  17. # 或像一个'折叠块'(使用 '>')。
  18. literal_block: |
  19. This entire block of text will be the value of the 'literal_block' key,
  20. with line breaks being preserved.
  21. The literal continues until de-dented, and the leading indentation is
  22. stripped.
  23. Any lines that are 'more-indented' keep the rest of their indentation -
  24. these lines will be indented by 4 spaces.
  25. folded_style: >
  26. This entire block of text will be the value of 'folded_style', but this
  27. time, all newlines will be replaced with a single space.
  28. Blank lines, like above, are converted to a newline character.
  29. 'More-indented' lines keep their newlines, too -
  30. this text will appear over two lines.
  31. ####################
  32. # 集合类型 #
  33. ####################
  34. # 嵌套是通过缩进完成的。
  35. a_nested_map:
  36. key: value
  37. another_key: Another Value
  38. another_nested_map:
  39. hello: hello
  40. # 地图不用有字符串键值。
  41. 0.25: a float key
  42. # 键值也可以是多行对象,用?表明键值的开始。
  43. ? |
  44. This is a key
  45. that has multiple lines
  46. : and this is its value
  47. # YAML也容许键值是集合类型,但是很多语言将会抱怨。
  48. # 序列 (等价于表或数组) 看起来像这样:
  49. a_sequence:
  50. - Item 1
  51. - Item 2
  52. - 0.5 # 序列可以包含不同类型。
  53. - Item 4
  54. - key: value
  55. another_key: another_value
  56. -
  57. - This is a sequence
  58. - inside another sequence
  59. # 因为YAML是JSON的超集,你也可以写JSON风格的地图和序列:
  60. json_map: {"key": "value"}
  61. json_seq: [3, 2, 1, "takeoff"]
  62. #######################
  63. # 其余的YAML特点 #
  64. #######################
  65. # YAML还有一个方便的特点叫'锚',它让你简单地在整个文件里重复内容。
  66. # 两个键值将会有相同的值:
  67. anchored_content: &anchor_name This string will appear as the value of two keys.
  68. other_anchor: *anchor_name
  69. # YAML还有标签,你可以用它显示地声明类型。
  70. explicit_string: !!str 0.5
  71. # 一些解析器实现特定语言的标签,就像这个为了Python的复数类型。
  72. python_complex_number: !!python/complex 1+2j
  73. ####################
  74. # 其余的YAML类型 #
  75. ####################
  76. # 字符串和数字不是仅有的YAML可以理解的标量。
  77. # ISO 格式的日期和日期时间文字也是可以被解析的。
  78. datetime: 2001-12-15T02:59:43.1Z
  79. datetime_with_spaces: 2001-12-14 21:59:43.10 -5
  80. date: 2002-12-14
  81. # 这个!!binary标签表明一个字符串实际上是一个二进制blob的base64编码表示。
  82. gif_file: !!binary |
  83. R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
  84. OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
  85. +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
  86. AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
  87. # YAML还有一个集合类型,它看起来像这样:
  88. set:
  89. ? item1
  90. ? item2
  91. ? item3
  92. # 像Python一样,集合仅是有null数值的地图;上面的集合等价于:
  93. set2:
  94. item1: null
  95. item2: null
  96. item3: null

还没有评论.