YAML 和 JSON 有什么区别?
YAML 和 JSON 之间有什么区别,具体考虑以下几点?
- 性能(编码/解码时间)
- 内存消耗
- 表达式清晰度
- 库可用性、易用性(我更喜欢 C)
我计划在我们的嵌入式系统中使用这两个之一来存储配置文件。
What are the differences between YAML and JSON, specifically considering the following things?
- Performance (encode/decode time)
- Memory consumption
- Expression clarity
- Library availability, ease of use (I prefer C)
I was planning to use one of these two in our embedded system to store configure files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
从技术上讲,YAML 是 JSON 的超集。这意味着,至少在理论上,YAML 解析器可以理解 JSON,但反之则不一定。
请参阅标题为 “YAML:与 JSON 的关系” 的官方规范。
一般来说,我喜欢 YAML 的某些东西是 JSON 所不具备的。
实际上,最后两点对于你或我所做的事情可能都不重要,但从长远来看,我认为 YAML 将是一种更强大、更可行的数据序列化格式。
目前,AJAX 和其他 Web 技术倾向于使用 JSON。 YAML 目前更多地用于离线数据处理。例如,它默认包含在基于 C 的 OpenCV 计算机视觉包中,而 JSON 则不然。
您将找到 JSON 和 YAML 的 C 库。 YAML 的库往往较新,但我过去使用它们没有遇到任何问题。请参阅 Yaml-cpp 示例。
Technically YAML is a superset of JSON. This means that, in theory at least, a YAML parser can understand JSON, but not necessarily the other way around.
See the official specs, in the section entitled "YAML: Relation to JSON".
In general, there are certain things I like about YAML that are not available in JSON.
In practice neither of these last two points will likely matter for things that you or I do, but in the long term, I think YAML will be a more robust and viable data serialization format.
Right now, AJAX and other web technologies tend to use JSON. YAML is currently being used more for offline data processes. For example, it is included by default in the C-based OpenCV computer vision package, whereas JSON is not.
You will find C libraries for both JSON and YAML. YAML's libraries tend to be newer, but I have had no trouble with them in the past. See for example Yaml-cpp.
差异:
{a: &b [*b]}
,这在某些转换器中会无限循环。即使使用循环检测,“yaml 炸弹”仍然是可能的(请参阅 xml 炸弹)。观察结果:
Differences:
{a: &b [*b]}
, which will loop infinitely in some converters. Even with circular detection, a "yaml bomb" is still possible (see xml bomb).Observations:
绕过深奥的理论
这回答了标题,而不是细节,因为大多数人只是像我一样从谷歌搜索结果中阅读标题,所以我觉得有必要从网络开发人员的角度解释。
房间里的大象:互联网本身
JavaScript 显然以巨大的优势在 Web 上占据主导地位,并且 JavaScript 开发人员更喜欢使用 JSON 作为压倒性的数据格式以及流行的 Web API,因此在进行 Web 编程时,很难争论使用 YAML 而不是 JSON。一般意义上,因为在团队环境中您可能会被否决。事实上,大多数 Web 程序员甚至不知道 YAML 的存在,更不用说考虑使用它了。
如果您正在进行任何 Web 编程,JSON 是默认方式,因为使用 JavaScript 时不需要翻译步骤,因此在这种情况下您必须提出更好的参数来使用 YAML 而不是 JSON。
Bypassing esoteric theory
This answers the title, not the details as most just read the title from a search result on google like me so I felt it was necessary to explain from a web developer perspective.
The Elephant in the room: The Internet itself
JavaScript so clearly dominates the web by a huge margin and JavaScript developers prefer using JSON as the data format overwhelmingly along with popular web APIs so it becomes difficult to argue using YAML over JSON when doing web programming in the general sense as you will likely be outvoted in a team environment. In fact, the majority of web programmers aren't even aware YAML exists, let alone consider using it.
If you are doing any web programming, JSON is the default way to go because no translation step is needed when working with JavaScript so then you must come up with a better argument to use YAML over JSON in that case.
这个问题已经有 6 年历史了,但奇怪的是,没有一个答案真正解决了所有四个点(速度、内存、表现力、可移植性)。
速度
显然,这取决于实现,但由于 JSON 的使用如此广泛,并且很容易实现,因此它往往会获得更多的本机支持,从而提高速度。考虑到 YAML 可以完成 JSON 所做的所有事情,而且还可以做更多的事情,因此很可能在两者的任何类似实现中,JSON 会更快。
但是,鉴于 YAML 文件可能比其 JSON 对应文件稍小(由于
"
和,
字符较少),因此可能高度优化的 YAML 解析器在特殊情况下可能会更快。基本上
很难理解为什么它们会比 JSON 解析器更高效。
,如果 YAML 解析器表示相同的数据结构,
那么 在其他人看来,Python 程序员更喜欢 YAML,JavaScript 程序员更喜欢 JSON,我将做出以下观察:
可移植性
很难想象没有 JSON 库的现代语言。也很难想象 JSON 解析器会实现任何低于完整规范的内容。 YAML 拥有广泛的支持,但不如 JSON 普遍,并且每个解析器都实现不同的子集。因此,YAML 文件的互操作性比您想象的要差。
总结
JSON 是性能(如果相关)和互操作性方面的赢家。 YAML 更适合人工维护的文件。 HJSON 是一个不错的折衷方案,尽管可移植性大大降低。 JSON5 是一个更合理的折衷方案,具有定义良好的语法。
This question is 6 years old, but strangely, none of the answers really addresses all four points (speed, memory, expressiveness, portability).
Speed
Obviously this is implementation-dependent, but because JSON is so widely used, and so easy to implement, it has tended to receive greater native support, and hence speed. Considering that YAML does everything that JSON does, plus a truckload more, it's likely that of any comparable implementations of both, the JSON one will be quicker.
However, given that a YAML file can be slightly smaller than its JSON counterpart (due to fewer
"
and,
characters), it's possible that a highly optimised YAML parser might be quicker in exceptional circumstances.Memory
Basically the same argument applies. It's hard to see why a YAML parser would ever be more memory efficient than a JSON parser, if they're representing the same data structure.
Expressiveness
As noted by others, Python programmers tend towards preferring YAML, JavaScript programmers towards JSON. I'll make these observations:
Portability
It's hard to imagine a modern language without a JSON library. It's also hard to imagine a JSON parser implementing anything less than the full spec. YAML has widespread support, but is less ubiquitous than JSON, and each parser implements a different subset. Hence YAML files are less interoperable than you might think.
Summary
JSON is the winner for performance (if relevant) and interoperability. YAML is better for human-maintained files. HJSON is a decent compromise although with much reduced portability. JSON5 is a more reasonable compromise, with well-defined syntax.
GIT 和 YAML
其他答案都很好。首先阅读这些内容。但我会添加有时使用 YAML 的另一个原因:git。
许多编程项目越来越多地使用 git 存储库进行分发和存档。而且,虽然 git 存储库的历史记录可以同等地存储 JSON 和 YAML 文件,但用于跟踪和显示文件更改的“diff”方法是面向行的。由于 YAML 被迫面向行,因此 YAML 文件中的任何微小更改都更容易被人看到。
当然,确实可以通过对字符串/键进行排序并添加缩进来“使 JSON 文件变得漂亮”。但这不是默认设置,而且我很懒。
就我个人而言,我通常使用 JSON 进行系统间交互。我经常将 YAML 用于配置文件、静态文件和跟踪文件。 (我通常也会避免添加 YAML 关系锚点。生命太短,无法寻找循环。)
此外,如果速度和空间确实是一个问题,我也不使用其中任何一个。您可能想看看 BSON。
GIT and YAML
The other answers are good. Read those first. But I'll add one other reason to use YAML sometimes: git.
Increasingly, many programming projects use git repositories for distribution and archival. And, while a git repo's history can equally store JSON and YAML files, the "diff" method used for tracking and displaying changes to a file is line-oriented. Since YAML is forced to be line-oriented, any small changes in a YAML file are easier to see by a human.
It is true, of course, that JSON files can be "made pretty" by sorting the strings/keys and adding indentation. But this is not the default and I'm lazy.
Personally, I generally use JSON for system-to-system interaction. I often use YAML for config files, static files, and tracked files. (I also generally avoid adding YAML relational anchors. Life is too short to hunt down loops.)
Also, if speed and space are really a concern, I don't use either. You might want to look at BSON.
我发现 YAML 更容易看:更少的括号、“”等。虽然 YAML 中存在制表符的烦恼……但人们会掌握它的窍门。
就性能/资源而言,我预计两者之间不会有太大差异。
此外,我们正在讨论配置文件,因此我不会期望编码/解码活动的频率很高,不是吗?
I find YAML to be easier on the eyes: less parenthesis, "" etc. Although there is the annoyance of tabs in YAML... but one gets the hang of it.
In terms of performance/resources, I wouldn't expect big differences between the two.
Futhermore, we are talking about configuration files and so I wouldn't expect a high frequency of encode/decode activity, no?
从技术上讲,YAML 提供的功能远不止 JSON (YAML v1.2是 JSON 的超集):
锚点和继承 - 3 个相同项目的示例:
大多数时候人们不会使用这些额外的功能,主要区别在于 YAML 使用缩进,而 < strong>JSON 使用括号。这使得 YAML 更加简洁和可读(对于受过训练的人来说)。
选择哪一个?
Technically YAML offers a lot more than JSON (YAML v1.2 is a superset of JSON):
anchors and inheritance - example of 3 identical items:
Most of the time people will not use those extra features and the main difference is that YAML uses indentation whilst JSON uses brackets. This makes YAML more concise and readable (for the trained eye).
Which one to choose?
如果您不需要 YAML 具有而 JSON 没有的任何功能,我会更喜欢 JSON,因为它非常简单并且得到广泛支持(有很多多种语言的库)。 YAML 更复杂并且支持较少。我认为解析速度或内存使用不会有太大不同,并且可能不是程序性能的重要组成部分。
If you don't need any features which YAML has and JSON doesn't, I would prefer JSON because it is very simple and is widely supported (has a lot of libraries in many languages). YAML is more complex and has less support. I don't think the parsing speed or memory use will be very much different, and maybe not a big part of your program's performance.
基准测试结果
下面是比较 YAML 与 JSON 加载时间的基准测试结果,在 Python 和 Perl 上
JSON 更快,但牺牲了一些可读性和注释等功能
测试方法
https://raw.githubusercontent.com/prust/wikipedia-movie -data/master/movies.json
结果
Benchmark results
Below are the results of a benchmark to compare YAML vs JSON loading times, on Python and Perl
JSON is much faster, at the expense of some readability, and features such as comments
Test method
https://raw.githubusercontent.com/prust/wikipedia-movie-data/master/movies.json
Results
来源:Arnaud Lauret 所著的《Web API 设计》。 :
JSON 数据格式
JSON 是一种基于 JavaScript 编程语言描述数据方式的文本数据格式,但尽管其名称如此,却完全独立于语言(请参阅 https://www.json.org/)。使用 JSON,您可以描述包含无序名称/值对的对象以及包含有序值的数组或列表,如图所示。
对象由大括号 ({}) 分隔。名称是带引号的字符串(“名称”),并通过冒号 (:) 与其值分隔。值可以是字符串(如“value”)、数字(如 1.23)、布尔值(true 或 false)、空值 null、对象或数组。数组由方括号 ([]) 分隔,其值由逗号 (,) 分隔。
JSON 格式可以使用任何编程语言轻松解析。它也相对容易阅读和编写。它被广泛应用于多种用途,例如数据库、配置文件,当然还有 API。
YAML
YAML(YAML 不是标记语言)是一种人性化的数据序列化格式。与 JSON 一样,YAML (http://yaml.org) 是一种键/值数据格式。该图显示了两者的比较。
请注意以下几点:
YAML 中的属性名称和值周围没有双引号 (" ")。
JSON 的结构花括号 ({}) 和逗号 (,) 被换行符和
YAML 中的缩进。
数组括号 ([]) 和逗号 (,) 被破折号 (-) 和换行符替换
YAML。
与 JSON 不同,YAML 允许以井号 (#) 开头的注释。
将其中一种格式转换为另一种格式相对容易。但请预先警告,将 YAML 文档转换为 JSON 时,您将丢失注释。
From: Arnaud Lauret Book “The Design of Web APIs.” :
The JSON data format
JSON is a text data format based on how the JavaScript programming language describes data but is, despite its name, completely language-independent (see https://www.json.org/). Using JSON, you can describe objects containing unordered name/value pairs and also arrays or lists containing ordered values, as shown in this figure.
An object is delimited by curly braces ({}). A name is a quoted string ("name") and is sep- arated from its value by a colon (:). A value can be a string like "value", a number like 1.23, a Boolean (true or false), the null value null, an object, or an array. An array is delimited by brackets ([]), and its values are separated by commas (,).
The JSON format is easily parsed using any programming language. It is also relatively easy to read and write. It is widely adopted for many uses such as databases, configura- tion files, and, of course, APIs.
YAML
YAML (YAML Ain’t Markup Language) is a human-friendly, data serialization format. Like JSON, YAML (http://yaml.org) is a key/value data format. The figure shows a comparison of the two.
Note the following points:
There are no double quotes (" ") around property names and values in YAML.
JSON’s structural curly braces ({}) and commas (,) are replaced by newlines and
indentation in YAML.
Array brackets ([]) and commas (,) are replaced by dashes (-) and newlines in
YAML.
Unlike JSON, YAML allows comments beginning with a hash mark (#).
It is relatively easy to convert one of those formats into the other. Be forewarned though, you will lose comments when converting a YAML document to JSON.
由于这个问题现在在搜索 YAML 和 JSON 时非常突出,因此值得注意两者之间一个很少被引用的区别:许可证。 JSON 声称拥有 JSON 用户必须遵守的 许可证(包括法律上不明确的“应用于善,而不是恶”)。 YAML 不包含此类许可声明,这可能是一个重要的区别(对您的律师来说,如果不是对您而言)。
Since this question now features prominently when searching for YAML and JSON, it's worth noting one rarely-cited difference between the two: license. JSON purports to have a license which JSON users must adhere to (including the legally-ambiguous "shall be used for Good, not Evil"). YAML carries no such license claim, and that might be an important difference (to your lawyer, if not to you).
有时您不必决定选择其中之一。
例如,在 Go 中,您可以同时拥有两者:
Sometimes you don't have to decide for one over the other.
In Go, for example, you can have both at the same time:
我发现 YAML 和 JSON 都非常有效。对我来说,真正决定何时使用一种语言而不是另一种语言的唯一两件事是,该语言最常用的用途。例如,如果我使用 Java、Javascript,我将使用 JSON。对于 Java,我将使用他们自己的对象,这些对象几乎都是 JSON,但缺乏某些功能,如果需要,我会将其转换为 JSON,或者首先将其转换为 JSON。我这样做是因为这在 Java 中很常见,并且可以让其他 Java 开发人员更轻松地修改我的代码。第二件事是我是否使用它来让程序记住属性,或者程序是否以配置文件的形式接收指令,在这种情况下我将使用 YAML,因为它很容易被人类阅读,具有良好的性能。即使您不知道 YAML 是如何工作的,它也很容易修改。然后,程序将读取它并将其转换为 JSON 或该语言首选的任何格式。
最后,老实说这并不重要。任何有经验的程序员都可以轻松阅读 JSON 和 YAML。
I find both YAML and JSON to be very effective. The only two things that really dictate when one is used over the other for me is one, what the language is used most popularly with. For example, if I'm using Java, Javascript, I'll use JSON. For Java, I'll use their own objects, which are pretty much JSON but lacking in some features, and convert it to JSON if I need to or make it in JSON in the first place. I do that because that's a common thing in Java and makes it easier for other Java developers to modify my code. The second thing is whether I'm using it for the program to remember attributes, or if the program is receiving instructions in the form of a config file, in this case I'll use YAML, because it's very easily human read, has nice looking syntax, and is very easy to modify, even if you have no idea how YAML works. Then, the program will read it and convert it to JSON, or whatever is preferred for that language.
In the end, it honestly doesn't matter. Both JSON and YAML are easily read by any experienced programmer.
如果您担心更好的解析速度,那么可以选择将数据存储在 JSON 中。我必须从文件会受到其他用户修改的位置解析数据,因此我使用 YAML,因为与 JSON 相比,它提供了更好的可读性。
您还可以在 YAML 文件中添加注释,这是在 JSON 文件中无法完成的。
If you are concerned about better parsing speed then storing the data in JSON is the option. I had to parse the data from a location where the file was subject to modification from other users and hence I used YAML as it provides better readability compared to JSON.
And you can also add comments in the YAML file which can't be done in a JSON file.