YAML和JSON之间有什么不同,特别是考虑到以下事情?

性能(编码/解码时间) 内存消耗 表达清晰 库可用性,易用性(我更喜欢C)

我打算在我们的嵌入式系统中使用这两个中的一个来存储配置文件。

相关:

应该使用YAML还是JSON来存储Perl数据?


当前回答

差异:

YAML, depending on how you use it, can be more readable than JSON JSON is often faster and is probably still interoperable with more systems It's possible to write a "good enough" JSON parser very quickly Duplicate keys, which are potentially valid JSON, are definitely invalid YAML. YAML has a ton of features, including comments and relational anchors. YAML syntax is accordingly quite complex, and can be hard to understand. It is possible to write recursive structures in yaml: {a: &b [*b]}, which will loop infinitely in some converters. Even with circular detection, a "yaml bomb" is still possible (see xml bomb). Because there are no references, it is impossible to serialize complex structures with object references in JSON. YAML serialization can therefore be more efficient. In some coding environments, the use of YAML can allow an attacker to execute arbitrary code.

观察:

Python programmers are generally big fans of YAML, because of the use of indentation, rather than bracketed syntax, to indicate levels. Many programmers consider the attachment of "meaning" to indentation a poor choice. If the data format will be leaving an application's environment, parsed within a UI, or sent in a messaging layer, JSON might be a better choice. YAML can be used, directly, for complex tasks like grammar definitions, and is often a better choice than inventing a new language.

其他回答

GIT 和 YAML

其他答案都很好。先读这些。但是我还要加上另一个有时使用YAML的原因:git。

越来越多的编程项目使用git存储库进行分发和归档。而且,虽然git回购的历史记录可以同样存储JSON和YAML文件,但用于跟踪和显示文件更改的“diff”方法是面向行的。由于YAML被迫面向行,因此YAML文件中的任何小更改都更容易被人看到。

当然,JSON文件确实可以通过对字符串/键进行排序和添加缩进来“变得漂亮”。但这不是默认的,我很懒。

就我个人而言,我通常使用JSON进行系统到系统的交互。我经常将YAML用于配置文件、静态文件和跟踪文件。(我通常也避免添加YAML关系锚。生命太短暂,没有时间去寻找循环。)

此外,如果速度和空间真的是一个问题,我都不用。你可能想看看BSON。

JSON编码六种数据类型:对象(映射)、数组、字符串数字、布尔值和Null。对于机器来说,它非常容易解析,并且提供很少的灵活性。说明书大约有一页半。

YAML allows the encoding of arbitrary Python data and other crazy crap (which leads to vulnerabilities when decoding it). It is hard to parse because it offers so much flexibility. The specification for YAML was 86 pages, the last time I checked. YAML syntax is obviously influenced by Python, but maybe they should have been a little more influenced by the Python philosophy on a few points: e.g. “there should be one—and preferably only one—obvious way to do it” and “simple is better than complex.”

与JSON相比,YAML的主要优点是更容易阅读和编辑,这使它成为配置文件的自然选择。

最近,我倾向于使用TOML作为配置文件。它不像YAML那样漂亮或灵活,但对机器和人类来说都更容易解析。该语法(几乎)是INI语法的超集,但它解析为类似json的数据结构,只添加了一种额外类型:日期类型。

差异:

YAML, depending on how you use it, can be more readable than JSON JSON is often faster and is probably still interoperable with more systems It's possible to write a "good enough" JSON parser very quickly Duplicate keys, which are potentially valid JSON, are definitely invalid YAML. YAML has a ton of features, including comments and relational anchors. YAML syntax is accordingly quite complex, and can be hard to understand. It is possible to write recursive structures in yaml: {a: &b [*b]}, which will loop infinitely in some converters. Even with circular detection, a "yaml bomb" is still possible (see xml bomb). Because there are no references, it is impossible to serialize complex structures with object references in JSON. YAML serialization can therefore be more efficient. In some coding environments, the use of YAML can allow an attacker to execute arbitrary code.

观察:

Python programmers are generally big fans of YAML, because of the use of indentation, rather than bracketed syntax, to indicate levels. Many programmers consider the attachment of "meaning" to indentation a poor choice. If the data format will be leaving an application's environment, parsed within a UI, or sent in a messaging layer, JSON might be a better choice. YAML can be used, directly, for complex tasks like grammar definitions, and is often a better choice than inventing a new language.

如果您关心更好的解析速度,那么可以选择将数据存储在JSON中。我必须从其他用户修改文件的位置解析数据,因此我使用YAML,因为与JSON相比,它提供了更好的可读性。 你还可以在YAML文件中添加注释,这在JSON文件中是做不到的。

基准测试结果

下面是在Python和Perl上比较YAML和JSON加载时间的基准测试结果

JSON要快得多,但牺牲了一些可读性和注释等特性

测试方法

在一台快速机器上连续运行100次,平均秒数 数据集是一个3.44MB的JSON文件,包含从维基百科抓取的电影数据 https://raw.githubusercontent.com/prust/wikipedia-movie-data/master/movies.json 链接来源:https://github.com/jdorfman/awesome-json-datasets

结果

Python 3.8.3 timeit
    JSON:            0.108
    YAML CLoader:    3.684
    YAML:           29.763

Perl 5.26.2 Benchmark::cmpthese
    JSON XS:         0.107
    YAML XS:         0.574
    YAML Syck:       1.050

Perl 5.26.2 Dumbbench (Brian D Foy, excludes outliers)
    JSON XS:         0.102
    YAML XS:         0.514
    YAML Syck:       1.027