YAML中的单个感叹号有什么作用?

Dar*_*era 37 .net tags parsing yaml yamldotnet

我正在使用YamlDotNet库,我在加载YAML文件时遇到此错误:

解析标记时,未找到预期的标记URI.

YAML文件应该是格式良好的,因为它来自RoR.该错误似乎是由以下代码触发的:

formats:
  default: ! '%d-%m-%Y'
  long: ! '%d %B, %Y'
  short: ! '%d %b'
Run Code Online (Sandbox Code Playgroud)

我不是专家,但我从YAML规范中看到,您可以使用感叹号来指示自定义对象/类型,并使用两个感叹号来指示显式内置类型.

obj1: !custom # whatever
obj2: !!str "My string"
Run Code Online (Sandbox Code Playgroud)

但是,我无法找到任何对上面使用的感叹号的引用.这是什么意思,为什么我使用的YAML库似乎无法解析它?请注意,如果我删除这些惊叹号,该文件将被解析.

Jua*_*ado 48

那个"!" 是"非特定标签".

YAML规范1.2保持(也是1.1):

通过显式指定"!"非特定标记属性,该节点将根据其类型解析为"vanilla"序列,映射或字符串.

看看这里的标签"语法":

none    : Unspecified tag (automatically resolved by application).
'!'     : Non-specific tag (by default, "!!map"/"!!seq"/"!!str").
'!foo'  : Primary (by convention, means a local "!foo" tag).
'!!foo' : Secondary (by convention, means "tag:yaml.org,2002:foo").
'!h!foo': Requires "%TAG !h! <prefix>" (and then means "<prefix>foo").
'!<foo>': Verbatim tag (always means "foo").
Run Code Online (Sandbox Code Playgroud)

为什么YamlDotNet会抛出错误?我不能100%肯定,但我认为你发现了一个错误.

YamlDotNet是一个端口LibYAML,所以很容易比较的来源.

scanner.c的第2635行(LibYAML):

/* Check if the tag is non-empty. */
if (!length) {
Run Code Online (Sandbox Code Playgroud)

Scanner.cs的第2146行(YamlDotNet):

// Check if the tag is non-empty.
if (tag.Length == 0)
Run Code Online (Sandbox Code Playgroud)

我知道,两者看起来非常相似,但此时length为1并且tag.Length为0.原始C代码处理初始"!" (全长)但C#没有这样做(只是标签"名称"长度).

向项目提交问题.

  • 解释有点不透明。您有使用`!`(或您提到的任何变体)的示例吗? (3认同)
  • 我同意 Burhan 的观点,如果没有更多的 YAML 经验,这是不可能破译的。这个答案详细阐述了标签本身:/sf/answers/1203456901/ (2认同)