Why does the YAML spec mandate a space after the colon?

≡放荡痞女 提交于 2021-02-08 12:19:36

问题


The YAML spec clearly states:

Mappings use a colon and space (“: ”) to mark each key: value pair.

So this is legal:

foo: bar

But this, Ain't:

foo:bar

I see many people online that are ranting about the space. I think they have a point. I got burned by it several times myself.

Why is the space mandatory? What was the design consideration behind it?


回答1:


It's easy to miss, because that specification uses the bizarre convention of only highlighting the last character of an internal link, but the “: ” in the section you quote is actually a link to another section of the specification which answers your question:

Normally, YAML insists the “:” mapping value indicator be separated from the value by white space. A benefit of this restriction is that the “:” character can be used inside plain scalars, as long as it is not followed by white space. This allows for unquoted URLs and timestamps. It is also a potential source for confusion as “a:1” is a plain scalar and not a key: value pair.

So the motivation is that you can write lists such as this without requiring any quoting:

useful_values:
- 2:30
- http://example.com
- localhost:8080

If the space was optional, this could end up being ambiguous, and interpreted as a set of key-value pairs.

Aside: Here's a snippet of JS to make the link formatting on that document less useless.

document.styleSheets[0].insertRule('a[href^="#"] { color: #00A !important; text-decoration: underline !important; background: none !important; }', 0);


来源:https://stackoverflow.com/questions/42124227/why-does-the-yaml-spec-mandate-a-space-after-the-colon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!