YAML和JSON之间有什么不同,特别是考虑到以下事情?
性能(编码/解码时间) 内存消耗 表达清晰 库可用性,易用性(我更喜欢C)
我打算在我们的嵌入式系统中使用这两个中的一个来存储配置文件。
相关:
应该使用YAML还是JSON来存储Perl数据?
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中有标签的烦恼…但总有人能掌握窍门。
在性能/资源方面,我不认为两者之间有很大的差异。
此外,我们讨论的是配置文件,所以我不期望编码/解码活动的频率很高,不是吗?
有时候你不需要在两者之间做出选择。
例如,在围棋中,你可以同时拥有这两者:
type Person struct {
Name string `json:"name" yaml:"name"`
Age int `json:"age" yaml:"age"`
}
如果你不需要YAML有而JSON没有的任何特性,我更喜欢JSON,因为它非常简单,并且得到广泛支持(有很多语言的库)。YAML更复杂,支持更少。我不认为解析速度或内存使用会有太大的不同,而且可能不是程序性能的主要部分。
GIT 和 YAML
其他答案都很好。先读这些。但是我还要加上另一个有时使用YAML的原因:git。
越来越多的编程项目使用git存储库进行分发和归档。而且,虽然git回购的历史记录可以同样存储JSON和YAML文件,但用于跟踪和显示文件更改的“diff”方法是面向行的。由于YAML被迫面向行,因此YAML文件中的任何小更改都更容易被人看到。
当然,JSON文件确实可以通过对字符串/键进行排序和添加缩进来“变得漂亮”。但这不是默认的,我很懒。
就我个人而言,我通常使用JSON进行系统到系统的交互。我经常将YAML用于配置文件、静态文件和跟踪文件。(我通常也避免添加YAML关系锚。生命太短暂,没有时间去寻找循环。)
此外,如果速度和空间真的是一个问题,我都不用。你可能想看看BSON。