深入對比TOML,JSON和YAML


 

坦率地說,在我開始與Hugo TOML合作之前,我感到羞恥是一個需要發現的新領域,但我對YAML和JSON非常熟悉。本文將幫助您了解如何通過不同的數據格式構建數據。

 

在Hugo中,您可以將所有這三種數據格式用於配置,前置事項和自定義數據,但TOML是用於整個項目的推薦格式。首先我想簡單介紹一下每種數據格式,然后再進入規范和比較。

TOML(Tom's Obvious,Minimal Language)

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

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

關於TOML語法的簡短事實

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

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

+++
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吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM