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.

其他回答

如果你不需要YAML有而JSON没有的任何特性,我更喜欢JSON,因为它非常简单,并且得到广泛支持(有很多语言的库)。YAML更复杂,支持更少。我不认为解析速度或内存使用会有太大的不同,而且可能不是程序性能的主要部分。

差异:

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.

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.

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

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

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

摘自:Arnaud Lauret的书《Web api的设计》。”:

JSON数据格式

JSON是一种基于JavaScript编程语言描述数据的文本数据格式,尽管它的名字是JSON,但它完全独立于语言(参见https://www.json.org/)。使用JSON,可以描述包含无序名称/值对的对象,也可以描述包含有序值的数组或列表,如图所示。

An object is delimited by curly braces ({}). A name is a quoted string ("name") and is sep- arated from its value by a colon (:). A value can be a string like "value", a number like 1.23, a Boolean (true or false), the null value null, an object, or an array. An array is delimited by brackets ([]), and its values are separated by commas (,). The JSON format is easily parsed using any programming language. It is also relatively easy to read and write. It is widely adopted for many uses such as databases, configura- tion files, and, of course, APIs.

YAML

YAML (YAML Ain 't Markup Language)是一种人性化的数据序列化格式。和JSON一样,YAML (http://yaml.org)也是一种键/值数据格式。该图显示了两者的比较。

注意以下几点:

在YAML中,属性名和值周围没有双引号(“”)。 JSON的结构花括号({})和逗号(,)被换行符和取代 YAML的压痕。 数组中的括号([])和逗号(,)被破折号(-)和换行符取代 YAML。 与JSON不同,YAML允许以#开头的注释。 将其中一种格式转换为另一种格式相对容易。不过要预先警告,在将YAML文档转换为JSON时会丢失注释。