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

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

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

相关:

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


当前回答

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.

其他回答

差异:

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.

我发现YAML更方便使用:括号更少,""等等。虽然YAML中有标签的烦恼…但总有人能掌握窍门。

在性能/资源方面,我不认为两者之间有很大的差异。

此外,我们讨论的是配置文件,所以我不期望编码/解码活动的频率很高,不是吗?

由于这个问题现在在搜索YAML和JSON时非常突出,值得注意的是两者之间一个很少被引用的区别:许可证。JSON声称拥有一个JSON用户必须遵守的许可(包括法律上模棱两可的“应使用代表善,而不是恶”)。YAML没有这样的许可声明,这可能是一个重要的区别(对您的律师来说,如果不是对您来说)。

从技术上讲,YAML提供了比JSON多得多的东西(YAML v1.2是JSON的超集):

评论 锚和继承- 3个相同项目的例子: item1: &anchor_name 名称:测试 title:测试标题 第二条:* anchor_name item3: < <: * anchor_name #你可以添加额外的东西。 ...

大多数情况下,人们不会使用这些额外的功能,主要的区别是YAML使用缩进,而JSON使用括号。这使得YAML(对于训练有素的眼睛)更加简洁和可读。

选择哪一个?

YAML额外的特性和简洁的符号使它成为配置文件(非用户提供的文件)的好选择。 JSON有限的特性、广泛的支持和更快的解析使其成为互操作性和用户提供数据的绝佳选择。

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的数据结构,只添加了一种额外类型:日期类型。