哪些有效的 JSON 文件不是有效的 YAML 1.1 文件? [英] What valid JSON files are not valid YAML 1.1 files?

查看:20
本文介绍了哪些有效的 JSON 文件不是有效的 YAML 1.1 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YAML 1.2 是(有一个关于重复键的次要警告)超集JSON,因此任何有效的 JSON 文件也是有效的 YAML 文件.但是,YAML 1.1 规范(其中有最多的 库支持) 没有提到 JSON.大多数有效的 JSON 文件都是有效的 YAML 1.1 文件,但我通过试验 PyYaml 和 Python 的标准 JSON 库发现了至少一个例外:

YAML 1.2 is (with one minor caveat regarding duplicate keys) a superset of JSON, so any valid JSON file is also a valid YAML file. However, the YAML 1.1 specification (which has the most library support) doesn't mention JSON. Most valid JSON files are valid YAML 1.1 files, but I found at least one exception by experimenting with PyYaml and Python's standard JSON library:

  • 12345e999 这样的双精度浮点溢出被 PyYAML 和 IEEE infinity 来自 Python 的 JSON 库.
  • a double-precision floating-point overflow such as 12345e999 is interpreted as a string by PyYAML and IEEE infinity by Python's JSON library.

有没有人有完整的差异列表,比通过在特定实现中测试边缘案例更可靠地确定?(也就是说,从规范的比较?)例如,我想生成 JSON 字符串,这些字符串将被 JSON 解析器和 YAML 1.1 解析器以相同的方式解释:我必须对我的字符串设置什么约束?

Does anyone have a complete list of differences, determined more robustly than by testing edge cases in a particular implementation? (That is, from a comparison of the specifications?) For example, I want to generate JSON strings that will be interpreted the same way by a JSON parser and a YAML 1.1 parser: what constraints must I place on my strings?

推荐答案

参见这里(特别是脚注 25).它说:

See here (specifically footnote 25). It says:

不兼容的地方如下:JSON 允许扩展字符设置像 UTF-32 并且具有不兼容的 unicode 字符转义语法相对于 YAML;YAML 在逗号等分隔符后需要一个空格,等于和冒号,而 JSON 没有.一些不规范的JSON 的实现扩展了语法以包含 Javascript 的/*...*/评论.处理这种边缘情况可能需要光在解析为内联 YAML 之前对 JSON 进行预处理

The incompatibilities were as follows: JSON allows extended character sets like UTF-32 and had incompatible unicode character escape syntax relative to YAML; YAML required a space after separators like comma, equals, and colon while JSON does not. Some non-standard implementations of JSON extend the grammar to include Javascript's /*...*/ comments. Handling such edge cases may require light pre-processing of the JSON before parsing as in-line YAML

另请参阅 https://metacpan.org/pod/JSON::XS#JSON-and-YAML

相关
YAML 和 JSON 有什么区别?何时更喜欢其中一个

这篇关于哪些有效的 JSON 文件不是有效的 YAML 1.1 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆