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和JSON时非常突出,值得注意的是两者之间一个很少被引用的区别:许可证。JSON声称拥有一个JSON用户必须遵守的许可(包括法律上模棱两可的“应使用代表善,而不是恶”)。YAML没有这样的许可声明,这可能是一个重要的区别(对您的律师来说,如果不是对您来说)。
基准测试结果
下面是在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
如果您关心更好的解析速度,那么可以选择将数据存储在JSON中。我必须从其他用户修改文件的位置解析数据,因此我使用YAML,因为与JSON相比,它提供了更好的可读性。 你还可以在YAML文件中添加注释,这在JSON文件中是做不到的。
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有而JSON没有的任何特性,我更喜欢JSON,因为它非常简单,并且得到广泛支持(有很多语言的库)。YAML更复杂,支持更少。我不认为解析速度或内存使用会有太大的不同,而且可能不是程序性能的主要部分。