标签:版本 hex use 前置 常见 int his 子集 access
坦率地说,在我开始与Hugo TOML合作之前,我感到羞耻是一个需要发现的新领域,但我对YAML和JSON非常熟悉。本文将帮助您了解如何通过不同的数据格式构建数据。
在Hugo中,您可以将所有这三种数据格式用于配置,前置事项和自定义数据,但TOML是用于整个项目的推荐格式。首先我想简单介绍一下每种数据格式,然后再进入规范和比较。
TOML显然是由Tom - Tom Preston-Werner编写的 - 确切地说。这是一个在麻省理工学院授权的开源项目,目前在Github上有超过5k星。2013年3月发布的第一个TOML版本,使TOML成为三个标准的年轻人。
TOML的目标是成为最小的配置文件格式,由于精确的语义,这种格式易于阅读。TOML被设计为无歧义地映射到散列表。TOML应该很容易用各种语言来解析数据结构。
关于TOML语法的简短事实
要在前面的问题中使用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的创建者和维护者是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语法的简短事实
要在前面的问题中使用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和大多数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"
}
}
现在让我们来看看最常见用例中的语法和功能集差异。
任何格式都支持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吧。
标签:版本 hex use 前置 常见 int his 子集 access
原文地址:https://www.cnblogs.com/sunsky303/p/9208848.html