深刻對比TOML,JSON和YAML

 

坦率地說,在我開始與Hugo TOML合做以前,我感到羞恥是一個須要發現的新領域,但我對YAML和JSON很是熟悉。本文將幫助您瞭解如何經過不一樣的數據格式構建數據。javascript

 

在Hugo中,您能夠將全部這三種數據格式用於配置,前置事項和自定義數據,但TOML是用於整個項目的推薦格式。首先我想簡單介紹一下每種數據格式,而後再進入規範和比較。前端

TOML(Tom's Obvious,Minimal Language)

TOML顯然是由Tom - Tom Preston-Werner編寫的 - 確切地說。這是一個在麻省理工學院受權的開源項目,目前在Github上有超過5k星2013年3月發佈的第一個TOML版本,使TOML成爲三個標準的年輕人。java

TOML的目標是成爲最小的配置文件格式,因爲精確的語義,這種格式易於閱讀。TOML被設計爲無歧義地映射到散列表。TOML應該很容易用各類語言來解析數據結構。git

關於TOML語法的簡短事實github

  • TOML區分大小寫。
  • TOML文件只能包含UTF-8編碼的Unicode字符。
  • 空格表示製表符(0x09)或空格(0x20)。
  • 換行符表示LF(0x0A)或CRLF(0x0D0A)。

要在前面的問題中使用TOML,你須要將它封裝在+++以下之間json

+++
date = "2016-12-14T21:27:05.454Z" publishdate = "2016-12-14T21:27:05.454Z" title = "Deep dive into TOML, JSON and YAML" tags = ["toml","yaml","json", "front matter"] type = "article" [amp] elements = [] [article] lead = "Lorem ipsum." category = "frontmatter" related = [] [sitemap] changefreq = "monthly" priority = 0.5 filename = "sitemap.xml" +++ 

YAML(不是標記語言)

YAML是一種普遍使用的語言,用於跨不一樣語言和框架的配置文件。YAML的建立者和維護者是Clark C. Evans,起初是SML-DEV,專一於簡化XML的XML人員名單幫助生成Common XML,這是一個功能強大的XML子集,爲XML建立了數據序列化的替代方案,特別是與Python ,Perl和Ruby。該項目始於2001年,第一個1.0版本於2009年1月由Oren Ben-Kiki,Clark Evans和Brian Ingerson發佈。自2009年以來,當前版本1.2正在使用中。數組

關於YAML語法的簡短事實瀏覽器

  • .yml文件以' - '開頭,標記文檔的開始
  • 鍵值對由冒號分隔
  • 列表以連字符開頭
  • YAML使用具備一個或多個空格的縮進來描述嵌套集合

要在前面的問題中使用YAML,你須要將它包裹在之間---服務器

---
date: '2016-12-14T21:27:05.454Z' publishdate: '2016-12-14T21:27:05.454Z' title: Deep dive into TOML, JSON and YAML tags: - toml - yaml - json - front matter type: article amp: elements: [] article: lead: Lorem ipsum. category: frontmatter related: [] sitemap: changefreq: monthly priority: 0.5 filename: sitemap.xml --- 

JSON(JavaScript對象表示法)

JSON是一種輕量級的數據交換格式。因爲JavaScript和大多數Serverside語言自己支持JSON,所以JSON普遍用於Web環境中瀏覽器和服務器之間的API通訊。在21世紀初,Douglas Crockford引入了數據格式JSON的第一個規範。當前版本由ECMA-404於2013年10月指定。數據結構

有關JSON語法的簡短事實

  • 數據存儲在名稱/值對中
  • 記錄用逗號分隔。沒有如下屬性的尾隨逗號是不容許的。
  • 雙引號包裝屬性名稱和字符串。單引號是不容許的。

因爲JSON包裹在兩個花括號中,{}所以在Hugo的前端內容中沒有必要使用特殊的包裝:

{ "date" : "2016-12-14T21:27:05.454Z", "publishdate" : "2016-12-14T21:27:05.454Z", "title" : "Deep dive into TOML, JSON and YAML", "tags" : ["toml","yaml","json", "front matter"], "type" : "article", "amp" : { "elements" : [] }, "article" : { "lead" : "Lorem ipsum.", "category" : "frontmatter", "related" : [] }, "sitemap" : { "changefreq" : "monthly", "priority" : 0.5, "filename" : "sitemap.xml" } } 

TOML,YAML和JSON之間的語法差別

如今讓咱們來看看最多見用例中的語法和功能集差別。

字符串

任何格式都支持Strings。惟一的區別在於,JSON不支持多行字符串。

TOML

key = "String Value" multiline = """\ The quick brown \ fox jumps over \ the lazy dog.\ """ 

YAML

key : String Value multilinePreservedLinebreaks: | L1 - The quick brown L2 - fox jumps over L3 - the lazy dog. multilineReplaceLinebreaksWithWhitespace: > This sentence ist just too long to keep it on the same line. 

JSON

{ "key" : "String Value" } 

對象/哈希表/集合

TOML中的表格幾乎與YAML中的JSON和Collections中的對象相同。要訪問Hugo模板中的集合,請按照.相似方式導航{{ .Params.objectkey.subkey }}

TOML

[table_key] property = "Value" secondProperty = "2nd Value" [alternative.direct] access = "results in alternative.direct.access for this value" alternativeCalledInlineTable = { property = "Value", "etc" = "You got it." } 

YAML

objectKey: property: Value secondProperty: 2nd Value alternative: { subkey: 5.0, another: 123 } 

JSON

{ "objectKey" : { "property" : "Value", "secondProperty" : "2nd Value" } } 

數組/列表

數組或列表受全部語言支持。

TOML

fruits = [ "Apple", "Banana", "Strawberry" ] formats = [ "YAML", "JSON", "TOML" ] 

YAML

fruits: - Apple - Banana - Strawberry formats: [ YAML, JSON, TOML ] 

JSON

{ "fruits": ["Apple","Banana","Strawberry"], "formats": [ "YAML", "JSON", "TOML" ] } 

爲了擴展這些例子,咱們能夠建立一個對象/表/集合的列表,就像這樣:

TOML

[[fruits]] name = "Apple" weight = 600 [[fruits]] name = "Banana" weight = 300 [[fruits]] name = "Strawberry" weight = 40 

YAML

fruits: - name: Apple weight: 600 - name: Banana weight: 300 - name: Strawberry weight: 40 

JSON

{ "fruits": [ { "name" : "Apple", "weight" : 600 }, { "name" : "Banana", "weight" : 300 }, { "name" : "Strawberry", "weight" : 40 } ] } 

上面的全部示例都會生成一個能夠{{ range .Params.fruits }}<strong>{{ .name }}</strong> - Weight: {{ .weight }}{{ end }}在Hugo模板文件中迭代的列表

我認爲你如今對數組和表格是如何協同工做有了很好的理解; 讓咱們再次擴展以得到完整的概述。

TOML

[[fruits]] name = "Apple" weight = 600 [fruit.physical] color = "red" shape = "round" [[fruit.variety]] name = "red delicious" [[fruit.variety]] name = "granny smith" [[fruits]] name = "Banana" weight = 300 [fruit.physical] color = "yellow" shape = "curved" [[fruit.variety]] name = "plantain" [[fruits]] name = "Strawberry" weight = 40 [fruit.physical] color = "red" shape = "kind-of-oval" [[fruit.variety]] name = "the-good-one" 

YAML

fruits: - name: Apple weight: 600 physical: color: red shape: round variety: - name: red delicious - name: granny smith - name: Banana weight: 300 physical: color: yellow shape: curved variety: - name: plantain - name: Strawberry weight: 40 physical: color: red shape: kind-of-oval variety: - name: the-good-one 

JSON

{ "fruits": [ { "name" : "Apple", "weight" : 600, "physical": { "color": "red", "shape": "round" }, "variety": [ { "name": "red delicious" }, { "name": "granny smith" } ] }, { "name" : "Banana", "weight" : 300, "physical": { "color": "yellow", "shape": "curved" }, "variety": [ { "name": "plantain" } ] }, { "name" : "Strawberry", "weight" : 40, "physical": { "color": "red", "shape": "kind-of-oval" }, "variety": [ { "name": "the-good-one" } ] } ] } 

數字(整數,浮點數,無窮大等)

全部數據結構中的數字編寫都很是類似,但功能集有所不一樣:

TOML

explicit_pos = +99
positive = 42
zero = 0
negative = -17

# For large numbers, you may use underscores to enhance readability. # Each underscore must be surrounded by at least one digit. large = 1_000 verylarge = 5_349_221 # fractional float = +1.0 float_pi = 3.1415 negative_float = -0.01 # exponent flt4 = 5e+22 flt5 = 1e6 flt6 = -2E-2 # both flt7 = 6.626e-34 

YAML

integer: 12 octal_number: 014 hexadecimal: 0xC float: 18.6 exponential: 1.2e+32 infinity: .inf 

JSON Infinity而且NaN在JSON中不受支持)

{ "integer": 12, "octal_number": 12, "hexadecimal": 12, "float": 18.6, "exponential": 1.2e+32 } 

雜項 - 日期時間,布爾,空

TOML

bool1 = true
bool2 = false

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

YAML

bool1: true bool2: false null1: null null2: ~ date_iso: 2016-12-14T21:59:43.10-05:00 # ISO-8601 date_simple: 2016-12-14 

JSON

{ "bool1": true, "bool2": false, "null1": null, "date_iso": "2016-12-14 21:59:43 -0500", "date_simple": "2016-12-14" } 

  總結:但願你們能很好地瞭解這三種數據結構之間的差別,以便使用它們中的任何一種。要簡潔同時功能強大,請用yaml, 要在不一樣語言中交換、共享數據,請用json, 想嚐鮮,用toml,還不夠的話,請用xml吧。

相關文章
相關標籤/搜索