深⼊对⽐TOML,JSON和YAML
坦率地说,在我开始与Hugo TOML合作之前,我感到羞耻是⼀个需要发现的新领域,但我对YAML和JSON⾮常熟悉。本⽂将帮助您了解如何通过不同的数据格式构建数据。
在Hugo中,您可以将所有这三种数据格式⽤于配置,前置事项和⾃定义数据,但TOML是⽤于整个项⽬的推荐格式。⾸先我想简单介绍⼀下每种数据格式,然后再进⼊规范和⽐较。
TOML(Tom's Obvious,Minimal Language)
显然是由Tom - Tom Preston-Werner编写的 - 确切地说。这是⼀个在⿇省理⼯学院授权的开源项⽬,⽬前有超过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 = "l"
+++
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: l
---
JSON(JavaScript对象表⽰法)
是⼀种轻量级的数据交换格式。由于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" : "l"
}
}
need you now
TOML,YAML和JSON之间的语法差异
现在让我们来看看最常见⽤例中的语法和功能集差异。
字符串
任何格式都⽀持Strings。唯⼀的区别在于,JSON不⽀持多⾏字符串。
TOML
key = "String Value"
multiline = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""
key : String Value
multilinePrervedLinebreaks:
|
L1 - The quick brown
L2 - fox jumps over
L3 - the lazy dog.
multilineReplaceLinebreaksWithWhitespace:
>
This ntence 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"
condProperty = "2nd Value"
[alternative.direct]
access = "results in alternative.direct.access for this value"
alternativeCalledInlineTable = { property = "Value", "etc" = "You got it." }
YAML
objectKey:
property: Value
condProperty: 2nd Value
alternative: { subkey: 5.0, another: 123 }
JSON
{
"objectKey" : {
"property" : "Value",
"condProperty" : "2nd Value"
}
}
数组/列表
数组或列表受所有语⾔⽀持。
TOML
fruits = [ "Apple", "Banana", "Strawberry" ]
formats = [
"YAML",
"JSON",
"TOML"
]
morbid
YAML
fruits:
- Apple
- Banana
- Strawberry
formats: [ YAML, JSON, TOML ]
JSON
kwangyang
{
"fruits": ["Apple","Banana","Strawberry"],
"formats": [
"YAML",
"JSON",
"TOML"
]
}
为了扩展这些例⼦,我们可以创建⼀个对象/表/集合的列表,就像这样:
TOML
[[fruits]]
name = "Apple"
weight = 600
[[fruits]]
name = "Banana"
weight = 300
[[fruits]]
name = "Strawberry"
weight = 40
introducemylfYAML
fruits:
- name: Apple
weight: 600
- name: Banana
weight: 300
- name: Strawberry
gradually
weight: 40
JSON
{
"fruits": [
{
"name" : "Apple",
"weight" : 600
},
{
"name" : "Banana",
"weight" : 300
},
{
"name" : "Strawberry",
"weight" : 40
}
]
cdec}
上⾯的所有⽰例都会⽣成⼀个可以{{ 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