yaml解析的方式? (yaml-cpp)

发布于 2024-10-19 02:43:20 字数 1023 浏览 2 评论 0原文

我制作了一个如下所示的 yaml 文件。

Define1: &Define1
  0: zero
  1: one

Define2:
  <<: *Define1
  2: two

并在 在线 YAML 解析器中进行了尝试。结果如下。 (只需了解节点的构造方式即可。)

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "0": "zero", 
    "1": "one", 
    "2": "two"
  }
}

当然,我期望“yaml-cpp”会以相同的方式解析,但它在某种程度上有所不同。

我想是这样的。 (几乎可以肯定)

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "Define1": {
      "0": "zero", 
      "1": "one"
    },  
    "2": "two"
  }
}

什么鬼!那么我必须在循环时检查节点类型吗?

这是一个已知问题吗?或者'yaml-cpp'只是这样解析?

这段代码就是我的做法。

// already parsed
const YAML::Node& node = &(docYAML)["Define2"];

for (YAML::Iterator it=node->begin(); it!=node->end(); ++it)
{
    const YAML::Node& nodeList = it.second();

    std::string str;
    nodeList[0] >> str;
}

I've made a yaml file like below.

Define1: &Define1
  0: zero
  1: one

Define2:
  <<: *Define1
  2: two

And tried in Online YAML parser. The result is like below. ( Just get how nodes are constructed. )

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "0": "zero", 
    "1": "one", 
    "2": "two"
  }
}

Of course I expected 'yaml-cpp' would parse same way but it's somehow different.

I guess it's like this. (Almost sure)

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "Define1": {
      "0": "zero", 
      "1": "one"
    },  
    "2": "two"
  }
}

What the hell! Then do I have to check node type while looping?

Is this a known issue? or 'yaml-cpp' just parse that way?

This code is how I did.

// already parsed
const YAML::Node& node = &(docYAML)["Define2"];

for (YAML::Iterator it=node->begin(); it!=node->end(); ++it)
{
    const YAML::Node& nodeList = it.second();

    std::string str;
    nodeList[0] >> str;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-10-26 02:43:20

yaml-cpp 尚未实现“合并”键。如果您想跟踪该问题直至其实施,请参阅 http:// /code.google.com/p/yaml-cpp/issues/detail?id=41

目前,yaml-cpp 实际上将您的 YAML 文件解析为:

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "<<": {
      "0": "zero", 
      "1": "one"
    },  
    "2": "two"
  }
}

yaml-cpp doesn't implement the "merge" key yet. If you want to follow the issue until it's implemented, see http://code.google.com/p/yaml-cpp/issues/detail?id=41.

For now, yaml-cpp is actually parsing your YAML file as:

{
  "Define1": {
    "0": "zero", 
    "1": "one"
  }, 
  "Define2": {
    "<<": {
      "0": "zero", 
      "1": "one"
    },  
    "2": "two"
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文