.yml文件格式
2018-10-05 21:47 youxin 阅读(5946) 评论(0) 编辑 收藏 举报http://yaml.org/
YAML: YAML Ain't Markup Language What It Is: YAML is a human friendly data serialization standard for all programming languages.
YAML(Yet Another Markup Language)(发音 /ˈjæməl/ )
一种基于Unicode容易阅读,容易和脚本语言交互的,用来表达资料序列的编程语言。
适应场景
- 脚本语言:由于实现简单,解析成本很低,YAML 特别适合在脚本语言中使用
- 序列化: YAML是由宿主语言数据类型直转,的比较适合做序列化。
- 配置文件:写 YAML 要比写 XML 快得多(无需关注标签或引号),并且比 INI 文档功能更强。由于兼容性问题,不同语言间的数据流转建议不要用 YAML。
YAML 语法
- 使用空格 Space 缩进表示分层,不同层次之间的缩进可以使用不同的空格数目,但是同层元素一定左对齐,即前面空格数目相同(不能使用 Tab,各个系统 Tab对应的 Space 数目可能不同,导致层次混乱)
- ‘#’表示注释,只能单行注释,从#开始处到行尾
- 破折号后面跟一个空格(a dash and space)表示列表
- 用冒号和空格表示键值对 key: value
- 简单数据(scalars,标量数据)可以不使用引号括起来,包括字符串数据。用单引号或者双引号括起来的被当作字符串数据,在单引号或双引号中使用C风格的转义字符
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格。
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
本文介绍 YAML 的语法,以 JS-YAML 的实现为例。你可以去在线 Demo 验证下面的例子。
YAML 支持的数据结构有三种。
- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
- 纯量(scalars):单个的、不可再分的值
以下分别介绍这三种数据结构。
三、数组
一组连词线开头的行,构成一个数组。
- Cat
- Dog
- Goldfish
转为 JavaScript 如下。
[ 'Cat', 'Dog', 'Goldfish' ]
数据结构的子成员是一个数组,则可以在该项下面缩进一个空格。
-
- Cat
- Dog
- Goldfish
转为 JavaScript 如下。
[ [ 'Cat', 'Dog', 'Goldfish' ] ]
数组也可以采用行内表示法。
animal: [Cat, Dog] 必须要有空格,不然会被解释成字符串。
转为 JavaScript 如下。
{ animal: [ 'Cat', 'Dog' ] }
使用一个短横线加一个空格代表一个数组项:
hobby: - Java - LOL
当然也可以有这样的写法:
- - Java - LOL
可以简单理解为:[[Java,LOL]]
valueMapList:
- name: aaa
age: 21
- name: bbb
age: 31
{ valueMapList: [ { name: 'aaa', age: 21 }, { name: 'bbb', age: 31 } ] }
Mapping Scalars to Sequences 简单数据列表键值对
american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves
对象
使用冒号代表,格式为key: value。冒号后面要加一个空格:
key: value
可以使用缩进表示层级关系;
key: child-key: value child-key2: value2
YAML中还支持流式(flow)语法表示对象,比如上面例子可以写为:
key: {child-key: value, child-key2: value2}
较为复杂的对象格式,可以使用问号加一个空格代表一个复杂的key,配合一个冒号加一个空格代表一个value:
? - complexkey1 - complexkey2 : - complexvalue1 - complexvalue2
意思即对象的属性是一个数组[complexkey1,complexkey2],对应的值也是一个数组[complexvalue1,complexvalue2]
引用
锚点&
和别名*
,可以用来引用。
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
&
用来建立锚点(defaults
),<<
表示合并到当前数据,*
用来引用锚点。
--- # document start # Comments in YAML look like this. ################ # SCALAR TYPES # ################ # Our root object (which continues for the entire document) will be a map, # which is equivalent to a dictionary, hash or object in other languages. key: value another_key: Another value goes here. a_number_value: 100 scientific_notation: 1e+12 # The number 1 will be interpreted as a number, not a boolean. if you want # it to be interpreted as a boolean, use true boolean: true null_value: null key with spaces: value # Notice that strings don't need to be quoted. However, they can be. however: 'A string, enclosed in quotes.' 'Keys can be quoted too.': "Useful if you want to put a ':' in your key." single quotes: 'have ''one'' escape pattern' double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and more." # UTF-8/16/32 characters need to be encoded Superscript two: \u00B2 # Multiple-line strings can be written either as a 'literal block' (using |), # or a 'folded block' (using '>'). literal_block: | This entire block of text will be the value of the 'literal_block' key, with line breaks being preserved. The literal continues until de-dented, and the leading indentation is stripped. Any lines that are 'more-indented' keep the rest of their indentation - these lines will be indented by 4 spaces. folded_style: > This entire block of text will be the value of 'folded_style', but this time, all newlines will be replaced with a single space. Blank lines, like above, are converted to a newline character. 'More-indented' lines keep their newlines, too - this text will appear over two lines. #################### # COLLECTION TYPES # #################### # Nesting uses indentation. 2 space indent is preferred (but not required). a_nested_map: key: value another_key: Another Value another_nested_map: hello: hello # Maps don't have to have string keys. 0.25: a float key # Keys can also be complex, like multi-line objects # We use ? followed by a space to indicate the start of a complex key. ? | This is a key that has multiple lines : and this is its value # YAML also allows mapping between sequences with the complex key syntax # Some language parsers might complain # An example ? - Manchester United - Real Madrid : [2001-01-01, 2002-02-02] # Sequences (equivalent to lists or arrays) look like this # (note that the '-' counts as indentation): a_sequence: - Item 1 - Item 2 - 0.5 # sequences can contain disparate types. - Item 4 - key: value another_key: another_value - - This is a sequence - inside another sequence - - - Nested sequence indicators - can be collapsed # Since YAML is a superset of JSON, you can also write JSON-style maps and # sequences: json_map: {"key": "value"} json_seq: [3, 2, 1, "takeoff"] and quotes are optional: {key: [3, 2, 1, takeoff]} ####################### # EXTRA YAML FEATURES # ####################### # YAML also has a handy feature called 'anchors', which let you easily duplicate # content across your document. Both of these keys will have the same value: anchored_content: &anchor_name This string will appear as the value of two keys. other_anchor: *anchor_name # Anchors can be used to duplicate/inherit properties base: &base name: Everyone has same name # The regexp << is called Merge Key Language-Independent Type. It is used to # indicate that all the keys of one or more specified maps should be inserted # into the current map. foo: <<: *base age: 10 bar: <<: *base age: 20 # foo and bar would also have name: Everyone has same name # YAML also has tags, which you can use to explicitly declare types. explicit_string: !!str 0.5 # Some parsers implement language specific tags, like this one for Python's # complex number type. python_complex_number: !!python/complex 1+2j # We can also use yaml complex keys with language specific tags ? !!python/tuple [5, 7] : Fifty Seven # Would be {(5, 7): 'Fifty Seven'} in Python #################### # EXTRA YAML TYPES # #################### # Strings and numbers aren't the only scalars that YAML can understand. # ISO-formatted date and datetime literals are also parsed. datetime: 2001-12-15T02:59:43.1Z datetime_with_spaces: 2001-12-14 21:59:43.10 -5 date: 2002-12-14 # The !!binary tag indicates that a string is actually a base64-encoded # representation of a binary blob. gif_file: !!binary | R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+ +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs= # YAML also has a set type, which looks like this: set: ? item1 ? item2 ? item3 or: {item1, item2, item3} # Sets are just maps with null values; the above is equivalent to: set2: item1: null item2: null item3: null ... # document end
https://www.ruanyifeng.com/blog/2016/07/yaml.html
https://learnxinyminutes.com/docs/yaml/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2013-10-05 单次遍历,带权随机选取问题
2013-10-05 转:面试中常见的一些算法问题
2013-10-05 树状数组资料
2013-10-05 逆序数的求法
2013-10-05 全排列的hash
2013-10-05 转:2个指针位置的妙用
2013-10-05 编程珠玑I算法总结