温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用c++怎么对yaml文件进行解析

发布时间:2020-12-15 14:36:12 来源:亿速云 阅读:947 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关利用c++怎么对yaml文件进行解析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

git clone git@github.com:jbeder/yaml-cpp.git下来编译成静态库

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
v: 1
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());

  T t;
  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) {
      std::cout << "vector: name: " << it->name << " age: " << it->age << std::endl;
    }
  }

  return 0;
}

看完上述内容,你们对利用c++怎么对yaml文件进行解析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI