TOML 配置文件

规格

  • TOML 是大小写敏感的
  • TOML 文件必须是合法的 UTF-8 编码的 Unicode 文档
  • 空白是指制表符(0x09)或空格(0x20)
  • 换行是指 LF(0x0A)或 CRLF(0x0D 0x0A)
  • 使用 # 符号作为注释

键值对

TOML 文档最基本的构成区块是键值对, 键名和键值周围的空白会被忽略

多次定义同一个key是非法的

1
key = "value"

点分隔符

1
2
3
4
name = "Orange"
physical.color = "orange"
physical.shape = "round"
site."google.com" = true  # 引号内的点不会作为分隔符

等价于

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    "name": "Orange",
    "physical": {
        "color": "orange",
        "shape": "round"
    },
    "site": {
        "google.com": true
    }
}

表(也被称为哈希表或字典)是键值对的集合 它们由表头定义,连同方括号作为单独的行出现 表头不同于数组,因为数组只有值 缩进会被忽略

从表头下方, 直至下一个表头或者文件结束, 都是这个表的键值对

1
2
3
4
5
6
7
[table-1]
key1 = "some string"
key2 = 123

[table-2]
key1 = "another string"
key2 = 456

等价于

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    "table-1": {
        "key1": "some string",
        "key2": 123
    },
    "table-2": {
        "key1": "another string",
        "key2": 456
    }
}

表数组

1
2
3
4
5
6
7
8
[[products]]
name = "Hammer"
sku = 738594937
[[products]]  # 数组里的空表
[[products]]
name = "Nail"
sku = 284758393
color = "gray"

等价于

1
2
3
4
5
6
7
{
    "products": [
        { "name": "Hammer", "sku": 738594937 },
        { },
        { "name": "Nail", "sku": 284758393, "color": "gray" }
    ]
}

TOML to JSON