_pika_cJSON.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #ifndef pika_cJSON__h
  20. #define pika_cJSON__h
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #if !defined(__WINDOWS__) && \
  25. (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
  26. #define __WINDOWS__
  27. #endif
  28. #ifdef __WINDOWS__
  29. /* When compiling for windows, we specify a specific calling convention to
  30. avoid issues where we are being called from a project with a different
  31. default calling convention. For windows you have 3 define options:
  32. pika_cJSON_HIDE_SYMBOLS - Define this in the case where you don't want to
  33. ever dllexport symbols pika_cJSON_EXPORT_SYMBOLS - Define this on library
  34. build when you want to dllexport symbols (default) pika_cJSON_IMPORT_SYMBOLS
  35. - Define this if you want to dllimport symbol
  36. For *nix builds that support visibility attribute, you can define similar
  37. behavior by
  38. setting default visibility to hidden by adding
  39. -fvisibility=hidden (for gcc)
  40. or
  41. -xldscope=hidden (for sun cc)
  42. to CFLAGS
  43. then using the pika_cJSON_API_VISIBILITY flag to "export" the same symbols
  44. the way pika_cJSON_EXPORT_SYMBOLS does
  45. */
  46. #define pika_cJSON_CDECL __cdecl
  47. #define pika_cJSON_STDCALL __stdcall
  48. /* export symbols by default, this is necessary for copy pasting the C and
  49. * header file */
  50. #if !defined(pika_cJSON_HIDE_SYMBOLS) && \
  51. !defined(pika_cJSON_IMPORT_SYMBOLS) && !defined(pika_cJSON_EXPORT_SYMBOLS)
  52. #define pika_cJSON_EXPORT_SYMBOLS
  53. #endif
  54. #if defined(pika_cJSON_HIDE_SYMBOLS)
  55. #define pika_cJSON_PUBLIC(type) type pika_cJSON_STDCALL
  56. #elif defined(pika_cJSON_EXPORT_SYMBOLS)
  57. #define pika_cJSON_PUBLIC(type) __declspec(dllexport) type pika_cJSON_STDCALL
  58. #elif defined(pika_cJSON_IMPORT_SYMBOLS)
  59. #define pika_cJSON_PUBLIC(type) __declspec(dllimport) type pika_cJSON_STDCALL
  60. #endif
  61. #else /* !__WINDOWS__ */
  62. #define pika_cJSON_CDECL
  63. #define pika_cJSON_STDCALL
  64. #if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) && \
  65. defined(pika_cJSON_API_VISIBILITY)
  66. #define pika_cJSON_PUBLIC(type) __attribute__((visibility("default"))) type
  67. #else
  68. #define pika_cJSON_PUBLIC(type) type
  69. #endif
  70. #endif
  71. /* project version */
  72. #define pika_cJSON_VERSION_MAJOR 1
  73. #define pika_cJSON_VERSION_MINOR 7
  74. #define pika_cJSON_VERSION_PATCH 15
  75. #include <stddef.h>
  76. /* cJSON Types: */
  77. #define pika_cJSON_Invalid (0)
  78. #define pika_cJSON_False (1 << 0)
  79. #define pika_cJSON_True (1 << 1)
  80. #define pika_cJSON_NULL (1 << 2)
  81. #define pika_cJSON_Number (1 << 3)
  82. #define pika_cJSON_String (1 << 4)
  83. #define pika_cJSON_Array (1 << 5)
  84. #define pika_cJSON_Object (1 << 6)
  85. #define pika_cJSON_Raw (1 << 7) /* raw json */
  86. #define pika_cJSON_IsReference 256
  87. #define pika_cJSON_StringIsConst 512
  88. /* The cJSON structure: */
  89. typedef struct cJSON {
  90. /* next/prev allow you to walk array/object chains. Alternatively, use
  91. * GetArraySize/GetArrayItem/GetObjectItem */
  92. struct cJSON* next;
  93. struct cJSON* prev;
  94. /* An array or object item will have a child pointer pointing to a chain
  95. * of the items in the array/object. */
  96. struct cJSON* child;
  97. /* The type of the item, as above. */
  98. int type;
  99. /* The item's string, if type==pika_cJSON_String and type ==
  100. * pika_cJSON_Raw
  101. */
  102. char* valuestring;
  103. /* writing to valueint is DEPRECATED, use pika_cJSON_SetNumberValue
  104. * instead
  105. */
  106. int valueint;
  107. /* The item's number, if type==pika_cJSON_Number */
  108. double valuedouble;
  109. /* The item's name string, if this item is the child of, or is in the
  110. * list of subitems of an object. */
  111. char* string;
  112. } cJSON;
  113. typedef struct pika_cJSON_Hooks {
  114. /* malloc/free are CDECL on Windows regardless of the default calling
  115. * convention of the compiler, so ensure the hooks allow passing those
  116. * functions directly. */
  117. void*(pika_cJSON_CDECL* malloc_fn)(size_t sz);
  118. void(pika_cJSON_CDECL* free_fn)(void* ptr);
  119. } pika_cJSON_Hooks;
  120. typedef int pika_cJSON_bool;
  121. /* Limits how deeply nested arrays/objects can be before cJSON rejects to parse
  122. * them. This is to prevent stack overflows. */
  123. #ifndef pika_cJSON_NESTING_LIMIT
  124. #define pika_cJSON_NESTING_LIMIT 1000
  125. #endif
  126. /* returns the version of cJSON as a string */
  127. pika_cJSON_PUBLIC(const char*) pika_cJSON_Version(void);
  128. /* Supply malloc, realloc and free functions to cJSON */
  129. pika_cJSON_PUBLIC(void) pika_cJSON_InitHooks(pika_cJSON_Hooks* hooks);
  130. /* Memory Management: the caller is always responsible to free the results
  131. * from all variants of pika_cJSON_Parse (with pika_cJSON_Delete) and
  132. * pika_cJSON_Print (with stdlib free, pika_cJSON_Hooks.free_fn, or
  133. * pika_cJSON_free as appropriate). The exception is
  134. * pika_cJSON_PrintPreallocated, where the caller has full responsibility of
  135. * the buffer. */
  136. /* Supply a block of JSON, and this returns a cJSON object you can
  137. * interrogate.
  138. */
  139. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_Parse(const char* value);
  140. pika_cJSON_PUBLIC(cJSON*)
  141. pika_cJSON_ParseWithLength(const char* value, size_t buffer_length);
  142. /* ParseWithOpts allows you to require (and check) that the JSON is null
  143. * terminated, and to retrieve the pointer to the final byte parsed. */
  144. /* If you supply a ptr in return_parse_end and parsing fails, then
  145. * return_parse_end will contain a pointer to the error so will match
  146. * pika_cJSON_GetErrorPtr(). */
  147. pika_cJSON_PUBLIC(cJSON*)
  148. pika_cJSON_ParseWithOpts(const char* value,
  149. const char** return_parse_end,
  150. pika_cJSON_bool require_null_terminated);
  151. pika_cJSON_PUBLIC(cJSON*)
  152. pika_cJSON_ParseWithLengthOpts(const char* value,
  153. size_t buffer_length,
  154. const char** return_parse_end,
  155. pika_cJSON_bool require_null_terminated);
  156. /* Render a cJSON entity to text for transfer/storage. */
  157. pika_cJSON_PUBLIC(char*) pika_cJSON_Print(const cJSON* item);
  158. /* Render a cJSON entity to text for transfer/storage without any
  159. * formatting. */
  160. pika_cJSON_PUBLIC(char*) pika_cJSON_PrintUnformatted(const cJSON* item);
  161. /* Render a cJSON entity to text using a buffered strategy. prebuffer is a
  162. * guess at the final size. guessing well reduces reallocation. fmt=0 gives
  163. * unformatted, =1 gives formatted */
  164. pika_cJSON_PUBLIC(char*) pika_cJSON_PrintBuffered(const cJSON* item,
  165. int prebuffer,
  166. pika_cJSON_bool fmt);
  167. /* Render a cJSON entity to text using a buffer already allocated in memory
  168. * with given length. Returns 1 on success and 0 on failure. */
  169. /* NOTE: cJSON is not always 100% accurate in estimating how much memory it
  170. * will use, so to be safe allocate 5 bytes more than you actually need */
  171. pika_cJSON_PUBLIC(pika_cJSON_bool)
  172. pika_cJSON_PrintPreallocated(cJSON* item,
  173. char* buffer,
  174. const int length,
  175. const pika_cJSON_bool format);
  176. /* Delete a cJSON entity and all subentities. */
  177. pika_cJSON_PUBLIC(void) pika_cJSON_Delete(cJSON* item);
  178. /* Returns the number of items in an array (or object). */
  179. pika_cJSON_PUBLIC(int) pika_cJSON_GetArraySize(const cJSON* array);
  180. /* Retrieve item number "index" from array "array". Returns NULL if
  181. * unsuccessful. */
  182. pika_cJSON_PUBLIC(cJSON*)
  183. pika_cJSON_GetArrayItem(const cJSON* array, int index);
  184. /* Get item "string" from object. Case insensitive. */
  185. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_GetObjectItem(const cJSON* const object,
  186. const char* const string);
  187. pika_cJSON_PUBLIC(cJSON*)
  188. pika_cJSON_GetObjectItemCaseSensitive(const cJSON* const object,
  189. const char* const string);
  190. pika_cJSON_PUBLIC(pika_cJSON_bool)
  191. pika_cJSON_HasObjectItem(const cJSON* object, const char* string);
  192. /* For analysing failed parses. This returns a pointer to the parse error.
  193. * You'll probably need to look a few chars back to make sense of it.
  194. * Defined when pika_cJSON_Parse() returns 0. 0 when pika_cJSON_Parse()
  195. * succeeds. */
  196. pika_cJSON_PUBLIC(const char*) pika_cJSON_GetErrorPtr(void);
  197. /* Check item type and return its value */
  198. pika_cJSON_PUBLIC(char*) pika_cJSON_GetStringValue(const cJSON* const item);
  199. pika_cJSON_PUBLIC(double) pika_cJSON_GetNumberValue(const cJSON* const item);
  200. /* These functions check the type of an item */
  201. pika_cJSON_PUBLIC(pika_cJSON_bool)
  202. pika_cJSON_IsInvalid(const cJSON* const item);
  203. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsFalse(const cJSON* const item);
  204. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsTrue(const cJSON* const item);
  205. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsBool(const cJSON* const item);
  206. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsNull(const cJSON* const item);
  207. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsNumber(const cJSON* const item);
  208. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsString(const cJSON* const item);
  209. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsArray(const cJSON* const item);
  210. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsObject(const cJSON* const item);
  211. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_IsRaw(const cJSON* const item);
  212. /* These calls create a cJSON item of the appropriate type. */
  213. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateNull(void);
  214. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateTrue(void);
  215. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateFalse(void);
  216. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateBool(pika_cJSON_bool boolean);
  217. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateNumber(double num);
  218. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateString(const char* string);
  219. /* raw json */
  220. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateRaw(const char* raw);
  221. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateArray(void);
  222. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateObject(void);
  223. /* Create a string where valuestring references a string so
  224. * it will not be freed by pika_cJSON_Delete */
  225. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateStringReference(const char* string);
  226. /* Create an object/array that only references it's elements so
  227. * they will not be freed by pika_cJSON_Delete */
  228. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateObjectReference(const cJSON* child);
  229. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_CreateArrayReference(const cJSON* child);
  230. /* These utilities create an Array of count items.
  231. * The parameter count cannot be greater than the number of elements in the
  232. * number array, otherwise array access will be out of bounds.*/
  233. pika_cJSON_PUBLIC(cJSON*)
  234. pika_cJSON_CreateIntArray(const int* numbers, int count);
  235. pika_cJSON_PUBLIC(cJSON*)
  236. pika_cJSON_CreateFloatArray(const double* numbers, int count);
  237. pika_cJSON_PUBLIC(cJSON*)
  238. pika_cJSON_CreateDoubleArray(const double* numbers, int count);
  239. pika_cJSON_PUBLIC(cJSON*)
  240. pika_cJSON_CreateStringArray(const char* const* strings, int count);
  241. /* Append item to the specified array/object. */
  242. pika_cJSON_PUBLIC(pika_cJSON_bool)
  243. pika_cJSON_AddItemToArray(cJSON* array, cJSON* item);
  244. pika_cJSON_PUBLIC(pika_cJSON_bool)
  245. pika_cJSON_AddItemToObject(cJSON* object, const char* string, cJSON* item);
  246. /* Use this when string is definitely const (i.e. a literal, or as good as),
  247. * and will definitely survive the cJSON object. WARNING: When this function
  248. * was used, make sure to always check that (item->type &
  249. * pika_cJSON_StringIsConst) is zero before writing to `item->string` */
  250. pika_cJSON_PUBLIC(pika_cJSON_bool)
  251. pika_cJSON_AddItemToObjectCS(cJSON* object,
  252. const char* string,
  253. cJSON* item);
  254. /* Append reference to item to the specified array/object. Use this when you
  255. * want to add an existing cJSON to a new cJSON, but don't want to corrupt
  256. * your existing cJSON. */
  257. pika_cJSON_PUBLIC(pika_cJSON_bool)
  258. pika_cJSON_AddItemReferenceToArray(cJSON* array, cJSON* item);
  259. pika_cJSON_PUBLIC(pika_cJSON_bool)
  260. pika_cJSON_AddItemReferenceToObject(cJSON* object,
  261. const char* string,
  262. cJSON* item);
  263. /* Remove/Detach items from Arrays/Objects. */
  264. pika_cJSON_PUBLIC(cJSON*)
  265. pika_cJSON_DetachItemViaPointer(cJSON* parent, cJSON* const item);
  266. pika_cJSON_PUBLIC(cJSON*)
  267. pika_cJSON_DetachItemFromArray(cJSON* array, int which);
  268. pika_cJSON_PUBLIC(void) pika_cJSON_DeleteItemFromArray(cJSON* array, int which);
  269. pika_cJSON_PUBLIC(cJSON*)
  270. pika_cJSON_DetachItemFromObject(cJSON* object, const char* string);
  271. pika_cJSON_PUBLIC(cJSON*)
  272. pika_cJSON_DetachItemFromObjectCaseSensitive(cJSON* object,
  273. const char* string);
  274. pika_cJSON_PUBLIC(void)
  275. pika_cJSON_DeleteItemFromObject(cJSON* object, const char* string);
  276. pika_cJSON_PUBLIC(void)
  277. pika_cJSON_DeleteItemFromObjectCaseSensitive(cJSON* object,
  278. const char* string);
  279. /* Update array items. */
  280. pika_cJSON_PUBLIC(pika_cJSON_bool) pika_cJSON_InsertItemInArray(
  281. cJSON* array,
  282. int which,
  283. cJSON* newitem); /* Shifts pre-existing items to the right. */
  284. pika_cJSON_PUBLIC(pika_cJSON_bool)
  285. pika_cJSON_ReplaceItemViaPointer(cJSON* const parent,
  286. cJSON* const item,
  287. cJSON* replacement);
  288. pika_cJSON_PUBLIC(pika_cJSON_bool)
  289. pika_cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem);
  290. pika_cJSON_PUBLIC(pika_cJSON_bool)
  291. pika_cJSON_ReplaceItemInObject(cJSON* object,
  292. const char* string,
  293. cJSON* newitem);
  294. pika_cJSON_PUBLIC(pika_cJSON_bool)
  295. pika_cJSON_ReplaceItemInObjectCaseSensitive(cJSON* object,
  296. const char* string,
  297. cJSON* newitem);
  298. /* Duplicate a cJSON item */
  299. pika_cJSON_PUBLIC(cJSON*)
  300. pika_cJSON_Duplicate(const cJSON* item, pika_cJSON_bool recurse);
  301. /* Duplicate will create a new, identical cJSON item to the one you pass, in
  302. * new memory that will need to be released. With recurse!=0, it will
  303. * duplicate any children connected to the item. The item->next and ->prev
  304. * pointers are always zero on return from Duplicate. */
  305. /* Recursively compare two cJSON items for equality. If either a or b is
  306. * NULL or invalid, they will be considered unequal. case_sensitive
  307. * determines if object keys are treated case sensitive (1) or case
  308. * insensitive (0) */
  309. pika_cJSON_PUBLIC(pika_cJSON_bool)
  310. pika_cJSON_Compare(const cJSON* const a,
  311. const cJSON* const b,
  312. const pika_cJSON_bool case_sensitive);
  313. /* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n')
  314. * from strings. The input pointer json cannot point to a read-only address
  315. * area, such as a string constant, but should point to a readable and
  316. * writable address area. */
  317. pika_cJSON_PUBLIC(void) pika_cJSON_Minify(char* json);
  318. /* Helper functions for creating and adding items to an object at the same
  319. * time. They return the added item or NULL on failure. */
  320. pika_cJSON_PUBLIC(cJSON*)
  321. pika_cJSON_AddNullToObject(cJSON* const object, const char* const name);
  322. pika_cJSON_PUBLIC(cJSON*)
  323. pika_cJSON_AddTrueToObject(cJSON* const object, const char* const name);
  324. pika_cJSON_PUBLIC(cJSON*)
  325. pika_cJSON_AddFalseToObject(cJSON* const object, const char* const name);
  326. pika_cJSON_PUBLIC(cJSON*)
  327. pika_cJSON_AddBoolToObject(cJSON* const object,
  328. const char* const name,
  329. const pika_cJSON_bool boolean);
  330. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_AddNumberToObject(cJSON* const object,
  331. const char* const name,
  332. const double number);
  333. pika_cJSON_PUBLIC(cJSON*)
  334. pika_cJSON_AddStringToObject(cJSON* const object,
  335. const char* const name,
  336. const char* const string);
  337. pika_cJSON_PUBLIC(cJSON*) pika_cJSON_AddRawToObject(cJSON* const object,
  338. const char* const name,
  339. const char* const raw);
  340. pika_cJSON_PUBLIC(cJSON*)
  341. pika_cJSON_AddObjectToObject(cJSON* const object, const char* const name);
  342. pika_cJSON_PUBLIC(cJSON*)
  343. pika_cJSON_AddArrayToObject(cJSON* const object, const char* const name);
  344. /* When assigning an integer value, it needs to be propagated to valuedouble
  345. * too. */
  346. #define pika_cJSON_SetIntValue(object, number) \
  347. ((object) ? (object)->valueint = (object)->valuedouble = (number) \
  348. : (number))
  349. /* helper for the pika_cJSON_SetNumberValue macro */
  350. pika_cJSON_PUBLIC(double)
  351. pika_cJSON_SetNumberHelper(cJSON* object, double number);
  352. #define pika_cJSON_SetNumberValue(object, number) \
  353. ((object != NULL) ? pika_cJSON_SetNumberHelper(object, (double)number) \
  354. : (number))
  355. /* Change the valuestring of a pika_cJSON_String object, only takes effect
  356. * when type of object is pika_cJSON_String */
  357. pika_cJSON_PUBLIC(char*)
  358. pika_cJSON_SetValuestring(cJSON* object, const char* valuestring);
  359. /* Macro for iterating over an array or object */
  360. #define pika_cJSON_ArrayForEach(element, array) \
  361. for (element = (array != NULL) ? (array)->child : NULL; element != NULL; \
  362. element = element->next)
  363. /* malloc/free objects using the malloc/free functions that have been set
  364. * with pika_cJSON_InitHooks */
  365. pika_cJSON_PUBLIC(void*) pika_cJSON_malloc(size_t size);
  366. pika_cJSON_PUBLIC(void) pika_cJSON_free(void* object);
  367. #ifdef __cplusplus
  368. }
  369. #endif
  370. #endif