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

查看:153
本文介绍了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"
}

(在线验证我知道为什么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第5.5节:

YAML可以识别两个空格字符:空格 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天全站免登陆