关于YAML 规范,有关于问号的第2.11段:
问号和空格("?")表示复杂的映射关键字.在块集合中,键:值对可以在短划线,冒号或问号后立即开始.
给出了这个例子:
---
? - Detroit Tigers
- Chicago cubs
:
- 2001-07-23
Run Code Online (Sandbox Code Playgroud)
另一个示例也无法转换为XML:
%YAML 1.2
- - -
!!map {
? !!str "Not indented"
: !!map {
? !!str "By one space"
: !!str "By four\n spaces\n",
? !!str "Flow style"
: !!seq [
!!str "By two",
!!str "Also by two",
!!str "Still by two",
]
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我不明白它意味着什么.我尝试使用codebeautify将其转换为XML ,但是我收到了一个错误.
所以我的问题是:
问号有什么作用?
Jor*_*ing 48
规格不是很清楚,但经过一番头脑清醒后我才明白了.YAML有两种块映射键:隐式和显式.隐含的风格是你熟悉的那种:
mapping:
foo: 1
bar baz: 2
"qux:quux": 3
Run Code Online (Sandbox Code Playgroud)
如果我们在Ruby中加载这个YAML(例如),我们得到以下结果:
{ "mapping" =>
{ "foo" => 1,
"bar baz" => 2,
"qux:quux" => 3
}
}
Run Code Online (Sandbox Code Playgroud)
但我们也可以使用显式样式来表达同样的事情:
mapping:
? foo
: 1
? bar baz
: 2
? "qux:quux"
: 3
Run Code Online (Sandbox Code Playgroud)
换句话说,以开头的行?表示键,以行开头的行:表示值.
有什么用?好吧,它允许我们使用任何YAML结构作为映射键.想使用序列作为映射密钥?您可以!想使用映射作为映射密钥?看吧:
mapping:
# Use a sequence as a key
? - foo
- bar
: 1
# Use a mapping as a key
? baz: qux
: 2
# You can skip the value, which implies `null`
? quux
# You can leave the key blank, which implies a `null` key
?
: 3
# You can even skip both the key and value, so both will be `null`
?
# Or you can use a preposterously long scalar as a key
? |
We the People of the United States, in Order to form a more
perfect Union, establish Justice, insure domestic Tranquility,
provide for the common defence, promote the general Welfare,
and secure the Blessings of Liberty to ourselves and our
Posterity, do ordain and establish this Constitution for the
United States of America.
: 3
# Or just be ridiculous
? - foo: bar
baz:
- { qux: quux }
- stahp
: 4
Run Code Online (Sandbox Code Playgroud)
在Ruby中,这将产生以下令人愉快的哈希:
{ "mapping" =>
{ [ "foo", "bar" ] => 1,
{ "baz" => "qux" } => 2,
"quux" => nil,
nil => nil,
"We the People of the United States, in Order to form a more\nperfect Union, establish Justice, insure domestic Tranquility,\nprovide for the common defence, promote the general Welfare,\nand secure the Blessings of Liberty to ourselves and our\nPosterity, do ordain and establish this Constitution for the\nUnited States of America.\n" => 3
[ { "foo" => "bar", "baz" => [ { "qux" => "quux" } ] }, "stahp" ] => 4
}
}
Run Code Online (Sandbox Code Playgroud)
哦,当Ruby解析它时,"底特律老虎"的例子看起来像这样:
YAML.load <<YML
? - Detroit Tigers
- Chicago cubs
:
- 2001-07-23
? [ New York Yankees,
Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
2001-08-14 ]
YML
# => { [ "Detroit Tigers", "Chicago cubs" ] =>
# [ #<Date: 2001-07-23 ((2452114j,0s,0n),+0s,2299161j)> ],
# [ "New York Yankees", "Atlanta Braves" ] =>
# [ #<Date: 2001-07-02 ((2452093j,0s,0n),+0s,2299161j)>,
# #<Date: 2001-08-12 ((2452134j,0s,0n),+0s,2299161j)>,
# #<Date: 2001-08-14 ((2452136j,0s,0n),+0s,2299161j)> ]
# }
Run Code Online (Sandbox Code Playgroud)
只是想补充一点,如果您的密钥是特殊的 char ,复杂的映射也会有所帮助。例如,如果您k:v是:
!: "abc"
Run Code Online (Sandbox Code Playgroud)
不允许使用这种特殊的 char 键,因此,您可以这样做:
? "!"
: "abc"
Run Code Online (Sandbox Code Playgroud)