RyanJsonBaseTestDetachJson.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "RyanJsonBaseTest.h"
  2. /* --------------------------------------------------------------------- */
  3. RyanJsonBool_e RyanJsonBaseTestDetachJson(void)
  4. {
  5. char jsonstr[] =
  6. "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,\"item\":"
  7. "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},"
  8. "\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89],"
  9. "\"arrayString\":[\"hello\",\"hello\",\"hello\",\"hello\",\"hello\"],"
  10. "\"array\":[16,16.89,\"hello\",true,false,null],"
  11. "\"arrayItem\":[{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},{"
  12. "\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},{\"inter\":16,"
  13. "\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},{\"inter\":16,\"double\":16.89,"
  14. "\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},"
  15. "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null}],"
  16. "\"string2222\":\"hello\"}";
  17. RyanJson_t json = RyanJsonParse(jsonstr);
  18. RyanJsonCheckReturnFalse(NULL != json);
  19. /**
  20. * @brief 对象子项分离测试(头、中、尾)
  21. */
  22. {
  23. RyanJson_t json2 = RyanJsonParse(jsonstr);
  24. // 头部 (第一个 key: inter)
  25. RyanJsonDelete(RyanJsonDetachByIndex(json, 0));
  26. if (RyanJsonGetObjectToKey(json, "inter"))
  27. {
  28. RyanJsonCheckCode(NULL, { goto err; });
  29. }
  30. // 中间 (double)
  31. RyanJsonDelete(RyanJsonDetachByKey(json, "double"));
  32. if (RyanJsonGetObjectToKey(json, "double"))
  33. {
  34. RyanJsonCheckCode(NULL, { goto err; });
  35. }
  36. // 尾部 (最后一个 key: string2222)
  37. uint32_t lastIndex = RyanJsonGetSize(json) - 1;
  38. RyanJsonDelete(RyanJsonDetachByIndex(json, lastIndex));
  39. if (RyanJsonGetObjectToKey(json, "string2222"))
  40. {
  41. RyanJsonCheckCode(NULL, { goto err; });
  42. }
  43. RyanJsonDelete(json2);
  44. }
  45. /**
  46. * @brief 数组元素分离测试 (arrayInt 头、中、尾)
  47. */
  48. {
  49. RyanJson_t arr = RyanJsonGetObjectByKey(json, "arrayInt");
  50. RyanJson_t json2 = RyanJsonParse(jsonstr);
  51. uint32_t jsonSize = RyanJsonGetSize(arr);
  52. // 头部
  53. RyanJsonDelete(RyanJsonDetachByIndex(arr, 0));
  54. if (jsonSize == RyanJsonGetSize(arr))
  55. {
  56. RyanJsonCheckCode(NULL, { goto err; });
  57. }
  58. // 中间
  59. jsonSize = RyanJsonGetSize(arr);
  60. RyanJsonDelete(RyanJsonDetachByIndex(arr, 2));
  61. if (jsonSize == RyanJsonGetSize(arr))
  62. {
  63. RyanJsonCheckCode(NULL, { goto err; });
  64. }
  65. // 尾部
  66. uint32_t lastIndex = RyanJsonGetSize(arr) - 1;
  67. RyanJsonDelete(RyanJsonDetachByIndex(arr, lastIndex));
  68. if (NULL != RyanJsonGetObjectToIndex(arr, lastIndex))
  69. {
  70. RyanJsonCheckCode(NULL, { goto err; });
  71. }
  72. RyanJsonDelete(json2);
  73. }
  74. /**
  75. * @brief 数组元素分离测试 (arrayDouble 头、中、尾)
  76. */
  77. {
  78. RyanJson_t arr = RyanJsonGetObjectByKey(json, "arrayDouble");
  79. RyanJson_t json2 = RyanJsonParse(jsonstr);
  80. RyanJsonDelete(RyanJsonDetachByIndex(arr, 0));
  81. if (RyanJsonGetObjectToIndex(arr, 0) == NULL)
  82. {
  83. RyanJsonCheckCode(NULL, { goto err; });
  84. }
  85. RyanJsonDelete(RyanJsonDetachByIndex(arr, 2));
  86. if (RyanJsonGetObjectToIndex(arr, 2) == NULL)
  87. {
  88. RyanJsonCheckCode(NULL, { goto err; });
  89. }
  90. uint32_t lastIndex = RyanJsonGetSize(arr) - 1;
  91. RyanJsonDelete(RyanJsonDetachByIndex(arr, lastIndex));
  92. if (RyanJsonGetObjectToIndex(arr, lastIndex) != NULL)
  93. {
  94. RyanJsonCheckCode(NULL, { goto err; });
  95. }
  96. RyanJsonDelete(json2);
  97. }
  98. /**
  99. * @brief 数组元素分离测试 (arrayString 头、中、尾)
  100. */
  101. {
  102. RyanJson_t arr = RyanJsonGetObjectByKey(json, "arrayString");
  103. RyanJson_t json2 = RyanJsonParse(jsonstr);
  104. RyanJsonDelete(RyanJsonDetachByIndex(arr, 0));
  105. if (RyanJsonGetObjectToIndex(arr, 0) == NULL)
  106. {
  107. RyanJsonCheckCode(NULL, { goto err; });
  108. }
  109. RyanJsonDelete(RyanJsonDetachByIndex(arr, 2));
  110. if (RyanJsonGetObjectToIndex(arr, 2) == NULL)
  111. {
  112. RyanJsonCheckCode(NULL, { goto err; });
  113. }
  114. uint32_t lastIndex = RyanJsonGetSize(arr) - 1;
  115. RyanJsonDelete(RyanJsonDetachByIndex(arr, lastIndex));
  116. if (RyanJsonGetObjectToIndex(arr, lastIndex) != NULL)
  117. {
  118. RyanJsonCheckCode(NULL, { goto err; });
  119. }
  120. RyanJsonDelete(json2);
  121. }
  122. /**
  123. * @brief 嵌套对象分离测试 (item)
  124. */
  125. {
  126. RyanJson_t json2 = RyanJsonParse(jsonstr);
  127. RyanJsonDelete(RyanJsonDetachByKey(json, "item"));
  128. if (RyanJsonGetObjectToKey(json, "item"))
  129. {
  130. RyanJsonCheckCode(NULL, { goto err; });
  131. }
  132. RyanJsonDelete(json2);
  133. }
  134. /**
  135. * @brief 数组对象元素分离测试 (arrayItem 头、中、尾)
  136. */
  137. {
  138. RyanJson_t arr = RyanJsonGetObjectByKey(json, "arrayItem");
  139. RyanJson_t json2 = RyanJsonParse(jsonstr);
  140. RyanJsonDelete(RyanJsonDetachByIndex(arr, 0));
  141. if (RyanJsonGetObjectToIndex(arr, 0) == NULL)
  142. {
  143. RyanJsonCheckCode(NULL, { goto err; });
  144. }
  145. RyanJsonDelete(RyanJsonDetachByIndex(arr, 1));
  146. if (RyanJsonGetObjectToIndex(arr, 1) == NULL)
  147. {
  148. RyanJsonCheckCode(NULL, { goto err; });
  149. }
  150. uint32_t lastIndex = RyanJsonGetSize(arr) - 1;
  151. RyanJsonDelete(RyanJsonDetachByIndex(arr, lastIndex));
  152. if (RyanJsonGetObjectToIndex(arr, lastIndex) != NULL)
  153. {
  154. RyanJsonCheckCode(NULL, { goto err; });
  155. }
  156. RyanJsonDelete(json2);
  157. }
  158. /**
  159. * @brief 特殊类型分离测试(null / bool)
  160. */
  161. {
  162. RyanJson_t json2 = RyanJsonParse(jsonstr);
  163. RyanJsonDelete(RyanJsonDetachByKey(json, "null"));
  164. if (RyanJsonGetObjectToKey(json, "null"))
  165. {
  166. RyanJsonCheckCode(NULL, { goto err; });
  167. }
  168. RyanJsonDelete(RyanJsonDetachByKey(json, "boolTrue"));
  169. if (RyanJsonGetObjectToKey(json, "boolTrue"))
  170. {
  171. RyanJsonCheckCode(NULL, { goto err; });
  172. }
  173. RyanJsonDelete(json2);
  174. }
  175. char *str = RyanJsonPrint(json, 1024, RyanJsonTrue, NULL);
  176. RyanJsonFree(str);
  177. RyanJsonDelete(json);
  178. return RyanJsonTrue;
  179. err:
  180. RyanJsonDelete(json);
  181. return RyanJsonFalse;
  182. }