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.

其他回答

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

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

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

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

选择哪一个?

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

从技术上讲,YAML是JSON的超集。这意味着,至少在理论上,YAML解析器可以理解JSON,但不一定反过来。

在“YAML:与JSON的关系”一节中可以看到官方规范。

总的来说,我喜欢YAML的某些东西是JSON中没有的。

正如@jdupont指出的,YAML在视觉上更容易看。事实上,YAML主页本身就是有效的YAML,但是它很容易被人阅读。 YAML能够使用“锚”引用YAML文件中的其他项。因此,它可以像在MySQL数据库中一样处理关系信息。 YAML在YAML文件中嵌入其他序列化格式(如JSON或XML)方面更加健壮。

在实践中,最后两点可能对您或我所做的事情都不重要,但从长远来看,我认为YAML将是一种更健壮、更可行的数据序列化格式。

现在,AJAX和其他web技术倾向于使用JSON。YAML目前更多地用于脱机数据处理。例如,它默认包含在基于c语言的OpenCV计算机视觉包中,而JSON则没有。

您将发现JSON和YAML的C库。YAML的库往往更新,但我过去使用它们没有遇到过麻烦。参见Yaml-cpp的例子。

GIT 和 YAML

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

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

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

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

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

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.