今天就跟大家聊聊有關(guān)利用c++怎么對yaml文件進(jìn)行解析,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
先git clone git@github.com:jbeder/yaml-cpp.git
下來編譯成靜態(tài)庫
mkdir build cd build cmake .. make
運(yùn)行完后,會得到libyaml-cpp.a。
新建一個項目,結(jié)構(gòu)大致如下
yaml_demo |__ include |__yaml-cpp 頭文件夾 |__ lib |__yaml-cpp 庫文件夾 |__ main.cpp
配置CMakeLists.txt把頭文件和靜態(tài)庫加到項目里,這樣在編譯和鏈接時才能通過
project(yaml_demo) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) # 二進(jìn)制文件的輸出目錄 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的配置就完成了??匆幌挛业腸onfig文件
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有自己的轉(zhuǎn)換模板
template <typename T> struct convert;
在進(jìn)行轉(zhuǎn)換的時候他會判斷有沒有實(shí)現(xiàn) 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的核心,我們的配置的所有操作都從這個類中進(jìn)行。
我們只要具體化自定義的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轉(zhuǎn)換成yaml-cpp的Node,
轉(zhuǎn)換時可以這樣
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; }
看完上述內(nèi)容,你們對利用c++怎么對yaml文件進(jìn)行解析有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
本文標(biāo)題:利用c++怎么對yaml文件進(jìn)行解析-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://jinyejixie.com/article34/disise.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、響應(yīng)式網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容