TOML的由來

轉自:http://mlworks.cn/posts/introduction-to-toml/

配置文件的使用由來已久,從.ini、XML、JSON、YAML再到TOML,語言的表達能力越來越強,同時書寫便捷性也在不斷提升。 TOML是前GitHub CEO, Tom Preston-Werner,於2013年創建的語言,其目標是成爲一個小規模的易於使用的語義化配置文件格式。TOML被設計爲可以無二義性的轉換爲一個哈希表(Hash table)。

例子

# 這是一個TOML文件

title = "TOML Example"

[owner]
name = "Lance Uppercut"
dob = 1979-05-27T07:32:00-08:00 # 日期是一等公民

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

[servers]
  #你可以使用空格、製表符進行縮進,或者根本不縮進。TOML不關心縮進。
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"

  [servers.beta]
  ip = "10.0.0.2"
  dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

# 數組內可以混入換行符
hosts = [
  "alpha",
  "omega"
]

規範

  • TOML是大小寫敏感的
  • TOML文件必須是UTF8編碼的
  • 空白符可以是製表符(0x09)或空格(0x20)
  • 換行符可以是 LF (0x0A) 或 CRLF (0x0D0A)

TOML仍在不斷完善,目前的版本0.4.0,下面是最新的規範。

註釋

使用#來表示註釋開始,至當前行尾結束。

# I am a comment. Hear me roar. Roar.
key = "value" # Yeah, you can do this.

字符串

TOML中有4種字符串表示方法:基本、多行-基本、字面量、多行-字面量。所有字符串必須是合法的UTF8字符。

基本字符串由雙引號包裹,所有Unicode字符均可出現,除了雙引號、反斜線、控制字符(U+0000 to U+001F)需要轉義。

str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."

常用的轉義序列:

\b         - backspace       (U+0008)
\t         - tab             (U+0009)
\n         - linefeed        (U+000A)
\f         - form feed       (U+000C)
\r         - carriage return (U+000D)
\"         - quote           (U+0022)
\\         - backslash       (U+005C)
\uXXXX     - unicode         (U+XXXX)
\UXXXXXXXX - unicode         (U+XXXXXXXX)

多行-基本字符串由三個雙引號包裹,除了分隔符開始的換行外,字符串內的換行將被保留。

str1 = """
Roses are red
Violets are blue"""

TOML解析器可以將其翻譯爲平臺相關的字符串,如

# Unix上,上述字符串等同於
str2 = "Roses are red\nViolets are blue"

# Windows上,上述字符串等同於
str3 = "Roses are red\r\nViolets are blue"

多行-基本字符串中可以在行尾使用\來忽略其後的所有(換行符和空白符)直到第一個非空白符。

# 以下字符串等價
str1 = "The quick brown fox jumps over the lazy dog."

str2 = """
The quick brown \


  fox jumps over \
    the lazy dog."""

key3 = """\
       The quick brown \
       fox jumps over \
       the lazy dog.\
       """

字面量字符串由單引號包裹,其內不允許轉義,因此可以方便的表示基本字符串中需要轉義的內容。

# What you see is what you get.
winpath  = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
quoted   = 'Tom "Dubs" Preston-Werner'
regex    = '<\i\c*\s*>'

多行-字面量字符串與多行-基本字符串類似。

整數

int1 = +99
int2 = 42
int3 = 0
int4 = -17

爲了增加可讀性,整數可以使用_分隔。每個_必須被至少一個數字環繞。

不被允許的表達:前置0,2、8、16進制、無窮、NaN。 整數的範圍是64bit signed long類型的範圍。

浮點數

# fractional
flt1 = +1.0
flt2 = 3.1415
flt3 = -0.01

# exponent
flt4 = 5e+22
flt5 = 1e6
flt6 = -2E-2

# both
flt7 = 6.626e-34

浮點數的範圍是64 bit double類型的範圍。

布爾值

小寫的true或false。

bool1 = true
bool2 = false

日期時間

使用RFC 3339描述的時間格式

date1 = 1979-05-27T07:32:00Z
date2 = 1979-05-27T00:32:00-07:00
date3 = 1979-05-27T00:32:00.999999-07:00

數組

數組使用方括號包裹。空格會被忽略,包括換行符。元素使用逗號分隔。注意,不允許混用數據類型(所有的字符串類型均爲同一類型)。

arr1 = [ 1, 2, 3 ]
arr2 = [ "red", "yellow", "green" ]
arr3 = [ [ 1, 2 ], [3, 4, 5] ]
arr4 = [ "all", 'strings', """are the same""", '''type'''] # this is ok
arr5 = [ [ 1, 2 ], ["a", "b", "c"] ] # this is ok
arr6 = [ 1, 2.0 ] # note: this is NOT ok

表格

表格葉稱爲哈希表或字典,用來存儲鍵值對。表格名由方括號包裹,且自成一行。

[table]

表格名下,直到下一個表格名或文件尾,均爲當前表格的內容。

[table]
key = "value"
bare_key = "value"
bare-key = "value"

"127.0.0.1" = "value"
"character encoding" = "value"
"ʎǝʞ" = "value"

表格可以嵌套,即表格中某個鍵的值可以爲表格。

[dog]
onekey = onevalue

[dog.tater]
type = "pug"

等價於

{
	"dog": { 
		"onekey":"onevalue",
		"tater": { "type": "pug" } 
	} 
}

如果你不想的話,你不用聲明所有的父表。TOML 知道該如何處理。

# [x] 你
# [x.y] 不需要
# [x.y.z] 這些
[x.y.z.w] # 可以直接寫

表格數組

[[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" }
  ]
}

詳細信息可以參考toml-lang/toml at github

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章