pika_cjson_cJSON.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "pika_cjson_cJSON.h"
  2. #include "cJSON.h"
  3. char* pika_cjson_cJSON_print(PikaObj* self) {
  4. cJSON* item = obj_getPtr(self, "item");
  5. char* res = cJSON_Print(item);
  6. obj_setStr(self, "_buf", res);
  7. cJSON_free(res);
  8. return obj_getStr(self, "_buf");
  9. }
  10. void pika_cjson_cJSON___del__(PikaObj* self) {
  11. cJSON* item = obj_getPtr(self, "item");
  12. if (obj_getInt(self, "needfree") == 1) {
  13. cJSON_Delete(item);
  14. }
  15. }
  16. PikaObj* pika_cjson_cJSON_getObjectItem(PikaObj* self, char* string) {
  17. cJSON* item = obj_getPtr(self, "item");
  18. cJSON* subItem = cJSON_GetObjectItem(item, string);
  19. if (NULL == subItem) {
  20. return NULL;
  21. }
  22. /* create subCJSON */
  23. PikaObj* subCJSON = newNormalObj(New_pika_cjson_cJSON);
  24. /* init the subCJSON */
  25. obj_setPtr(subCJSON, "item", subItem);
  26. obj_setInt(subCJSON, "needfree", 0);
  27. return subCJSON;
  28. }
  29. void pika_cjson_cJSON___init__(PikaObj* self) {
  30. /* const value */
  31. obj_setInt(self, "cJSON_Invalid", cJSON_Invalid);
  32. obj_setInt(self, "cJSON_False", cJSON_False);
  33. obj_setInt(self, "cJSON_True", cJSON_True);
  34. obj_setInt(self, "cJSON_NULL", cJSON_NULL);
  35. obj_setInt(self, "cJSON_Number", cJSON_Number);
  36. obj_setInt(self, "cJSON_String", cJSON_String);
  37. obj_setInt(self, "cJSON_Array", cJSON_Array);
  38. obj_setInt(self, "cJSON_Object", cJSON_Object);
  39. obj_setInt(self, "cJSON_Raw", cJSON_Raw);
  40. }
  41. PikaObj* pika_cjson_cJSON_getChild(PikaObj* self) {
  42. cJSON* item = obj_getPtr(self, "item");
  43. cJSON* resItem = item->child;
  44. if (NULL == resItem) {
  45. return NULL;
  46. }
  47. /* create subCJSON */
  48. PikaObj* resCJSON = newNormalObj(New_pika_cjson_cJSON);
  49. /* init the subCJSON */
  50. obj_setPtr(resCJSON, "item", resItem);
  51. obj_setInt(resCJSON, "needfree", 0);
  52. return resCJSON;
  53. }
  54. PikaObj* pika_cjson_cJSON_getNext(PikaObj* self) {
  55. cJSON* item = obj_getPtr(self, "item");
  56. cJSON* resItem = item->next;
  57. if (NULL == resItem) {
  58. return NULL;
  59. }
  60. /* create subCJSON */
  61. PikaObj* resCJSON = newNormalObj(New_pika_cjson_cJSON);
  62. /* init the subCJSON */
  63. obj_setPtr(resCJSON, "item", resItem);
  64. obj_setInt(resCJSON, "needfree", 0);
  65. return resCJSON;
  66. }
  67. PikaObj* pika_cjson_cJSON_getPrev(PikaObj* self) {
  68. cJSON* item = obj_getPtr(self, "item");
  69. cJSON* resItem = item->prev;
  70. if (NULL == resItem) {
  71. return NULL;
  72. }
  73. /* create subCJSON */
  74. PikaObj* resCJSON = newNormalObj(New_pika_cjson_cJSON);
  75. /* init the subCJSON */
  76. obj_setPtr(resCJSON, "item", resItem);
  77. obj_setInt(resCJSON, "needfree", 0);
  78. return resCJSON;
  79. }
  80. char* pika_cjson_cJSON_getString(PikaObj* self) {
  81. cJSON* item = obj_getPtr(self, "item");
  82. return item->string;
  83. }
  84. int pika_cjson_cJSON_getType(PikaObj* self) {
  85. cJSON* item = obj_getPtr(self, "item");
  86. return item->type;
  87. }
  88. pika_float pika_cjson_cJSON_getValueDouble(PikaObj* self) {
  89. cJSON* item = obj_getPtr(self, "item");
  90. return item->valuedouble;
  91. }
  92. int pika_cjson_cJSON_getValueInt(PikaObj* self) {
  93. cJSON* item = obj_getPtr(self, "item");
  94. return item->valueint;
  95. }
  96. char* pika_cjson_cJSON_getValueString(PikaObj* self) {
  97. cJSON* item = obj_getPtr(self, "item");
  98. return item->valuestring;
  99. }
  100. Arg* pika_cjson_cJSON_getValue(PikaObj* self) {
  101. cJSON* item = obj_getPtr(self, "item");
  102. int type = item->type;
  103. if (type == cJSON_Invalid) {
  104. return arg_newNone();
  105. }
  106. if (type == cJSON_False) {
  107. return arg_newBool(0);
  108. }
  109. if (type == cJSON_True) {
  110. return arg_newBool(1);
  111. }
  112. if (type == cJSON_NULL) {
  113. return arg_newNone();
  114. }
  115. if (type == cJSON_Number) {
  116. return arg_newFloat(item->valuedouble);
  117. }
  118. if (type == cJSON_String) {
  119. return arg_newStr(item->valuestring);
  120. }
  121. return arg_newNone();
  122. }
  123. PikaObj* pika_cjson_cJSON_getArrayItem(PikaObj* self, int index) {
  124. cJSON* item = obj_getPtr(self, "item");
  125. cJSON* subItem = cJSON_GetArrayItem(item, index);
  126. if (NULL == subItem) {
  127. return NULL;
  128. }
  129. /* create subCJSON */
  130. PikaObj* subCJSON = newNormalObj(New_pika_cjson_cJSON);
  131. /* init the subCJSON */
  132. obj_setPtr(subCJSON, "item", subItem);
  133. obj_setInt(subCJSON, "needfree", 0);
  134. return subCJSON;
  135. }
  136. int pika_cjson_cJSON_getArraySize(PikaObj* self) {
  137. cJSON* item = obj_getPtr(self, "item");
  138. return cJSON_GetArraySize(item);
  139. }
  140. int pika_cjson_cJSON_isArray(PikaObj* self) {
  141. cJSON* item = obj_getPtr(self, "item");
  142. return cJSON_IsArray(item);
  143. }
  144. int pika_cjson_cJSON_isBool(PikaObj* self) {
  145. cJSON* item = obj_getPtr(self, "item");
  146. return cJSON_IsBool(item);
  147. }
  148. int pika_cjson_cJSON_isFalse(PikaObj* self) {
  149. cJSON* item = obj_getPtr(self, "item");
  150. return cJSON_IsFalse(item);
  151. }
  152. int pika_cjson_cJSON_isInvalid(PikaObj* self) {
  153. cJSON* item = obj_getPtr(self, "item");
  154. return cJSON_IsInvalid(item);
  155. }
  156. int pika_cjson_cJSON_isNull(PikaObj* self) {
  157. cJSON* item = obj_getPtr(self, "item");
  158. return cJSON_IsNull(item);
  159. }
  160. int pika_cjson_cJSON_isNumber(PikaObj* self) {
  161. cJSON* item = obj_getPtr(self, "item");
  162. return cJSON_IsNumber(item);
  163. }
  164. int pika_cjson_cJSON_isObject(PikaObj* self) {
  165. cJSON* item = obj_getPtr(self, "item");
  166. return cJSON_IsObject(item);
  167. }
  168. int pika_cjson_cJSON_isRaw(PikaObj* self) {
  169. cJSON* item = obj_getPtr(self, "item");
  170. return cJSON_IsRaw(item);
  171. }
  172. int pika_cjson_cJSON_isString(PikaObj* self) {
  173. cJSON* item = obj_getPtr(self, "item");
  174. return cJSON_IsString(item);
  175. }
  176. int pika_cjson_cJSON_isTrue(PikaObj* self) {
  177. cJSON* item = obj_getPtr(self, "item");
  178. return cJSON_IsTrue(item);
  179. }
  180. void pika_cjson_cJSON_addItemToArray(PikaObj* self, PikaObj* item) {
  181. cJSON* self_item = obj_getPtr(self, "item");
  182. cJSON* item_item = obj_getPtr(item, "item");
  183. cJSON_AddItemToArray(self_item, item_item);
  184. obj_setInt(item, "needfree", 0);
  185. }
  186. void pika_cjson_cJSON_addItemToObject(PikaObj* self,
  187. char* string,
  188. PikaObj* item) {
  189. cJSON* self_item = obj_getPtr(self, "item");
  190. cJSON* item_item = obj_getPtr(item, "item");
  191. cJSON_AddItemToObject(self_item, string, item_item);
  192. obj_setInt(item, "needfree", 0);
  193. }