TOML
Table of Contents
1. TOML 简介
TOML 是 Tom's Obvious Minimal Language 的缩写,它是一种配置文件格式。TOML 文件看起来和 INI 格式有点像,不过 INI 格式没有统一的标准,而 TOML 有完整的标准定义。
TOML 的规范文档:https://toml.io/en/v1.0.0
2. TOML 和 JSON 的比较
TOML 和 JSON 之间是可以相互转换的(节 2.1 中有两个例外情况)。下面例子展示了这两种格式的对应关系:
TOML | JSON |
# This is a TOML document. title = "TOML Example" # String [owner] name = "Tom Preston-Werner" dob = 1979-05-27T07:32:00-08:00 # Datatime [database] server = "192.168.1.1" ports = [ 8000, 8001, 8002 ] # Array connection_max = 5000 # Integer enabled = true # Boolean temp_targets = { cpu = 79.5, case = 72.0 } [servers] [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10" [clients] data = [ ["gamma", "delta"], [1, 2] ] |
{ "title": "TOML Example", "owner": { "dob": { "$__toml_private_datetime": "1979-05-27T07:32:00-08:00" }, "name": "Tom Preston-Werner" }, "database": { "server": "192.168.1.1", "ports": [ 8000, 8001, 8002 ], "connection_max": 5000, "enabled": true, "temp_targets": { "case": 72.0, "cpu": 79.5 } }, "servers": { "alpha": { "dc": "eqdc10", "ip": "10.0.0.1" }, "beta": { "dc": "eqdc10", "ip": "10.0.0.2" } }, "clients": { "data": [ ["gamma", "delta"], [1, 2] ] } } |
要进行 TOML 和 JSON 之间的相互转换,可以试下这个在线工具:https://pseitz.github.io/toml-to-json-online-converter/
2.1. TOML 和 JSON 表达能力的区别(TOML 原生支持时间)
- TOML 原生支持日期和时间(参考 Date-Time),JSON 不原生支持日期和时间;
- TOML 最外层总是 Table(不能是数组);JSON 最外层可以是数组。如
["a", "b"]
是合法的 JSON,但它没有对应的 TOML 格式。
2.2. 三种 Key
TOML 中有三种 Key:
- Bare keys,不使用引号,它只能包含 A-Za-z0-9_- 中的字符;
- Quoted keys,使用双引号(或单引号)包围,如果一个 Key 的名字中含有除 A-Za-z0-9_- 以外的字符,则必须使用双引号(或单引号)包围;
- Dotted keys,使用点作为不同层级的分隔符。
下面的 TOML 包含了这三种 Key:
name = "Orange" # name 是 Bare key "key 1" = "value 1" # "key 1" 是 Quoted key,由于其中有空格,必须使用引号 physical.color = "orange" # physical.color 是 Dotted key,表示两个不同的层级 physical.shape = "round" site."google.com" = true # 既有 Dotted key,又有 Quoted key
上面 TOML 格式对应的 JSON 格式为:
{ "name": "Orange", "key 1": "value 1", "physical": { "color": "orange", "shape": "round" }, "site": { "google.com": true } }
2.3. 字符串和多行字符串
双引号和单引号都可以表示字符串,它们的区别在于:双引号会转义,而单引号是 Literal String,不转义。
三个双引号或三个单引号都可以表示多行字符串(区别也只在于是否转义):
str1 = """ Roses are\n red Violets are blue """ lines = ''' The first newline is trimmed in raw strings. All other \nwhitespace is preserved. '''
上面 TOML 格式对应的 JSON 格式为:
{ "str1": "Roses are\n red\n Violets are blue\n", "lines": "The first newline is\ntrimmed in raw strings.\n All other \\nwhitespace\n is preserved.\n" }
2.4. Inline Table
TOML 中, {
和 }
表示 Inline Table,这是一种简洁的语法,如:
[database] server = "192.168.1.1" temp_targets = { cpu = 79.5, case = 72.0 } # Inline Table
上面的 TOML 格式等价于下面 TOML 格式:
[database] server = "192.168.1.1" temp_targets.cpu = 79.5 # 展开写的话,用点分开即可 temp_targets.case = 72.0
它们对应的 JSON 格式为:
{ "database": { "server": "192.168.1.1", "temp_targets": { "case": 72.0, "cpu": 79.5 } } }
2.5. Array of Tables
TOML 中, [[
和 ]]
表示 Table 的数组。如:
[[products]] name = "Hammer" sku = 738594937 [[products]] # empty table within the array [[products]] name = "Nail" sku = 284758393 color = "gray"
上面 TOML 格式对应的 JSON 格式为:
{ "products": [ { "name": "Hammer", "sku": 738594937 }, { }, { "name": "Nail", "sku": 284758393, "color": "gray" } ] }