Special significance to single 'n' character in YAML?

丶灬走出姿态 提交于 2021-01-27 20:31:04

问题


Does a single n character hold any special significance in YAML? I'm asking because my Sublime 3 Text Editor highlights it with a different color than other string values and I don't really understand the reason for it.


回答1:


TL;DR; That n is the, pre-2009, canonical representation in YAML for the boolean "False" value.

Sublime should not highlight that by default as n hasn't had such a representation in YAML since 2009. If at all, it should only highlight that n if you explicitly specified to be editing a YAML 1.1 file via the interface, or if the document was recognised as such via the directive %YAML 1.1 at the top of the document.


In YAML a scalar can have different formats (from the YAML 1.2 specification):

YAML allows scalars to be presented in several formats. For example, the integer “11” might also be written as “0xB”. Tags must specify a mechanism for converting the formatted content to a canonical form for use in equality testing. Like node style, the format is a presentation detail and is not reflected in the serialization tree and representation graph.

The older 1.1 specification has a different second sentence in that paragraph, that directly mentions booleans:

For example, the boolean “true” might also be written as “yes”.

How a YAML scalar is constructed in your programming language depends on the schema the parser uses. The most basic schema constructs every scalar as a string (i.e. no interpretation).

In more practical schema, there is the canonical form (typically used when representing a computer programming type like an integer, boolean, date-time-stamp, etc.) and the matching scalars (often via regular expressions) that allow construction as such a type. E.g 42 is the canonical representation, but on the input side 0x2a and 0o52 are allowed as well for the same integer value.

In YAML 1.2 the core schemas for booleans were sanitized to the canonical forms true and false while matching true, True, TRUE, false, False and FALSE— an extension the simpler JSON schema that only allows true and false.

This was changed extensively from YAML 1.1 where canonical representation was y and n while matching all of y, Y, yes, Yes, YES, n, N, no, No, NO, true, True, TRUE, false, False, FALSE, on, On, ON, off, Off, OFF. This often led to confusion, as matching strings in a program had to be represented in YAML with explicit quotes, in order not to be interpreted as booleans on loading.

Sublime seems to support the YAML 1.1 specification where a boolean could be written as n. Since the YAML 1.2 specification is from 2009, I really don't thing Sublime is correct in doing so, and should not interpret n as a boolean, unless the YAML document starts with an explicit directive %YAML 1.1. (Since you gave no indication of that in the interface, and since it is not in the document, I don't think you have).

Of course Sublime's original release preceded the YAML 1.2 specification, but you would hope that after 8 years they would have corrected this by an update. Another such change are scalars that start with 0, like 0123, which are strings in YAML 1.2 but were an (octal) integers in YAML 1.1. Maybe you use that as a test to see if the YAML mode has been correctly updated: 0123 should not be highlighted in your editor unless you explicitly specified YAML 1.1.

There are of course outdated libraries that only support YAML 1.1, and you might even be using one, but that is no excuse for that being the default in an editor.



来源:https://stackoverflow.com/questions/44211899/special-significance-to-single-n-character-in-yaml

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