toml
简介
配置文件是一种非常基础的文件格式,但远没有数据文件格式(如 SQLite
)、文档文件格式(如 Markdown
)、编程语言(如 JavaScript
)、甚至二进制文件格式(如 PNG
)需求那么复杂。
只要严谨但不严苛、支持必要的数据类型和嵌套,又易于人类手工直接阅读和编辑就可以了。
但就是这样一种广泛需要而又简单的应用场景,却反而长期以来一直没有一种足够好的文件格式。
基础语法
注释写法
# 这是一个单独行的注释
value = 'value' # 这是一个行尾的注释
数字
整数、浮点数、无穷甚至非数都是支持的。你可以用科学计数法甚至千分符。
[number]
int = 1
float = 1.5
- 转换成
json
{
"number":{
"int":1,
"float":1.5
}
}
字符串
[user]
name = "ccvb"
ip = '10.10.0.1'
desc = '''
大家好,这是我的自我介绍可支持换行'''
profile = 'C:\Users\nodejs\templates\profile.proj'
- 转换成
json
{
"user":{
"name":"ccvb",
"ip":"10.10.0.1"
"profile":"C:\\Users\\nodejs\\templates\\profile.proj"
}
}
布尔值
DEBUG = true
- 转换成
json
{
"DEBUG":true
}
对象写法
[userInfo]
name = 'ccvb1'
id = 1
profile = "C:\Users\nodejs\templates\ccvb1.proj"
- 转换成
json
{
userInfo:{
"name":"ccvb",
}
}
数组
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" }
]
对象数组
[[products]]
name = "Hammer"
sku = 738594937
[[products]] # 数组里的空表
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
- 转换成
json
{
"products": [
{ "name": "Hammer", "sku": 738594937 },
{ },
{ "name": "Nail", "sku": 284758393, "color": "gray" }
]
}