c++解析yaml文件

一直用c++操作ini做配置文件,想换成yaml,在全球最大的同性交友网站github上搜索,看有没有开源的库,功夫不负有心人,找到了yaml-cpp,试着解析了一个yaml文件,给个满分。分享一下如何使用他。
git clone git@github.com:jbeder/yaml-cpp.git下,进行build四件套,把他编译成静态库

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

其中apiv是比较简单的键值,我们可以直接读取他们的值

std::cout << "api: " << config["api"].as<std::string>() << std::endl;
std::cout << "v: " << config["v"].as<int>() << std::endl;

label是一个mapcontainers是一个列表,这就要特殊处理一下,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());
  }
};

Nodeyaml-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-cppNode
转换时可以这样

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;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在C++中读写YAML文件,您可以使用第三方库来处理YAML格式。以下是使用`yaml-cpp`库读写YAML文件的简单示例: 首先,您需要安装`yaml-cpp`库。您可以从https://github.com/jbeder/yaml-cpp下载源代码,并根据其提供的说明进行安装。 接下来,您可以使用以下代码示例来读取YAML文件: ```cpp #include <iostream> #include <fstream> #include <yaml-cpp/yaml.h> int main() { // 打开YAML文件 std::ifstream file("example.yaml"); // 解析YAML文件 YAML::Node config = YAML::Load(file); // 读取YAML中的数据 std::string name = config["name"].as<std::string>(); int age = config["age"].as<int>(); // 打印读取的数据 std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; return 0; } ``` 在上面的示例中,我们打开了名为`example.yaml`的YAML文件,并使用`YAML::Load`函数解析文件内容。然后,我们可以使用`[]`操作符来访问读取的数据,并使用`as`函数将其转换为适当的类型。 如果您想要将数据写入YAML文件,可以使用以下代码示例: ```cpp #include <iostream> #include <fstream> #include <yaml-cpp/yaml.h> int main() { // 创建一个YAML节点 YAML::Node config; config["name"] = "John"; config["age"] = 25; // 打开要写入的YAML文件 std::ofstream file("output.yaml"); // 将YAML节点写入文件 file << config; return 0; } ``` 在上面的示例中,我们创建了一个名为`config`的YAML节点,并设置了两个属性。然后,我们打开了名为`output.yaml`的文件,并使用`<<`操作符将YAML节点写入文件。 这些示例演示了如何使用`yaml-cpp`库在C++中读写YAML文件。请确保在编译时链接`yaml-cpp`库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猰貐的新时代

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值