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配置文件来说,更加的简洁,操作也更加简单,同时可以存放不同类型的数据,不会改变原有数据类型,所有的数据类型在读取时 ...
随机推荐
- php(tp5)实现分页效果
public function admin(){ if(request()->isPost()){ //获取第二页的数据传current = 2过来即可 $post['origin'] = in ...
- python-基础入门-3(对文件操作)
打开文件用open()函数 open(filename)默认为读取模式 等价于open(filename,'r') 1 txt=open(filename) 2 print txt.read() 3 ...
- Fruity Granulizer合成器功能简介
本章节采用图文结合的方式给大家介绍电音编曲软件-FL Studio的插件Fruity Granulizer合成器,感兴趣的朋友可以一起沟通交流. Fruity Granulizer合成器是一个使用了粒 ...
- Vegas视频FX功能详解
今天呢,小编就带大家走进Vegas(Win系统)视频FX的世界.那么什么是视频FX呢,就是视频制作软件Vegas中自带添加特效的地方,它可以用于添加模糊,黑白,镜像等滤镜效果,各种高大上的视频大片都需 ...
- 在线思维导图Ayoa共享功能使用教程
Ayoa是一个制作思维导图的软件,除了导图制作,小编在使用过程中还发现了一些令人惊喜的功能,这些功能使得Ayoa有了更大的亮点以吸引用户. 下面就为大家简单介绍几个小编认为Ayoa中较为实用的共享功能 ...
- MindManager中主题间距/线条粗细的灵活调整
在MindManager中,主题和线条是思维导图的基本元素,只有通过它们才能将要表达的思想呈现.并联系起来.因此,关于它们的属性设置就会多一点,如颜色.宽度.位置等.而调整主题之间的距离及线条的粗细, ...
- Unity减少构建安装包的体积(210MB减小到7MB)
概述 项目简介 由于是公司内做的项目,不方便开源,就只分享优化过程吧. 项目信息 逐日是一个移动端单机小游戏,使用Unity开发,目前已将项目使用的Unity升级到2019.4.14f1c1 (3e5 ...
- 使用logisim搭建单周期CPU与添加指令
使用logisim搭建单周期CPU与添加指令 搭建 总设计 借用高老板的图,我们只需要分别做出PC.NPC.IM.RF.EXT.ALU.DM.Controller模块即可,再按图连线,最后进行控制信号 ...
- LeetCode双周赛#34
5492. 分割字符串的方案数 #组合公式 #乘法原理 #区间分割 题目链接 题意 给定01二进制串\(s\),可将\(s\)分割为三个非空 字符串\(s_1,s_2,s_3\),即(\(s_1+s_ ...
- AppWeb认证绕过漏洞(CVE-2018-8715)
AppWeb认证绕过漏洞(CVE-2018-8715) 一.漏洞描述 Appweb简介 Appweb是一个嵌入式HTTP Web服务器,主要的设计思路是安全.这是直接集成到客户的应用和设备,便于开发和 ...