RyanJsonBaseTestDeleteJson.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "RyanJsonBaseTest.h"
  2. /* --------------------------------------------------------------------- */
  3. RyanJsonBool_e RyanJsonBaseTestDeleteJson(void)
  4. {
  5. // 保持原始 jsonStr,不做修改
  6. char jsonstr[] =
  7. "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null,\"item\":"
  8. "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},"
  9. "\"arrayInt\":[16,16,16,16,16],\"arrayDouble\":[16.89,16.89,16.89,16.89,16.89],"
  10. "\"arrayString\":[\"hello\",\"hello\",\"hello\",\"hello\",\"hello\"],"
  11. "\"array\":[16,16.89,\"hello\",true,false,null],"
  12. "\"arrayItem\":[{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null},"
  13. "{\"inter\":16,\"double\":16.89,\"string\":\"hello\",\"boolTrue\":true,\"boolFalse\":false,\"null\":null}],"
  14. "\"string2222\":\"hello\"}";
  15. RyanJson_t json = RyanJsonParse(jsonstr);
  16. RyanJsonCheckReturnFalse(NULL != json);
  17. /**
  18. * @brief 场景 1:删除对象中的节点(头、中、尾)
  19. */
  20. {
  21. // 删除中间节点 (double)
  22. RyanJsonDeleteByKey(json, "double");
  23. RyanJsonCheckCode(NULL == RyanJsonGetObjectToKey(json, "double"), { goto err; });
  24. // 删除头部节点 (inter)
  25. RyanJsonDeleteByIndex(json, 0);
  26. RyanJsonCheckCode(NULL == RyanJsonGetObjectToKey(json, "inter"), { goto err; });
  27. // 删除尾部节点 (string2222)
  28. uint32_t lastIndex = RyanJsonGetSize(json) - 1;
  29. RyanJsonDeleteByIndex(json, lastIndex);
  30. RyanJsonCheckCode(NULL == RyanJsonGetObjectToKey(json, "string2222"), { goto err; });
  31. }
  32. /**
  33. * @brief 场景 2:删除数组中的元素(arrayInt)
  34. */
  35. {
  36. RyanJson_t array = RyanJsonGetObjectToKey(json, "arrayInt");
  37. // 删除数组首位
  38. RyanJsonDeleteByIndex(array, 0);
  39. RyanJsonCheckCode(RyanJsonGetSize(array) == 4, { goto err; });
  40. // 删除数组中间元素
  41. RyanJsonDeleteByIndex(array, 1);
  42. RyanJsonCheckCode(RyanJsonGetSize(array) == 3, { goto err; });
  43. // 删除数组尾部元素
  44. uint32_t lastIndex = RyanJsonGetSize(array) - 1;
  45. RyanJsonDeleteByIndex(array, lastIndex);
  46. RyanJsonCheckCode(RyanJsonGetSize(array) == 2, { goto err; });
  47. }
  48. /**
  49. * @brief 场景 3:深层嵌套删除(item)
  50. */
  51. {
  52. RyanJsonDeleteByKey(json, "item");
  53. RyanJsonCheckCode(NULL == RyanJsonGetObjectToKey(json, "item"), { goto err; });
  54. }
  55. /**
  56. * @brief 场景 4:数组对象元素删除(arrayItem)
  57. */
  58. {
  59. RyanJson_t arrObj = RyanJsonGetObjectToKey(json, "arrayItem");
  60. // 删除第一个对象
  61. RyanJsonDeleteByIndex(arrObj, 0);
  62. RyanJsonCheckCode(RyanJsonGetSize(arrObj) == 1, { goto err; });
  63. // 删除最后一个对象
  64. RyanJsonDeleteByIndex(arrObj, 0);
  65. RyanJsonCheckCode(RyanJsonGetSize(arrObj) == 0, { goto err; });
  66. }
  67. /**
  68. * @brief 场景 5:特殊类型删除(null / bool)
  69. */
  70. {
  71. RyanJsonDeleteByKey(json, "null");
  72. RyanJsonCheckCode(NULL == RyanJsonGetObjectToKey(json, "null"), { goto err; });
  73. RyanJsonDeleteByKey(json, "boolTrue");
  74. RyanJsonCheckCode(NULL == RyanJsonGetObjectToKey(json, "boolTrue"), { goto err; });
  75. RyanJsonDeleteByKey(json, "boolFalse");
  76. RyanJsonCheckCode(NULL == RyanJsonGetObjectToKey(json, "boolFalse"), { goto err; });
  77. }
  78. /**
  79. * @brief 场景 6:异常路径覆盖(健壮性)
  80. */
  81. {
  82. RyanJsonDeleteByKey(json, "non_exist"); // 删除不存在的 Key
  83. RyanJsonDeleteByIndex(NULL, 0); // 在 NULL 上操作
  84. }
  85. char *str = RyanJsonPrint(json, 1024, RyanJsonTrue, NULL);
  86. RyanJsonFree(str);
  87. RyanJsonDelete(json);
  88. return RyanJsonTrue;
  89. err:
  90. RyanJsonDelete(json);
  91. return RyanJsonFalse;
  92. }