YAML 作为 JSON 超集和 TAB 字符 [英] YAML as a JSON superset and TAB characters

查看:28
本文介绍了YAML 作为 JSON 超集和 TAB 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法准确找到此错误的引用,但 YAML 1.2 表示它是 JSON 超集,如果我在 JSON 中使用制表符,它会将其视为错误.

I am unable to find a reference to this error exactly, but YAML 1.2 says it's a JSON superset, and if I use tab characters in a JSON it treats it like an error.

例如

"root": {
        "key": "value"
}

(在线验证here 表示 ' ' 不能启动任何令牌)

我知道为什么 YAML 历来不允许选项卡,但我如何在 JSON 超集的上下文中解释这一点?

I know why YAML historically disallows tabs, but how can I interpret this in the context of JSON-superset?

(例如,YAML 不是一个实际的超集还是 JSON 也不允许选项卡?或者在这种情况下规范确实允许选项卡但尚未实现?)

(e.g. Is YAML not an actual superset or does JSON also disallow tabs? Or the spec does allow for tabs in this case but the implementation is not there yet?)

谢谢.

推荐答案

制表符在 YAML 中是允许的,但仅限于不适用缩进的地方.

Tabs ARE allowed in YAML, but only where indentation does not apply.

根据YAML 1.2 Section 5.5:

YAML 识别两个空格字符:空格制表符.

YAML recognizes two white space characters: space and tab.

以下示例将使用 · 表示空格,使用 表示制表符.所有示例都可以使用官方 YAML 参考解析器进行验证.

The following examples will use · to denote spaces and to denote tabs. All examples can be validated using the official YAML Reference Parser.

YAML 有块样式和流样式.在块样式中,缩进决定了文档的结构.以下文档使用块样式.

YAML has a block style and flow style. In block style, indentation determines the structure of a document. The following document uses block style.

root:
··key: value

验证

在流式中,特殊字符表示文档的结构.以下等效文档使用流样式.

In flow style, special characters indicate the structure of the document. The following equivalent document uses flow style.

{
→ root: {
→ → key: value
→ }
}

验证

您甚至可以在流样式中混合缩进.

You can even mix indentation in flow style.

{
→ root: {
··→ key: value
····}
}

验证

如果您混合使用块和流样式,则整个流样式部分必须尊重块样式缩进.

If you're mixing block and flow style, the entire flow style part must respect the block style indentation.

root:
··{
····key: value
··}

验证

但您仍然可以在流样式部分中混合缩进.

But you can still mix your indentation within the flow style part.

root:
··{
··→ key: value
··}

验证

如果您有一个有价文件,您可以用各种空格包围该值.

If you have a single value document, you can surround the value with all manner of whitespace.

→ ··value··→ 

验证

关键是,每个被解析为 YAML 的 JSON 文档都会将文档放入支持制表符的流样式(因为初始的 {[ 字符),除非它是单值 JSON 文档,在这种情况下 YAML 仍然允许使用空格填充.

The point is, every JSON document that is parsed as YAML will put the document into flow style (because of the initial { or [ character) which supports tabs, unless it is a single value JSON document, in which case YAML still allows padding with whitespace.

如果 YAML 解析器由于 JSON 文档中的制表符而抛出异常,则它不是有效的解析器.

If a YAML parser throws because of tabs in a JSON document, then it is not a valid parser.

话虽如此,您的示例失败了,因为如果块样式映射值与映射名称不在同一行,则必须始终缩进.

That being said, your example is failing because a block style mapping value must always be indented if it's not on the same line as the mapping name.

root: {
··key: value
}

无效,但是

root:
··{
····key: value
··}

有效,并且

root: { key: value }

也是有效.

这篇关于YAML 作为 JSON 超集和 TAB 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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