TOML 备忘清单

这是 TOML 格式配置文件语法的快速参考备忘清单。

入门

介绍

TOML 是一种最小的配置文件格式,由于明显的语义而易于阅读。

示例

  1. bool = true
  2. date = 2006-05-27T07:32:00Z
  3. string = "hello"
  4. number = 42
  5. float = 3.14
  6. scientificNotation = 1e+12

注释

  1. # A single line comment example
  2. # block level comment example
  3. # 注释行 1
  4. # 注释行 2
  5. # 注释行 3

整数

  1. int1 = +42
  2. int2 = 0
  3. int3 = -21
  4. integerRange = 64

浮点数

  1. float2 = 3.1415
  2. float4 = 5e+22
  3. float7 = 6.626e-34

布尔值

  1. bool1 = true
  2. bool2 = false
  3. boolMustBeLowercase = true

时间日期

  1. date1 = 1989-05-27T07:32:00Z
  2. date2 = 1989-05-26T15:32:00-07:00
  3. date3 = 1989-05-27T07:32:00
  4. date4 = 1989-05-27
  5. time1 = 07:32:00
  6. time2 = 00:32:00.999999

字符串

  1. str1 = "I'm a string."
  2. str2 = "You can \"quote\" me."
  3. str3 = "Name\tJos\u00E9\nLoc\tSF."

See: Strings

Table

  1. [owner]
  2. name = "Tom Preston-Werner"
  3. dob = 1979-05-27T07:32:00-08:00

See: Tables

数组

  1. array1 = [1, 2, 3]
  2. array2 = ["Commas", "are", "delimiter"]
  3. array3 = [8001, 8001, 8002]

友好数组

  1. array1 = [ "Don't mix", "different", "types" ]
  2. array2 = [ [ 1.2, 2.4 ], ["all", 'strings', """are the same""", '''type'''] ]
  3. array3 = [
  4. "Whitespace", "is",
  5. "ignored"
  6. ]

TOML 字符串

多行字符串

  1. multiLineString = """
  2. Multi-line basic strings are surrounded
  3. by three quotation marks on each side
  4. and allow newlines.
  5. """

文字字符串

  1. path = 'C:\Users\nodejs\templates'
  2. path2 = '\\User\admin$\system32'
  3. quoted = 'Tom "Dubs" Preston-Werner'
  4. regex = '<\i\c*\s*>'

用单引号括起来。不允许转义。

多行文字字符串

  1. re = '''\d{2} apps is t[wo]o many'''
  2. lines = '''
  3. The first newline is
  4. trimmed in raw strings.
  5. All other whitespace
  6. is preserved.
  7. '''

TOML Tables

基本的

  1. [name]
  2. foo = 1
  3. bar = 2

foobar 是名为name 的表中的键

嵌套

  1. [table1]
  2. foo = "bar"
  3. [table1.nested_table]
  4. baz = "bat"

类数组

  1. [[comments]]
  2. author = "Nate"
  3. text = "Great Article!"
  4. [[comments]]
  5. author = "Anonymous"
  6. text = "Love it!"

↓ 等效的 JSON

  1. {
  2. "comments" : [
  3. {
  4. "author" : "Nate",
  5. "text" : "Great Article!"
  6. },
  7. {
  8. "author" : "Anonymous",
  9. "text" : "Love It!"
  10. }
  11. ]
  12. }

点分隔

  1. [dog."tater.man"]
  2. type = "pug"

↓ 等效的 JSON

  1. {
  2. "dog": {
  3. "tater.man": {
  4. "type": "pug"
  5. }
  6. }
  7. }

多嵌套

  1. [foo.bar.baz]
  2. bat = "hi"

↓ 等效的 JSON

  1. {
  2. "foo" : {
  3. "bar" : {
  4. "baz" : {
  5. "bat" : "hi"
  6. }
  7. }
  8. }
  9. }

忽略空格

  1. [a.b.c] # this is best practice
  2. [ d.e.f ] # same as [d.e.f]
  3. [ g . h .i ] # same as [g.h.i]
  4. [ j . "ʞ" .'l' ] # same as [j."ʞ".'l']

Inline Table

  1. name = { first = "Tom", last = "Preston-Werner" }
  2. point = { x = 1, y = 2 }
  3. animal = { type.name = "pug" }

另见