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

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

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

相关:

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


当前回答

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

其他回答

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.

有时候你不需要在两者之间做出选择。

例如,在围棋中,你可以同时拥有这两者:

type Person struct {
    Name string `json:"name" yaml:"name"`
    Age int `json:"age" yaml:"age"`
}

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

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

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

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

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

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

选择哪一个?

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