# yamlcplus **Repository Path**: tianyayouge/yamlcplus ## Basic Information - **Project Name**: yamlcplus - **Description**: 1、一个读写YAML的C++实现库 2、支持读取和回写YAML文件 3、支持保留原有YAML中的注释、空行 4、支持动态添加新节点,并支持添加删除注释和空行 - **Primary Language**: C++ - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-02-04 - **Last Updated**: 2025-05-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # yamlcplus YAML 1.0 C++11 serializer/deserializer. ## 介绍 1. 一个读写YAML的C++实现库 2. 支持读取和回写YAML文件 3. 支持保留原有YAML中的注释、空行 4. 并支持添加、修改、删除注释和空行 5. 支持动态添加新节点 6. C++11实现,支持WINDOWS、LINUX等 7. 项目是使用QtCreator和qmake管理的,可以直接拷贝src/format/**中的代码到实际项目中使用 ## Quickstart #### main.cpp ```cpp cppc::yaml::YamlNode root; try { cppc::yaml::ParseFile(root, "data/test.yaml"); } catch (std::runtime_error e) { std::cout << e.what() << std::endl; // 加载异常 return; } std::cout << "----检查----" << std::endl; std::cout << std::boolalpha << "has none: " << root.hasSubNode("none") << std::endl; std::cout << std::boolalpha << "has a: " << root.hasSubNode("a") << std::endl; std::cout << std::boolalpha << "has c.c1: " << root.hasSubNode("c.c1", true) << std::endl; std::cout << std::boolalpha << "has d.d1.d12: " << root.hasSubNode("d.d1.d12", true) << std::endl; std::cout << "----取值----" << std::endl; // 对象取值 // 指定默认值,并获返回值是否是传入的默认值 auto aaPair = root["aa"].asStringPair("aaaaaa"); std::cout << "aa,useDefaultValue: " << aaPair.first << ",getValue: " << aaPair.second << std::endl; std::cout << "a-str: " << root["a"].asString() << std::endl; std::cout << "a-int: " << root["a"].asInt() << std::endl; std::cout << std::boolalpha << "b: " << root["b"].asBool() << std::endl; std::cout << std::boolalpha << "b2: " << root["b2"].asBool() << std::endl; std::cout << "bbb: " << root["bbb"].asString("bbbbbb") << std::endl; std::cout << "d.d1.d12: " << root.getSubNode("d.d1.d12", true).asString("d.d1.d12") << std::endl; std::cout << "d.d1.d12: " << root["d"]["d1"]["d12"].asString("d.d1.d12") << std::endl; // 数组取值 cppc::yaml::YamlNode &c1 = root["c"]["c1"]; std::cout << "c1.size: " << c1.size() << std::endl; for (size_t i = 0; i < c1.size(); i++) { std::cout << " c1[" << i << "]: " << c1[i].asInt() << std::endl; } // 数组取值结束 // 流式取值开始 cppc::yaml::YamlNode &e = root["e"]; std::cout << "e.size: " << e.size() << std::endl; for (size_t i = 0; i < e.size(); i++) { cppc::yaml::YamlNode &eSub = e[i]; for (size_t k = 0; k < eSub.size(); k++) { std::cout << " e[" << i << "][" << k << "]: " << eSub[k].asDouble() << std::endl; } } cppc::yaml::YamlNode &f = root["f"]; std::cout << "f.size: " << f.size() << std::endl; std::vector fKeys = f.getSubNodeKeyList(); for (size_t i = 0; i < fKeys.size(); i++) { cppc::yaml::YamlNode &fSub = f[fKeys[i]]; if (fSub.isScalar()) { std::cout << " f[\"" << fKeys[i] << "\"]: " << fSub.asString() << std::endl; } } // 流式取值结束 // 赋值 root["a"] = 456; // 添加新值 cppc::yaml::YamlNode &version = root.pushFront(); version.setNodeKey("root"); version = ("1.0.0"); version.setComment("版本号"); // 添加新数组 cppc::yaml::YamlNode &seq1 = root["seq1"]; seq1.initSequence(); // 新加对象作为数组 seq1[0] = 11; seq1[1] = 22; // 添加新流式数组 cppc::yaml::YamlNode &seq2 = root["seq2"]; seq2.initSequence(); // 新加对象作为数组 seq2.setIsFlow(true); seq2[0] = 11; seq2[1] = 22; cppc::yaml::YamlNode &seq2Before = root.insertBefore(seq2); seq2Before.initEmptyLine(); seq2Before.setComment("seq2的注释"); // 文本插入空行 root.pushEmptyLine(); cppc::yaml::YamlNode &emptyLine = root.pushBack(false); emptyLine.initEmptyLine(); // 文本插入空行,并设置注释内容 emptyLine = root.pushBack(false); emptyLine.initEmptyLine(); emptyLine.setComment("最后一行注释"); try { cppc::yaml::SerializeToFile(root, "data/test_new.yaml"); } catch (std::runtime_error e) { std::cout << e.what() << std::endl; // 序列化至文件异常 return; } ``` ## 其他 1、本代码参考了mini-yaml-master项目中的设计思想 ## Todo - 支持标签和锚点