相关文章推荐
有胆有识的鸵鸟  ·  语义分割之 标签生成_语义分割标签·  2 月前    · 
路过的大象  ·  【可视化】图像分割中标签label与预测ma ...·  2 月前    · 
打篮球的煎饼  ·  渲染 Vue 组件列表 - 学习 Web ...·  1 周前    · 
爱喝酒的紫菜汤  ·  Velocity ...·  1 年前    · 
高兴的烈酒  ·  在vscode中使用c的printf函数没有 ...·  1 年前    · 
读研的葫芦  ·  如何使用 frp ...·  1 年前    · 
耍酷的书签  ·  Vue突然报错 doesn‘t work ...·  1 年前    · 
闯红灯的馒头  ·  10亿数据如何快速插入MySQL - ...·  1 年前    · 
Code  ›  c++ 解析yaml文件开发者社区
string yaml config label
https://cloud.tencent.com/developer/article/1762157
慷慨的水煮肉
1 年前
作者头像
lpxxn
0 篇文章

c++ 解析yaml文件

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 技术之路 > c++ 解析yaml文件

c++ 解析yaml文件

作者头像
lpxxn
发布 于 2020-12-17 11:27:50
1.4K 0
发布 于 2020-12-17 11:27:50
举报

一直用c++操作 ini 做配置文件,想换成 yaml ,在全球最大的同性交友网站 github 上搜索,看有没有开源的库,功夫不负有心人,找到了 yaml-cpp ,试着解析了一个 yaml 文件,给个满分。分享一下如何使用他。 先 git clone git@github.com:jbeder/yaml-cpp.git 下,进行 build 四件套,把他编译成静态库

mkdir build
cd build
cmake ..
make

运行完后,会得到 libyaml-cpp.a 。 新建一个项目,结构大致如下

yaml_demo
  |__ include
         |__yaml-cpp 头文件夹
  |__ lib 
         |__yaml-cpp 库文件夹
  |__ main.cpp

把头文件和库拷贝到相应的文件夹内。 配置 CMakeLists.txt 把头文件和静态库加到项目里,这样在编译和链接时才能通过

project(yaml_demo)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) # 二进制文件的输出目录
link_directories(${PROJECT_SOURCE_DIR}/lib/yaml-cpp)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME} yaml-cpp.a) 

对 yaml-cpp 的配置就完成了。看一下我的 config 文件

api: aaaaa
label:
  app: hello
  image: abc
containers:
  - name: abc
    age: 18
  - name: 222
    age: 12

其中 api 和 v 是比较简单的键值,我们可以直接读取他们的值

    std::cout << "api: " << config["api"].as<std::string>() << std::endl;
    std::cout << "v: " << config["v"].as<int>() << std::endl;

label 是一个 map , containers 是一个列表,这就要特殊处理一下, yaml-cpp 有自己的转换模板

template <typename T>
struct convert;

在进行转换的时候他会判断有没有实现 decode 方法

struct as_if<T, void> {
  explicit as_if(const Node& node_) : node(node_) {}
  const Node& node;
  T operator()() const {
    if (!node.m_pNode)
      throw TypedBadConversion<T>(node.Mark());
    if (convert<T>::decode(node, t))
      return t;
    throw TypedBadConversion<T>(node.Mark());
};

Node 是 yaml-cpp 的核心,我们的配置的所有操作都从这个类中进行。 我们只要具体化自定义的 struct 就可以使用了

struct label {
    std::string app;
    std::string image;
namespace YAML {
    template<>
    struct convert<label> {
        static Node encode(const label &rhs) {
            Node node;
            node.push_back(rhs.app);
            node.push_back(rhs.image);
            return node;
        static bool decode(const Node &node, label &rhs) {
            std::cout << node.Type() << std::endl;
            rhs.app = node["app"].as<std::string>();
            rhs.image = node["image"].as<std::string>();
            return true;
}

encode 方法是把我们自定义的 struct 转换成 yaml-cpp 的 Node , 转换时可以这样

if (config["label"]) {
    label l = config["label"].as<label>();
    std::cout << "app: " << l.app << " image: " << l.image << std::endl;
}

container 也是一样的具体化

struct container {
    std::string name;
    int age;
namespace YAML {
    template<>
    struct convert<container> {
        static Node encode(const container &rhs) {
            Node node;
            node.push_back(rhs.name);
            node.push_back(rhs.age);
            return node;
        static bool decode(const Node &node, container &rhs) {
            rhs.name = node["name"].as<std::string>();
            rhs.age = node["age"].as<int>();
            return true;
}

完整代码如下:

#include <string>
#include <iostream>
#include <yaml-cpp/yaml.h>
#include <yaml-cpp/node/parse.h>
struct container {
    std::string name;
    int age;
namespace YAML {
    template<>
    struct convert<container> {
        static Node encode(const container &rhs) {
            Node node;
            node.push_back(rhs.name);
            node.push_back(rhs.age);
            return node;
        static bool decode(const Node &node, container &rhs) {
            rhs.name = node["name"].as<std::string>();
            rhs.age = node["age"].as<int>();
            return true;
struct label {
    std::string app;
    std::string image;
namespace YAML {
    template<>
    struct convert<label> {
        static Node encode(const label &rhs) {
            Node node;
            node.push_back(rhs.app);
            node.push_back(rhs.image);
            return node;
        static bool decode(const Node &node, label &rhs) {
            std::cout << node.Type() << std::endl;
            rhs.app = node["app"].as<std::string>();
            rhs.image = node["image"].as<std::string>();
            return true;
int main(int argc, char **argv) {
    std::string config_path = "./config.yaml";
    std::cout << config_path << std::endl;
    YAML::Node config = YAML::LoadFile(config_path);
    std::cout << "api: " << config["api"].as<std::string>() << std::endl;
    std::cout << "v: " << config["v"].as<int>() << std::endl;
    if (config["label"]) {
        label l = config["label"].as<label>();
        std::cout << "app: " << l.app << " image: " << l.image << std::endl;
    if (config["containers"]) {
        std::vector<container> vi = config["containers"].as<std::vector<container>>();
        for (std::vector<container>::iterator it = vi.begin(); it != vi.end(); ++it) {
 
推荐文章
有胆有识的鸵鸟  ·  语义分割之 标签生成_语义分割标签
2 月前
路过的大象  ·  【可视化】图像分割中标签label与预测mask的两种可视化方法_如何可视化分割.nii.gz预测和真实标签不同区域用分割线表示
2 月前
打篮球的煎饼  ·  渲染 Vue 组件列表 - 学习 Web 开发 | MDN
1 周前
爱喝酒的紫菜汤  ·  Velocity 读取字符串模板生成代码-阿里云开发者社区
1 年前
高兴的烈酒  ·  在vscode中使用c的printf函数没有显示问题也没有显示输出是什么情况? - 知乎
1 年前
读研的葫芦  ·  如何使用 frp 内网穿透将本地服务暴露在外网上 - 掘金
1 年前
耍酷的书签  ·  Vue突然报错 doesn‘t work properly without JavaScript enabled_doesn't work properly without javascript en
1 年前
闯红灯的馒头  ·  10亿数据如何快速插入MySQL - zydbky - 博客园
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号