TOML
- TOML
- Tom's Obvious, Minimal Language
整个 TOML 文件是一个哈希表。
注释
# 这是一个全行注释
key = "value" # 这是一个行末注释
another = "# 这不是一个注释"
键值对
TOML 文档的基本构成块是键值对。
name = "Orange"
physical.color = "orange"
physical.shape = "round"
site."google.com" = true
等价于如下的 JSON:
{
"name": "Orange",
"physical": {
"color": "orange",
"shape": "round"
},
"site": {
"google.com": true
}
}
字符串
多行字符串由三个引号包裹,允许折行。
- 紧随开头引号的换行会被去除
- 其它空白和换行会被原样保留
str1 = """
Roses are red
Violets are blue"""
数组
- 子元素由逗号分隔
- 空白会被忽略
- 可以混合不同类型的值
- 数组可以跨行
- 数组的最后一个值后面可以有终逗号(也称为尾逗号)
- 值、逗号、结束括号前可以存在任意数量的换行和注释
- 数组值和逗号之间的缩进被作为空白对待而被忽略
integers = [ 1, 2, 3 ]
colors = [ "红", "黄", "绿" ]
nested_array_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
string_array = [ "所有的", '字符串', """是相同的""", '''类型''' ]
# 允许混合类型的数组
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
contributors = [
"Foo Bar <foo@example.com>",
{ name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" }
]
# 允许任意数量的换行
integers2 = [
1, 2, 3
]
integers3 = [
1,
2, # 这是可以的
]
表(重要)
表是键值对的集合。
- 表由
[表头]定义,[表头]连同方括号作为单独的行出现 - 在
[表头]下方,直至下一个[表头]或文件结束,都是这个表的键值对 - 表不保证键值对的声明顺序
[table-1]
key1 = "some string"
key2 = 123
[table-2]
key1 = "another string"
key2 = 456
等价于:
{
"table-1": {
"key1": "some string",
"key2": 123
},
"table-2": {
"key1": "another string",
"key2": 456
}
}
表数组(重要)
- 第一个
[[表头]]定义了表数组及首个表元素,而后续的每个[[表头]]则在表数组中创建一个新的表元素 - 表元素按出现顺序插入表数组
[[products]]
name = "Hammer"
sku = 738594937
[[products]] # 空表
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
等价于:
{
"products": [
{ "name": "Hammer", "sku": 738594937 },
{ },
{ "name": "Nail", "sku": 284758393, "color": "gray" }
]
}

浙公网安备 33010602011771号