ZBLOG

yaml-cpp 自定解析结构体 struct NsStore { std::string appId; std::string clusterName; std::string namespaceName; std::string comment; std::string format; bool isPublic; std::vector items; ...

可以使用 yaml-cpp 库的编码和解码接口,将结构体对象转换为 YAML 格式的字符串或将 YAML 格式的字符串转换为结构体对象。

下面是具体的代码实现:

  1. #include <yaml-cpp/yaml.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. struct NsStore
  7. {
  8. std::string appId;
  9. std::string clusterName;
  10. std::string namespaceName;
  11. std::string comment;
  12. std::string format;
  13. bool isPublic;
  14. std::vector<std::map<std::string, std::string>> items;
  15. std::string dataChangeCreatedBy;
  16. std::string dataChangeLastModifiedBy;
  17. std::string dataChangeCreatedTime;
  18. std::string dataChangeLastModifiedTime;
  19. };
  20. namespace YAML
  21. {
  22. template <>
  23. struct convert<NsStore>
  24. {
  25. static Node encode(const NsStore& store)
  26. {
  27. Node node;
  28. node["appId"] = store.appId;
  29. node["clusterName"] = store.clusterName;
  30. node["namespaceName"] = store.namespaceName;
  31. node["comment"] = store.comment;
  32. node["format"] = store.format;
  33. node["isPublic"] = store.isPublic;
  34. node["dataChangeCreatedBy"] = store.dataChangeCreatedBy;
  35. node["dataChangeLastModifiedBy"] = store.dataChangeLastModifiedBy;
  36. node["dataChangeCreatedTime"] = store.dataChangeCreatedTime;
  37. node["dataChangeLastModifiedTime"] = store.dataChangeLastModifiedTime;
  38. Node items_node;
  39. for (const auto& item : store.items)
  40. {
  41. Node item_node;
  42. for (const auto& kv : item)
  43. {
  44. item_node[kv.first] = kv.second;
  45. }
  46. items_node.push_back(item_node);
  47. }
  48. node["items"] = items_node;
  49. return node;
  50. }
  51. static bool decode(const Node& node, NsStore& store)
  52. {
  53. if (!node.IsMap())
  54. {
  55. return false;
  56. }
  57. store.appId = node["appId"].as<std::string>();
  58. store.clusterName = node["clusterName"].as<std::string>();
  59. store.namespaceName = node["namespaceName"].as<std::string>();
  60. store.comment = node["comment"].as<std::string>();
  61. store.format = node["format"].as<std::string>();
  62. store.isPublic = node["isPublic"].as<bool>();
  63. store.dataChangeCreatedBy = node["dataChangeCreatedBy"].as<std::string>();
  64. store.dataChangeLastModifiedBy = node["dataChangeLastModifiedBy"].as<std::string>();
  65. store.dataChangeCreatedTime = node["dataChangeCreatedTime"].as<std::string>();
  66. store.dataChangeLastModifiedTime = node["dataChangeLastModifiedTime"].as<std::string>();
  67. const auto& items_node = node["items"];
  68. store.items.reserve(items_node.size());
  69. for (const auto& item_node : items_node)
  70. {
  71. std::map<std::string, std::string> item;
  72. for (const auto& kv : item_node)
  73. {
  74. item[kv.first.as<std::string>()] = kv.second.as<std::string>();
  75. }
  76. store.items.push_back(std::move(item));
  77. }
  78. return true;
  79. }
  80. };
  81. }
  82. int main()
  83. {
  84. try
  85. {
  86. NsStore store;
  87. store.appId = "some-app-id";
  88. store.clusterName = "some-cluster-name";
  89. store.namespaceName = "some-namespace-name";
  90. store.comment = "some-comment";
  91. store.format = "some-format";
  92. store.isPublic = true;
  93. store.dataChangeCreatedBy = "some-user";
  94. store.dataChangeLastModifiedBy = "some-user";
  95. store.dataChangeCreatedTime = "2022-01-01T00:00:00Z";
  96. store.dataChangeLastModifiedTime = "2022-01-02T00:00:00Z";
  97. store.items.push_back({ {"key1", "value1"}, {"key2", "value2"} });
  98. store.items.push_back({ {"key3", "value3"}, {"key4", "value4"} });
  99. std::cout << "=== Encode ===\n";
  100. std::cout << YAML::Dump(store) << "\n";
  101. const std::string yaml_str =
  102. "appId: some-app-id\n"
  103. "clusterName: some-cluster-name\n"
  104. "namespaceName: some-namespace-name\n"
  105. "comment: some-comment\n"
  106. "format: some-format\n"
  107. "isPublic: true\n"
  108. "items:\n"
  109. " - {key1: value1, key2: value2}\n"
  110. " - {key3: value3, key4: value4}\n"
  111. "dataChangeCreatedBy: some-user\n"
  112. "dataChangeLastModifiedBy: some-user\n"
  113. "dataChangeCreatedTime: '2022-01-01T00:00:00Z'\n"
  114. "dataChangeLastModifiedTime: '2022-01-02T00:00:00Z'\n";
  115. std::cout << "=== Decode ===\n";
  116. const YAML::Node node = YAML::Load(yaml_str);
  117. NsStore store2;
  118. if (node.IsDefined() && !node.IsNull())
  119. {
  120. store2 = node.as<NsStore>();
  121. }
  122. std::cout << "appId=" << store2.appId << "\n";
  123. std::cout << "clusterName=" << store2.clusterName << "\n";
  124. std::cout << "namespaceName=" << store2.namespaceName << "\n";
  125. std::cout << "comment=" << store2.comment << "\n";
  126. std::cout << "format=" << store2.format << "\n";
  127. std::cout << "isPublic=" << store2.isPublic << "\n";
  128. std::cout << "dataChangeCreatedBy=" << store2.dataChangeCreatedBy << "\n";
  129. std::cout << "dataChangeLastModifiedBy=" << store2.dataChangeLastModifiedBy << "\n";
  130. std::cout << "dataChangeCreatedTime=" << store2.dataChangeCreatedTime << "\n";
  131. std::cout << "dataChangeLastModifiedTime=" << store2.dataChangeLastModifiedTime << "\n";
  132. std::cout << "items:\n";
  133. for (const auto& item : store2.items)
  134. {
  135. for (const auto& kv : item)
  136. {
  137. std::cout << " " << kv.first << "=" << kv.second << "\n";
  138. }
  139. }
  140. }
  141. catch (const YAML::Exception& ex)
  142. {
  143. std::cerr << ex.what() << "\n";
  144. return 1;
  145. }
  146. return 0;
  147. }

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=251

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?