testJsonConvert.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #define CATCH_CONFIG_MAIN
  2. #include <catch2.hpp>
  3. #include "JsonConvertHelper.hpp"
  4. TEST_CASE("从JSON字符串中获取结构体内容", "[json]")
  5. {
  6. struct DemoObjct
  7. {
  8. bool boolValue;
  9. int intValue;
  10. std::string strValue;
  11. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue,intValue,strValue)
  12. };
  13. DemoObjct demoObj;
  14. REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\"}")));
  15. REQUIRE(demoObj.boolValue == true);
  16. REQUIRE(demoObj.intValue == 1234);
  17. REQUIRE(demoObj.strValue == "demo object!");
  18. }
  19. TEST_CASE("JSON字段数据不顺序对应", "[json]")
  20. {
  21. struct DemoObjct
  22. {
  23. bool boolValue;
  24. int intValue;
  25. std::string strValue;
  26. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  27. };
  28. DemoObjct demoObj;
  29. REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"strValue\":\"demo object!\", \"intValue\":1234}")));
  30. REQUIRE(demoObj.boolValue == true);
  31. REQUIRE(demoObj.intValue == 1234);
  32. REQUIRE(demoObj.strValue == "demo object!");
  33. }
  34. TEST_CASE("从JSON字符串中获取结构体内容,声明中添加空格等占位符", "[json]")
  35. {
  36. struct DemoObjct
  37. {
  38. bool boolValue;
  39. int intValue;
  40. std::string strValue;
  41. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  42. };
  43. DemoObjct demoObj;
  44. REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\"}")));
  45. REQUIRE(demoObj.boolValue == true);
  46. REQUIRE(demoObj.intValue == 1234);
  47. REQUIRE(demoObj.strValue == "demo object!");
  48. }
  49. TEST_CASE("从JSON字符串中获取结构体内容,支持重命名", "[json]")
  50. {
  51. struct DemoObjct
  52. {
  53. bool boolValue;
  54. int intValue;
  55. std::string strValue;
  56. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  57. JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("bValue", "iValue", "sValue")
  58. };
  59. DemoObjct demoObj;
  60. REQUIRE(Json2Object(demoObj, std::string("{\"bValue\":true, \"iValue\":1234, \"sValue\":\"demo object!\"}")));
  61. REQUIRE(demoObj.boolValue == true);
  62. REQUIRE(demoObj.intValue == 1234);
  63. REQUIRE(demoObj.strValue == "demo object!");
  64. }
  65. TEST_CASE("单结构体转换为字符串,有做字段转换声明", "[json]")
  66. {
  67. struct DemoObjct
  68. {
  69. bool boolValue;
  70. int intValue;
  71. std::string strValue;
  72. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  73. JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("bValue", "iValue", "sValue")
  74. };
  75. DemoObjct demoObj;
  76. REQUIRE(Json2Object(demoObj, std::string("{\"bValue\":true, \"iValue\":1234, \"sValue\":\"demo object!\"}")));
  77. REQUIRE(demoObj.boolValue == true);
  78. REQUIRE(demoObj.intValue == 1234);
  79. REQUIRE(demoObj.strValue == "demo object!");
  80. std::string rawString;
  81. REQUIRE(Object2Json(rawString, demoObj));
  82. std::cout << "returned json format: " << rawString << std::endl;
  83. }
  84. TEST_CASE("单结构体转换为字符串", "[json]")
  85. {
  86. struct DemoObjct
  87. {
  88. bool boolValue;
  89. int intValue;
  90. std::string strValue;
  91. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  92. };
  93. DemoObjct demoObj;
  94. REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\"}")));
  95. REQUIRE(demoObj.boolValue == true);
  96. REQUIRE(demoObj.intValue == 1234);
  97. REQUIRE(demoObj.strValue == "demo object!");
  98. std::string rawString;
  99. REQUIRE(Object2Json(rawString, demoObj));
  100. std::cout << "returned json format: " << rawString << std::endl;
  101. }
  102. TEST_CASE("从JSON中解析中嵌套结构体", "[json]")
  103. {
  104. struct DemoParentObjct
  105. {
  106. bool boolValue;
  107. int intValue;
  108. std::string strValue;
  109. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  110. };
  111. struct DemoObjct
  112. {
  113. bool boolValue;
  114. int intValue;
  115. std::string strValue;
  116. DemoParentObjct parent;
  117. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, parent)
  118. };
  119. DemoObjct demoObj;
  120. REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\", \"parent\" : {\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\"}}")));
  121. REQUIRE(demoObj.boolValue == true);
  122. REQUIRE(demoObj.intValue == 1234);
  123. REQUIRE(demoObj.strValue == "demo object!");
  124. REQUIRE(demoObj.parent.boolValue == true);
  125. REQUIRE(demoObj.parent.intValue == 1234);
  126. REQUIRE(demoObj.parent.strValue == "demo object!");
  127. }
  128. TEST_CASE("嵌套结构体转换转换为JSON字符串", "[json]")
  129. {
  130. struct DemoParentObjct
  131. {
  132. bool boolValue;
  133. int intValue;
  134. std::string strValue;
  135. double dValue;
  136. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, dValue)
  137. };
  138. struct DemoObjct
  139. {
  140. bool boolValue;
  141. int intValue;
  142. std::string strValue;
  143. DemoParentObjct parent;
  144. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, parent)
  145. };
  146. DemoObjct demoObj;
  147. REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\", \"parent\" : {\"dValue\":1.45, \"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\"}}")));
  148. REQUIRE(demoObj.boolValue == true);
  149. REQUIRE(demoObj.intValue == 1234);
  150. REQUIRE(demoObj.strValue == "demo object!");
  151. REQUIRE(demoObj.parent.boolValue == true);
  152. REQUIRE(demoObj.parent.intValue == 1234);
  153. REQUIRE(demoObj.parent.strValue == "demo object!");
  154. std::string rawString;
  155. REQUIRE(Object2Json(rawString, demoObj));
  156. std::cout << "returned json format: " << rawString << std::endl;
  157. DemoObjct demoObj2;
  158. REQUIRE(Json2Object(demoObj2, std::string(rawString)));
  159. REQUIRE(demoObj2.boolValue == true);
  160. REQUIRE(demoObj2.intValue == 1234);
  161. REQUIRE(demoObj2.strValue == "demo object!");
  162. REQUIRE(demoObj2.parent.boolValue == true);
  163. REQUIRE(demoObj2.parent.intValue == 1234);
  164. REQUIRE(demoObj2.parent.strValue == "demo object!");
  165. }
  166. TEST_CASE("从Json串中解析结构体数组", "[json]")
  167. {
  168. struct DemoChildrenObject
  169. {
  170. bool boolValue;
  171. int intValue;
  172. std::string strValue;
  173. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  174. };
  175. struct DemoObjct
  176. {
  177. bool boolValue;
  178. int intValue;
  179. std::string strValue;
  180. std::vector< DemoChildrenObject> children;
  181. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, children)
  182. };
  183. DemoObjct demoObj;
  184. REQUIRE(Json2Object(demoObj, std::string("{\"boolValue\":true, \"intValue\":1234, \"strValue\":\"demo object!\", \"children\" : [ {\"boolValue\":true, \"intValue\":3333, \"strValue\":\"demo child1 object!\"},{\"boolValue\":false, \"intValue\":44444, \"strValue\":\"demo child2 object!\"} ] }")));
  185. REQUIRE(demoObj.boolValue == true);
  186. REQUIRE(demoObj.intValue == 1234);
  187. REQUIRE(demoObj.strValue == "demo object!");
  188. REQUIRE(demoObj.children.size() == 2);
  189. REQUIRE(demoObj.children[0].boolValue == true);
  190. REQUIRE(demoObj.children[0].intValue == 3333);
  191. REQUIRE(demoObj.children[0].strValue == "demo child1 object!");
  192. REQUIRE(demoObj.children[1].boolValue == false);
  193. REQUIRE(demoObj.children[1].intValue == 44444);
  194. REQUIRE(demoObj.children[1].strValue == "demo child2 object!");
  195. }
  196. TEST_CASE("解析结构体数组到JSON串", "[json]")
  197. {
  198. struct DemoChildrenObject
  199. {
  200. bool boolValue;
  201. int intValue;
  202. std::string strValue;
  203. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue)
  204. };
  205. struct DemoObjct
  206. {
  207. bool boolValue;
  208. int intValue;
  209. std::string strValue;
  210. std::vector< DemoChildrenObject> children;
  211. JSONCONVERT2OBJECT_MEMEBER_REGISTER(boolValue, intValue, strValue, children)
  212. };
  213. DemoObjct demoObj;
  214. demoObj.boolValue = true;
  215. demoObj.intValue = 321;
  216. demoObj.strValue = "hello worLd";
  217. DemoChildrenObject child1;
  218. child1.boolValue = true;
  219. child1.intValue = 1000;
  220. child1.strValue = "hello worLd child1";
  221. DemoChildrenObject child2;
  222. child2.boolValue = true;
  223. child2.intValue = 30005;
  224. child2.strValue = "hello worLd child2";
  225. demoObj.children.push_back(child1);
  226. demoObj.children.push_back(child2);
  227. std::string jsonStr;
  228. REQUIRE(Object2Json(jsonStr, demoObj));
  229. std::cout << "returned json format: " << jsonStr << std::endl;
  230. DemoObjct demoObj2;
  231. REQUIRE(Json2Object(demoObj2, jsonStr));
  232. REQUIRE(demoObj2.boolValue == true);
  233. REQUIRE(demoObj2.intValue == 321);
  234. REQUIRE(demoObj2.strValue == "hello worLd");
  235. REQUIRE(demoObj2.children.size() == 2);
  236. REQUIRE(demoObj.children[0].boolValue == true);
  237. REQUIRE(demoObj.children[0].intValue == 1000);
  238. REQUIRE(demoObj.children[0].strValue == "hello worLd child1");
  239. REQUIRE(demoObj.children[1].boolValue == true);
  240. REQUIRE(demoObj.children[1].intValue == 30005);
  241. REQUIRE(demoObj.children[1].strValue == "hello worLd child2");
  242. }