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

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

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

相关:

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


当前回答

绕过深奥的理论

这回答了标题,而不是细节,因为大多数人只是从谷歌上的搜索结果中阅读标题,就像我一样,所以我觉得有必要从web开发人员的角度解释。

YAML uses space indentation, which is familiar territory for Python developers. JavaScript developers love JSON because it is a subset of JavaScript and can be directly interpreted and written inside JavaScript, along with using a shorthand way to declare JSON, requiring no double quotes in keys when using typical variable names without spaces. There are a plethora of parsers that work very well in all languages for both YAML and JSON. YAML's space format can be much easier to look at in many cases because the formatting requires a more human-readable approach. YAML's form while being more compact and easier to look at can be deceptively difficult to hand edit if you don't have space formatting visible in your editor. Tabs are not spaces so that further confuses if you don't have an editor to interpret your keystrokes into spaces. JSON is much faster to serialize and deserialize because of significantly less features than YAML to check for, which enables smaller and lighter code to process JSON. A common misconception is that YAML needs less punctuation and is more compact than JSON but this is completely false. Whitespace is invisible so it seems like there are less characters, but if you count the actual whitespace which is necessary to be there for YAML to be interpreted properly along with proper indentation, you will find YAML actually requires more characters than JSON. JSON doesn't use whitespace to represent hierarchy or grouping and can be easily flattened with unnecessary whitespace removed for more compact transport.

房间里的大象:互联网本身

JavaScript显然以巨大的优势统治着网络,JavaScript开发人员更喜欢使用JSON作为数据格式,以及流行的web api,因此在进行一般意义上的web编程时,很难争论使用YAML还是JSON,因为在团队环境中你可能会被压倒。事实上,大多数web程序员甚至不知道YAML的存在,更不用说考虑使用它了。

如果你正在做任何web编程,JSON是默认的方式,因为使用JavaScript时不需要翻译步骤,所以在这种情况下,你必须提出一个更好的参数来使用YAML而不是JSON。

其他回答

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

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

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

选择哪一个?

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

绕过深奥的理论

这回答了标题,而不是细节,因为大多数人只是从谷歌上的搜索结果中阅读标题,就像我一样,所以我觉得有必要从web开发人员的角度解释。

YAML uses space indentation, which is familiar territory for Python developers. JavaScript developers love JSON because it is a subset of JavaScript and can be directly interpreted and written inside JavaScript, along with using a shorthand way to declare JSON, requiring no double quotes in keys when using typical variable names without spaces. There are a plethora of parsers that work very well in all languages for both YAML and JSON. YAML's space format can be much easier to look at in many cases because the formatting requires a more human-readable approach. YAML's form while being more compact and easier to look at can be deceptively difficult to hand edit if you don't have space formatting visible in your editor. Tabs are not spaces so that further confuses if you don't have an editor to interpret your keystrokes into spaces. JSON is much faster to serialize and deserialize because of significantly less features than YAML to check for, which enables smaller and lighter code to process JSON. A common misconception is that YAML needs less punctuation and is more compact than JSON but this is completely false. Whitespace is invisible so it seems like there are less characters, but if you count the actual whitespace which is necessary to be there for YAML to be interpreted properly along with proper indentation, you will find YAML actually requires more characters than JSON. JSON doesn't use whitespace to represent hierarchy or grouping and can be easily flattened with unnecessary whitespace removed for more compact transport.

房间里的大象:互联网本身

JavaScript显然以巨大的优势统治着网络,JavaScript开发人员更喜欢使用JSON作为数据格式,以及流行的web api,因此在进行一般意义上的web编程时,很难争论使用YAML还是JSON,因为在团队环境中你可能会被压倒。事实上,大多数web程序员甚至不知道YAML的存在,更不用说考虑使用它了。

如果你正在做任何web编程,JSON是默认的方式,因为使用JavaScript时不需要翻译步骤,所以在这种情况下,你必须提出一个更好的参数来使用YAML而不是JSON。

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

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

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

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

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