YAML开源库yaml-cpp简介及使用

关于YAML的介绍可以参考:https://blog.csdn.net/fengbingchun/article/details/88090609

yaml-cpp是用c++实现的用来解析和生成yaml文件的,源码地址在https://github.com/jbeder/yaml-cpp 这里使用的是最新发布的稳定版0.6.2.

解析和产生yaml文件的测试代码如下:

#include "funset.hpp"
#include <string>
#include <fstream>
#include <yaml-cpp/yaml.h>

int test_parse_yaml_file()
{
#ifdef _MSC_VER
	YAML::Node config = YAML::LoadFile("E:/GitCode/Messy_Test/testdata/test_yaml-cpp.yml");
#else
	YAML::Node config = YAML::LoadFile("testdata/test_yaml-cpp.yml");
#endif
	
	std::string blog_name{"xxxxx"}, blog_id{"xxxxx"}, blog_url{"xxxxx"};
	if (config["blog"]) {
		if (config["blog"]["name"])
			blog_name = config["blog"]["name"].as<std::string>();
		if (config["blog"]["id"])
			blog_id = config["blog"]["id"].as<std::string>();
		if (config["blog"]["url"])
			blog_url = config["blog"]["url"].as<std::string>();

	} else {
		fprintf(stderr, "the node blog doesn't exist\n");
	}

	fprintf(stdout, "blog name: %s, id: %s, url: %s\n",
		blog_name.c_str(), blog_id.c_str(), blog_url.c_str());

	bool value1, value2;
	if (config["value1"])
		value1 = config["value1"].as<bool>();
	if (config["value2"])
		value2 = config["value2"].as<bool>();

	fprintf(stdout, "value1: %d, value2: %d\n", value1, value2);

	int number1;
	std::string number2, number3;
	float number4;
	if (config["number1"])
		number1 = config["number1"].as<int>();
	if (config["number2"])
		number2 = config["number2"].as<std::string>();
	if (config["number3"])
		number3 = config["number3"].as<std::string>();
	if (config["number4"])
		number4 = config["number4"].as<float>();
	fprintf(stdout, "number1: %d, number2: %s, number3: %s, number4: %f\n",
		number1, number2.c_str(), number3.c_str(), number4);

	std::string github_url, github_repos;
	if (config["github"])
		github_url = config["github"][0].as<std::string>();
		github_repos = config["github"][1].as<std::string>();
	fprintf(stdout, "github url: %s, repos: %s\n", github_url.c_str(), github_repos.c_str());

	return 0;
}

int test_generate_yaml_file()
{
	YAML::Node node;
	
	node["language"] = "cpp";
	node["version"] = 2;	

	node["url"].push_back("https://blog.csdn.net/fengbingchun");
	node["url"].push_back("https://github.com/fengbingchun");

	YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
	primes.push_back(13);
	fprintf(stdout, "primes size: %d\n", primes.size());
	node["primes"] = primes;

	YAML::Node lineup = YAML::Load("{1B: Prince Fielder, 2B: Rickie Weeks, LF: Ryan Braun}");
	lineup["RF"] = "Corey Hart";
	lineup["C"] = "Jonathan Lucroy";
	node["name"] = lineup;

	node["platform"]["linux"].push_back("x86");
	node["platform"]["linux"].push_back("x86_64");
	node["platform"]["linux"].push_back("armv7");
 
	node["platform"]["windows"].push_back("x86");
	node["platform"]["windows"].push_back("x86_64");

#ifdef _MSC_VER
	std::ofstream fout("E:/GitCode/Messy_Test/testdata/tmp.yml");
#else
	std::ofstream fout("testdata/tmp.yaml");
#endif
	fout << node;

	return 0;
}

test_yaml-cpp.yml内容如下:

# config file
---

blog:
  name: csdn
  id: fengbingchun
  url: https://blog.csdn.net/fengbingchun

commands:
  version:
  - g++ --version
  - cmake --version
  - git --version

value1: true
value2: false
value3: ~

number1: 123
number2: !!str 123
number3: "123"
number4: !!float 123

github: 
  - https://github.com/fengbingchun
  - NN_Test Face_Test OpenCV_Test
    Messy_Test CUDA_Test

data1: |
  There once
  was a
     short man

data2: >
  There once
  was a
     short man

date1: 2019-03-03

step: &id001   # defines anchor label &id001
  instrument:  Lasik 2000
  pulseEnergy: 5.4

step2: *id001   # refers to the first step (with anchor &id001)

...

解析yaml文件输出结果如下:

产生yaml文件的内容如下:

GitHub: https://github.com/fengbingchun/Messy_Test 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
YAML是一种人类可读的数据序列化格式,它非常适用于配置文件和数据交换。yaml-cpp是一个C++,用于解析和生成YAML格式的数据。 以下是yaml-cpp使用示例: 1. 安装yaml-cpp 首先,需要安装yaml-cpp。可以使用以下命令在Ubuntu上安装: ``` sudo apt-get install libyaml-cpp-dev ``` 2. 解析YAML文件 假设有一个名为config.yamlYAML文件,其内容如下: ``` name: John Smith age: 30 address: street: 123 Main St. city: Anytown state: CA zip: 12345 ``` 可以使用以下代码解析该文件: ```c++ #include <yaml-cpp/yaml.h> #include <iostream> int main(int argc, char** argv) { YAML::Node config = YAML::LoadFile("config.yaml"); std::string name = config["name"].as<std::string>(); int age = config["age"].as<int>(); std::string street = config["address"]["street"].as<std::string>(); std::string city = config["address"]["city"].as<std::string>(); std::string state = config["address"]["state"].as<std::string>(); int zip = config["address"]["zip"].as<int>(); std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Address: " << street << ", " << city << ", " << state << " " << zip << std::endl; return 0; } ``` 输出结果为: ``` Name: John Smith Age: 30 Address: 123 Main St., Anytown, CA 12345 ``` 3. 生成YAML文件 可以使用以下代码生成一个YAML文件: ```c++ #include <yaml-cpp/yaml.h> #include <iostream> #include <fstream> int main(int argc, char** argv) { YAML::Emitter out; out << YAML::BeginMap; out << YAML::Key << "name" << YAML::Value << "John Smith"; out << YAML::Key << "age" << YAML::Value << 30; out << YAML::Key << "address"; out << YAML::BeginMap; out << YAML::Key << "street" << YAML::Value << "123 Main St."; out << YAML::Key << "city" << YAML::Value << "Anytown"; out << YAML::Key << "state" << YAML::Value << "CA"; out << YAML::Key << "zip" << YAML::Value << 12345; out << YAML::EndMap; out << YAML::EndMap; std::ofstream fout("config.yaml"); fout << out.c_str(); return 0; } ``` 该代码将生成一个名为config.yaml的文件,其内容与上述示例相同。 总结 yaml-cpp是一个非常方便的C++,用于解析和生成YAML格式的数据。在使用时,只需包含头文件,并调用相应的函数即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值