简介:
YAML是一个可读性高,用来表达数据序列的格式。YAML文件扩展名为.yaml或.yml。YAML是”YAML Ain’t a Markup Language”(YAML不是一种标记语言)的递归缩写。在开发这种语言时,YAML的意思其实是:”Yet Another Markup Language”(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。
功能:由于它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件、倾印调试内容、文件大纲。
基本语法:
1.大小写敏感
2.使用缩进表示层级关系
3.缩进不允许使用tab,只允许空格
4.缩进的空格数不重要,只要相同层级的元素左对齐即可
5.’#'表示注释
具体见https://www.runoob.com/w3cnote/yaml-intro.html
实例:
Yaml_test.cpp
// An highlighted block
#include <iostream>
#include "yaml-cpp/yaml.h"
#include <fstream>
using namespace std;
typedef struct
{
int cpp;
int java;
int android;
int python;
}Skills;
typedef struct
{
string name;
string sex;
int age;
Skills skills;
} Config;
int test_base()
{
YAML::Node config;
try{
config = YAML::LoadFile("../config.yaml");
}catch(YAML::BadFile &e){
std::cout<<"read error!"<<std::endl;
return -1;
}
cout << "Node type " << config.Type() << endl;
cout << "skills type " << config["skills"].Type() << endl;
//可以用string类型作为下表,读取参数
string age = "age";
cout << "age when string is label:" << config[age].as<int>() << endl;
cout << "name:" << config["name"].as<string>() << endl;
cout << "sex:" << config["sex"].as<string>() << endl;
cout << "age:" << config["age"].as<int>() << endl;
//读取不存在的node值,报YAML::TypedBadConversion异常
try{
string label = config["label"].as<string>();
}catch(YAML::TypedBadConversion<string> &e){
std::cout<<"label node is NULL"<<std::endl;
}//TypedBadConversion是模板类,读取什么类型的参数就传入什么类型
cout << "skills cpp:" << config["skills"]["cpp"].as<int>() << endl;
cout << "skills java:" << config["skills"]["java"].as<int>() << endl;
cout << "skills android:" << config["skills"]["android"].as<int>() << endl;
cout << "skills python:" << config["skills"]["python"].as<int>() << endl;
for(YAML::const_iterator it= config["skills"].begin(); it != config["skills"].end();++it)
{
cout << it->first.as<string>() << ":" << it->second.as<int>() << endl;
}
YAML::Node test1 = YAML::Load("[1,2,3,4]");
cout << " Type: " << test1.Type() << endl;
YAML::Node test2 = YAML::Load("1");
cout << " Type: " << test2.Type() << endl;
YAML::Node test3 = YAML::Load("{'id':1,'degree':'senior'}");
cout << " Type: " << test3.Type() << endl;
ofstream fout("./testconfig.yaml"); //保存config为yaml文件
config["score"] = 99;//添加新元素
fout << config;
fout.close();
return 0;
}
int use_yaml_config()
{
YAML::Node config;
try{
config = YAML::LoadFile("../config.yaml");
}catch(YAML::BadFile &e){
std::cout<<"read error!"<<std::endl;
return -1;
}
Config param;
//提取, 赋值
param.age = config["age"].as<int>();
param.name = config["name"].as<string>();
param.sex = config["sex"].as<string>();
param.skills.android = config["skills"]["android"].as<int>();
param.skills.java = config["skills"]["java"].as<int>();
param.skills.python = config["skills"]["python"].as<int>();
param.skills.cpp = config["skills"]["cpp"].as<int>();
//查看结果
cout<< " param.age = "<<param.age <<endl;
cout<< " param.name = "<<param.name <<endl;
cout<< " param.sex = "<<param.sex <<endl;
cout<< "param.skills.android = "<<param.skills.android <<endl;
cout<< " param.skills.java = "<<param.skills.java <<endl;
cout<< " param.skills.python = "<<param.skills.python <<endl;
cout<< " param.skills.cpp = "<<param.skills.cpp <<endl;
return 0;
}
int main(int argc,char** argv)
{
use_yaml_config();
return 0;
}
Config.yaml
// An highlighted block
name: frank
sex: male
age: 18
skills:
cpp: 1
java: 2
android: 3
python: 4