c++ 解析yaml文件
一直用c++操作ini做配置文件,想换成yaml,在全球最大的同性交友网站github上搜索,看有没有开源的库,功夫不负有心人,找到了yaml-cpp,用他解析了一个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文件的更多相关文章
- 使用ruamel.yaml库,解析yaml文件
在实现的需求如下: 同事提供了一个文本文件,内含200多个host与ip的对应关系,希望能在k8s生成pod时,将这些对应关系注入到/etc/hosts中. 网上看文档,这可以通过扩充pod中的hos ...
- python解析yaml文件
YAML语法规则: http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/ 下载PyYAML: http://www.yaml.org/ 解压 ...
- 解析YAML文件
YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean(); yamlMapFactoryBean.setResources(ne ...
- python 解析 yaml文件
import yaml with open("./test.yaml") as f: x = yaml.load(f) print(x) [{'tasks': [{'yum': { ...
- Python--代码1(接口测试:测试用例从数据库读取写到yaml文件中)
一. 从数据库中读取全部接口,并写入yaml文件 数据库中的数据存储格式如下图: import pymysql import os import json # from ruamel import y ...
- YAML文件解析
YAML是“另一种标记语言”的外语缩写,YAML 是一种比JSON(json多层次{ 与 [ 会被搞晕的)更直观的表现形式,展示上更易查错和关系描述.因为不需要一个专业工具就可以排查正确性.YAML目 ...
- python基础——python解析yaml类型文件
一.yaml介绍 yaml全称Yet Another Markup Language(另一种标记语言).采用yaml作为配置文件,文件看起来直观.简洁.方便理解.yaml文件可以解析字典.列表和一些基 ...
- springboot中对yaml文件的解析
一.YAML是“YAML不是一种标记语言”的外语缩写 (见前方参考资料原文内容):但为了强调这种语言以数据做为中心,而不是以置标语言为重点,而用返璞词重新命名.它是一种直观的能够被电脑识别的数据序列化 ...
- yaml文件解析详解
前言 yaml文件是什么?yaml文件其实也是一种配置文件类型,相比较ini,conf配置文件来说,更加的简洁,操作也更加简单,同时可以存放不同类型的数据,不会改变原有数据类型,所有的数据类型在读取时 ...
随机推荐
- [LeetCode题解]92. 反转链表 II | 一次遍历 + 反转
解题思路 将链表分为三部分:[0, m).[m, n].(n, L],其中 L 为链表长度. 第一步:使用虚拟头节点 dummy,用于将 head 也能参与操作: 第二步:找到第 m-1 节点(fir ...
- "三剑客"之awk心中无剑
一.awk介绍 awk 是一种程序语言. 它具有一般程序语言常见的功能. 因awk语言具有某些特点, 如 : 使用直译器(Interpreter)不需先行编译; 变量无类型之分(Typeless), ...
- Guitar Pro教程之理解记谱法
前面的章节我们讲解了很多关于Guitar Pro'的功能使用,今天小编还是采用图文结合的方式为大家讲解它的理解记谱法,对于很多新人来说,在我们看谱之前,我们肯定要先熟悉他的一些功能如何使用以及一些关于 ...
- 和功能相近的虚拟机软件相比,CrossOver的产品优势有哪些?
很多用户其实并不喜欢虚拟机软件,他们只是想用回熟悉的Windows应用程序,因为苹果系统与许多软件并不兼容.无奈之下,他们只能安装虚拟机软件.可是虚拟机软件大多比较笨重并且也相对复杂一些,在后期维护上 ...
- MySQL优化篇(未完待续)
一.优化SQL语句的一般步骤 1.通过 show status命令了解各种sql的执行频率 mysql客户端连接成功后,通过show[session|global] status命令,可以查看服务器的 ...
- CentOS下配置VNC
配置桌面 # 安装gnome桌面环境 yum groupinstall Desktop -y # 安装中文语言支持包(可选) yum groupinstall 'Chinese Support' -y ...
- Mac MySQL 8.0 (免安装版) 主从集群搭建
一.下载解压包 打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/ ,选择面安装版本. 二.解压文件 下载到合适文件夹,解压压缩包. 解压 mys ...
- docker 部署 mongodb 并且开启远程连接
mongodb 使用 docker 部署 mongodb 拉取镜像 docker pull mongo 可以查看镜像是否下载成功 docker images | grep mongo 应该会有如下的显 ...
- HTML5 速览
HTML5 速览 一. HTML5 元素分类 HTML赋值文档内容的结构和含义, 内容呈现由css样式控制 元素选用原则 少亦可为多 标记只应该应内容对语义的需要使用. 有条经验法则是: 问问自己打算 ...
- PyQt(Python+Qt)学习随笔:print标准输出sys.stdout以及stderr重定向QTextBrowser等图形界面对象
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 <在Python实现print标准输出sys.stdout.st ...