无法读取嵌套映射(引发YAML :: InvalidScalar)

时间:2010-06-28 18:59:18

标签: yaml-cpp

我有一个类(包含一些标量值和一个浮点数向量),我想读取并写一个实例作为另一个映射的值。

    
// write

 out << YAML::Key << "my_queue" << YAML::Value << my_queue;



// read (other code cut out...)
 for (YAML::Iterator it=doc.begin();it!=doc.end();++it)
  {
    std::string key, value;
    it.first() >> key;
    it.second() >> value;
    if (key.compare("my_queue") == 0) {
      *it >> my_queue;
    }
 }

写这个课程非常有效,但无论我做什么,我都无法阅读。它不断抛出InvalidScalar。

Caught YAML::InvalidScalar  yaml-cpp: error at line 20, column 13: invalid scalar

这就是输出(用yaml-cpp编写而没有报告任何错误)看起来像:

Other Number: 80
my_queue:
  size: 20
  data:
    - 3.5
    - -1
    - -1.5
    - 0.25
    - -24.75
    - -5.75
    - 2.75
    - -33.55
    - 7.25
    - -11
    - 15
    - 37.5
    - -3.75
    - -28.25
    - 18.5
    - 14.25
    - -36.5
    - 6.75
    - -0.75
    - 14
  max_size: 20
  mean: -0.0355586
  stdev: 34.8981
even_more_data: 1277150400

文档似乎说这是支持的用法,一个嵌套的映射,在这种情况下,序列作为其中一个值。它抱怨它是一个InvalidScalar,即使我做的第一件事就是告诉它这是一张地图:

YAML::Emitter& operator << ( YAML::Emitter& out, const MeanStd& w )
{
  out << YAML::BeginMap;
  out << YAML::Key << "size";
  out << YAML::Value << w.size();

  out << YAML::Key << "data";
  out << YAML::Value << YAML::BeginSeq;
  for(Noor::Number i=0; i<w.size(); ++i) {
    out << w[i];
  }
  out << YAML::EndSeq;
  out << YAML::Key << "max_size";
  out << YAML::Value << w.get_max_size();
  out << YAML::Key << "mean";
  out << YAML::Value << w.mean();
  out << YAML::Key << "stdev";
  out << YAML::Value << w.stdev();

  out << YAML::EndMap;
  return out;
}

是否有人发现此问题?

2 个答案:

答案 0 :(得分:1)

当您阅读YAML时:

std::string key, value;
it.first() >> key;
it.second() >> value; // ***
if (key.compare("my_queue") == 0) {
  *it >> my_queue;
}

标记的行尝试将键/值对的读取为标量(std::string);这就是为什么它告诉你它是一个无效的标量。相反,你想要:

std::string key, value;
it.first() >> key;
if (key.compare("my_queue") == 0) {
  it.second() >> my_queue;
} else {
  // ...
  // for example: it.second() >> value;
}

答案 1 :(得分:0)

YAML::Node internalconfig_yaml = YAML::LoadFile(configFileName);
const YAML::Node &node = internalconfig_yaml["config"];
for(const auto& it : node )
{
    std::cout << "\nnested Key: " << it.first.as<std::string>() << "\n";
    if (it.second.Type() == YAML::NodeType::Scalar)
    {
        std::cout << "\nnested value: " << std::to_string(it.second.as<int>()) << "\n";
    }
    if (it.second.Type() == YAML::NodeType::Sequence)
    {
        std::vector<std::string> temp_vect;
        const YAML::Node &nestd_node2 = it.second;
        for(const auto& it2 : nestd_node2)
        {
            if (*it2)
            {
                std::cout << "\nnested sequence value: " << it2.as<std::string>() << "\n";
                temp_vect.push_back(it2.as<std::string>());
             }
        }
        std::ostringstream oss;
        std::copy(temp_vect.begin(), temp_vect.end(),                 
                  std::ostream_iterator<std::string>(oss, ","));
        std::cout << "\nnested sequence as string: " <<oss.str() << "\n";
    }
}
if (it2.second.Type() == YAML::NodeType::Map)
{
 // Iterate Recursively again !!
 
}