YAML和JSON之间有什么不同,特别是考虑到以下事情?
性能(编码/解码时间) 内存消耗 表达清晰 库可用性,易用性(我更喜欢C)
我打算在我们的嵌入式系统中使用这两个中的一个来存储配置文件。
相关:
应该使用YAML还是JSON来存储Perl数据?
YAML和JSON之间有什么不同,特别是考虑到以下事情?
性能(编码/解码时间) 内存消耗 表达清晰 库可用性,易用性(我更喜欢C)
我打算在我们的嵌入式系统中使用这两个中的一个来存储配置文件。
相关:
应该使用YAML还是JSON来存储Perl数据?
当前回答
GIT 和 YAML
其他答案都很好。先读这些。但是我还要加上另一个有时使用YAML的原因:git。
越来越多的编程项目使用git存储库进行分发和归档。而且,虽然git回购的历史记录可以同样存储JSON和YAML文件,但用于跟踪和显示文件更改的“diff”方法是面向行的。由于YAML被迫面向行,因此YAML文件中的任何小更改都更容易被人看到。
当然,JSON文件确实可以通过对字符串/键进行排序和添加缩进来“变得漂亮”。但这不是默认的,我很懒。
就我个人而言,我通常使用JSON进行系统到系统的交互。我经常将YAML用于配置文件、静态文件和跟踪文件。(我通常也避免添加YAML关系锚。生命太短暂,没有时间去寻找循环。)
此外,如果速度和空间真的是一个问题,我都不用。你可能想看看BSON。
其他回答
从技术上讲,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。
基准测试结果
下面是在Python和Perl上比较YAML和JSON加载时间的基准测试结果
JSON要快得多,但牺牲了一些可读性和注释等特性
测试方法
在一台快速机器上连续运行100次,平均秒数 数据集是一个3.44MB的JSON文件,包含从维基百科抓取的电影数据 https://raw.githubusercontent.com/prust/wikipedia-movie-data/master/movies.json 链接来源:https://github.com/jdorfman/awesome-json-datasets
结果
Python 3.8.3 timeit
JSON: 0.108
YAML CLoader: 3.684
YAML: 29.763
Perl 5.26.2 Benchmark::cmpthese
JSON XS: 0.107
YAML XS: 0.574
YAML Syck: 1.050
Perl 5.26.2 Dumbbench (Brian D Foy, excludes outliers)
JSON XS: 0.102
YAML XS: 0.514
YAML Syck: 1.027
摘自: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时会丢失注释。
差异:
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.