【发布时间】:2016-08-24 14:35:06
【问题描述】:
我有一个尝试使用 yaml-cpp 解析的测试 yaml 文件。
test.yaml
testConfig:
# this points to additional config files to be parsed
includes:
required: "thing1.yaml"
optional: "thing2.yaml"
#some extraneous config information
foo: 42
bar: 394
baz: 8675309
我解析它我得到testConfig.Type() 返回YAML::NodeType::Map。这是预期的行为。
然后,我尝试解析包含以获取我无法迭代的必需或可选值,因为includes.Type() 返回YAML::NodeType::Undefined。我对 yaml 和 yaml-cpp 真的很陌生,所以任何能告诉我哪里出错的帮助都将不胜感激。
解析代码:
{includes and other such nonsense}
.
.
.
YAML::Node configRoot = YAML::LoadFile(path.c_str() );
if( configRoot.IsNull() )
{
SYSTEM_LOG_ERROR("Failed to load the config file: %s.",
path.c_str());
return false;
}
YAML::Node includes = configRoot["includes"];
/* ^^^^^^^^^^^^^^^
* I believe that here lies the issue as includes is undefined and
* therefore I cannot iterate over it.
*/
for( auto it = include.begin(); it != include.end(); ++it )
{
// do some fantastically brilliant CS voodoo!
}
.
.
.
{ more C++ craziness to follow }
解决方案:
我删除了不必要的顶级 configTest,以便我可以根据需要解析包含。
【问题讨论】:
-
YAML 文件中无需在
thingX.yaml周围加上引号 -
@Anthon 感谢您提供的信息...我习惯了 json,其中似乎几乎所有内容都需要引号。