1. Overview
1.概述
In this quick article, we’ll take a look at the differences between YAML and JSON through quick and practical examples.
在这篇快速文章中,我们将通过快速和实际的例子来看看YAML和JSON的区别。
2. Format
2.样式
To have a better image, let’s start by looking at the JSON and YAML representations of a simple POJO:
为了有一个更好的形象,让我们先看看一个简单的POJO的JSON和YAML表示法。
class Person {
String name;
Integer age;
List<String> hobbies;
Person manager;
}
First, let’s look at its JSON representation:
首先,让我们看看它的JSON表示:。
{
"name":"John Smith",
"age":26,
"hobbies":[
"sports",
"cooking"
],
"manager":{
"name":"Jon Doe",
"age":45,
"hobbies":[
"fishing"
],
"manager":null
}
}
JSON syntax is somewhat cumbersome as it uses special syntax like curly braces {} and square brackets [] to represent objects and arrays.
JSON的语法有些麻烦,因为它使用特殊的语法,如大括号{}和方括号[]来表示对象和数组。
Next, let’s see how the same structure would look in YAML:
接下来,让我们看看同样的结构在YAML中会是什么样子。
name: John Smith
age: 26
hobbies:
- sports
- cooking
manager:
name: Jon Doe
age: 45
hobbies:
- fishing
manager:
YAML’s syntax looks a bit friendlier as it uses blank spaces to denote relations between objects and ‘–‘ to represent array elements.
YAML的语法看起来更友好一些,因为它使用空格来表示对象之间的关系,用’–‘来表示数组元素。
We can see that although both are easily readable, YAML tends to be more human-readable.
我们可以看到,尽管两者都很容易阅读,但YAML往往更适合人类阅读。
Another bonus point for YAML is the number of lines it takes to represent the same information — YAML takes only 11 lines, while JSON takes 16.
YAML的另一个优点是表示相同信息所需的行数 – YAML只需要11行,而JSON需要16行。
3. Size
3.尺寸
We’ve seen in the previous section that YAML is represented in fewer lines than JSON, but does that mean that it takes less space?
我们在上一节中看到,YAML比JSON用更少的行来表示,但这是否意味着它占用的空间更少呢?
Let’s imagine a deeply nested structure with a parent and five children represented as JSON:
让我们想象一个深度嵌套的结构,有一个父级和五个子级,以JSON表示。
{
"child":{
"child":{
"child":{
"child":{
"child":{
"child":{
"child":null
}
}
}
}
}
}
}
The same structure would look similar in YAML:
同样的结构在YAML中看起来类似:。
child:
child:
child:
child:
child:
child:
child:
On first sight, it might look like JSON takes more space, but, in reality, JSON specification doesn’t care about whitespace or newlines, and it can be shortened as follows:
乍一看,可能会觉得JSON占用了更多的空间,但实际上,JSON规范并不关心空白或换行,它可以被缩短为如下。
{"child":{"child":{"child":{"child":{"child":{"child":{"child":null}}}}}}}
We can see that the second form is much shorter, and it occupies only 74 bytes, while the YAML format takes 97 bytes.
我们可以看到,第二种形式要短得多,它只占用74个字节,而YAML格式需要97个字节。
4. YAML Features
4.YAML的特点
Besides the basic features that JSON provides, YAML comes with additional functionality as we’ll see next.
除了JSON提供的基本功能外,YAML还带有额外的功能,我们接下来会看到。
4.1. Comments
4.1.评论
YAML allows comments by using #, a feature that is often desired when working with JSON files:
YAML允许通过使用#进行评论,这是处理JSON文件时经常需要的功能:。
# This is a simple comment
name: John
4.2. Multi-Line Strings
4.2 多线串联
Another feature missing in JSON but present in YAML is multi-line strings:
另一个在JSON中缺少但在YAML中存在的功能是多行字符串。
website: |
line1
line2
line3
4.3. Aliases and Anchors
4.3.别名和锚
We can easily assign an alias to a specific item using & and anchor (reference) it using *:
我们可以使用&轻松地将别名分配给一个特定的项目,并使用*锚定(引用)它。
httpPort: 80
httpsPort: &httpsPort 443
defaultPort: *httpsPort
5. Performance
5.业绩
Due to the simple nature of JSON specification, its performance in parsing/serializing data is much better than YAML.
由于JSON规范的简单性,其解析/序列化数据的性能比YAML好得多。
We’re going to implement a simple benchmark to compare the parsing speed of YAML and JSON using JMH.
我们将实现一个简单的基准,使用JMH比较YAML和JSON的解析速度。
For the YAML benchmark, we’re going to use the well-known snake-yaml library, and for our JSON benchmark, we’ll use org-json:
对于YAML基准,我们将使用著名的snake-yaml库,而对于JSON基准,我们将使用org-json。
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Measurement(batchSize = 10_000, iterations = 5)
@Warmup(batchSize = 10_000, iterations = 5)
@State(Scope.Thread)
class Bench {
static void main(String[] args) {
org.openjdk.jmh.Main.main(args);
}
@State(Scope.Thread)
static class YamlState {
public Yaml yaml = new Yaml();
}
@Benchmark
Object benchmarkYaml(YamlState yamlState) {
return yamlState.yaml.load("foo: bar");
}
@Benchmark
Object benchmarkJson(Blackhole blackhole) {
return new JSONObject("{\"foo\": \"bar\"}");
}
}
As we might’ve expected, JSON is the winner, being approximately 30 times faster:
正如我们所预期的,JSON是赢家,大约快30倍。
Benchmark Mode Cnt Score Error Units
Main2.benchmarkJson thrpt 50 644.085 ± 9.962 ops/s
Main2.benchmarkYaml thrpt 50 20.351 ± 0.312 ops/s
6. Library Availability
6.图书馆的可用性
JavaScript is the standard for the web, meaning that it’s almost impossible to find a language that doesn’t fully support JSON.
JavaScript是网络的标准,意味着几乎不可能找到一种不完全支持JSON的语言。
On the other hand, YAML is widely supported, but it’s not a standard. This means that libraries exist for most popular programming languages, but due to its complexity, they might not fully implement the specification.
另一方面,YAML被广泛支持,但它不是一个标准。这意味着大多数流行的编程语言都有库,但由于其复杂性,它们可能无法完全实现该规范。
7. What Should I Choose?
7.我应该选择什么?
This might be a difficult question to answer and a subjective one in many cases.
这可能是一个很难回答的问题,在很多情况下是一个主观的问题。
If we need to expose a set of REST APIs to other front-end or back-end applications, we should probably go with JSON as it’s the de facto industry standard.
如果我们需要将一组REST API暴露给其他前端或后端应用程序,我们可能应该使用JSON,因为它是事实上的行业标准。
If we need to create a configuration file that will often be read/updated by humans, YAML might be a good option.
如果我们需要创建一个经常被人类读取/更新的配置文件,YAML可能是一个不错的选择。
Of course, there might also be use cases where both YAML and JSON would be a good fit, and it will be just a matter of taste.
当然,也可能有YAML和JSON都很适合的用例,这将只是一个品味问题。
8. Conclusion
8.结语
In this quick article, we’ve learned the main differences between YAML and JSON and what aspects to consider to make an informed decision as to which one we should choose.
在这篇简短的文章中,我们已经了解了YAML和JSON的主要区别,以及要考虑哪些方面来做出一个明智的决定,我们应该选择哪一个。