cJSON.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 cJSON__h
  20. #define cJSON__h
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #if !defined(__WINDOWS__) \
  25. && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) \
  26. || defined(_WIN32))
  27. #define __WINDOWS__
  28. #endif
  29. #ifdef __WINDOWS__
  30. /**
  31. * When compiling for windows, we specify a specific calling convention to avoid
  32. * issues where we are being called from a project with a different default
  33. * calling convention. For windows you have 3 define options:
  34. * CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever
  35. * dllexport symbols
  36. * CJSON_EXPORT_SYMBOLS - Define this on library build when you want to
  37. * dllexport symbols (default)
  38. * CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
  39. *
  40. * For *nix builds that support visibility attribute, you can define similar
  41. * behavior by setting default visibility to hidden by adding
  42. * -fvisibility=hidden (for gcc)
  43. * or
  44. * -xldscope=hidden (for sun cc)
  45. * to CFLAGS, then using the CJSON_API_VISIBILITY flag to "export" the same
  46. * symbols the way CJSON_EXPORT_SYMBOLS does
  47. */
  48. #define CJSON_CDECL __cdecl
  49. #define CJSON_STDCALL __stdcall
  50. /* export symbols by default, this is necessary for copy pasting the C and
  51. header file */
  52. #if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) \
  53. && !defined(CJSON_EXPORT_SYMBOLS)
  54. #define CJSON_EXPORT_SYMBOLS
  55. #endif
  56. #if defined(CJSON_HIDE_SYMBOLS)
  57. #define CJSON_PUBLIC(type) type CJSON_STDCALL
  58. #elif defined(CJSON_EXPORT_SYMBOLS)
  59. #define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
  60. #elif defined(CJSON_IMPORT_SYMBOLS)
  61. #define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
  62. #endif
  63. #else /* !__WINDOWS__ */
  64. #define CJSON_CDECL
  65. #define CJSON_STDCALL
  66. #if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) \
  67. && defined(CJSON_API_VISIBILITY)
  68. #define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
  69. #else
  70. #define CJSON_PUBLIC(type) type
  71. #endif
  72. #endif
  73. /* project version */
  74. #define CJSON_VERSION_MAJOR 1
  75. #define CJSON_VERSION_MINOR 7
  76. #define CJSON_VERSION_PATCH 10
  77. #include <stddef.h>
  78. /* cJSON Types: */
  79. #define cJSON_Invalid (0)
  80. #define cJSON_False (1 << 0)
  81. #define cJSON_True (1 << 1)
  82. #define cJSON_NULL (1 << 2)
  83. #define cJSON_Number (1 << 3)
  84. #define cJSON_String (1 << 4)
  85. #define cJSON_Array (1 << 5)
  86. #define cJSON_Object (1 << 6)
  87. #define cJSON_Raw (1 << 7) /* raw json */
  88. #define cJSON_IsReference 256
  89. #define cJSON_StringIsConst 512
  90. /* The cJSON structure: */
  91. typedef struct cJSON {
  92. /* next/prev allow you to walk array/object chains. Alternatively, use
  93. GetArraySize/GetArrayItem/GetObjectItem */
  94. struct cJSON *next;
  95. struct cJSON *prev;
  96. /* An array or object item will have a child pointer pointing to a chain of
  97. the items in the array/object. */
  98. struct cJSON *child;
  99. /* The type of the item, as above. */
  100. int type;
  101. /* The item's string, if type==cJSON_String and type == cJSON_Raw */
  102. char *valuestring;
  103. /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
  104. int valueint;
  105. /* The item's number, if type==cJSON_Number */
  106. double valuedouble;
  107. /* The item's name string, if this item is the child of, or is in the list
  108. of subitems of an object. */
  109. char *string;
  110. } cJSON;
  111. typedef struct cJSON_Hooks {
  112. /* malloc/free are CDECL on Windows regardless of the default calling
  113. * convention of the compiler, so ensure the hooks allow passing those
  114. * functions directly. */
  115. void *(CJSON_CDECL *malloc_fn)(size_t sz);
  116. void(CJSON_CDECL *free_fn)(void *ptr);
  117. } cJSON_Hooks;
  118. typedef int cJSON_bool;
  119. /* Limits how deeply nested arrays/objects can be before cJSON rejects to parse
  120. them. This is to prevent stack overflows. */
  121. #ifndef CJSON_NESTING_LIMIT
  122. #define CJSON_NESTING_LIMIT 1000
  123. #endif
  124. /* returns the version of cJSON as a string */
  125. CJSON_PUBLIC(const char *) cJSON_Version(void);
  126. /* Supply malloc, realloc and free functions to cJSON */
  127. CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks);
  128. /* Memory Management: the caller is always responsible to free the results from
  129. * all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib
  130. * free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is
  131. * cJSON_PrintPreallocated, where the caller has full responsibility of the
  132. * buffer. */
  133. /* Supply a block of JSON, and this returns a cJSON object you can interrogate.
  134. */
  135. CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
  136. /* ParseWithOpts allows you to require (and check) that the JSON is null
  137. * terminated, and to retrieve the pointer to the final byte parsed. */
  138. /* If you supply a ptr in return_parse_end and parsing fails, then
  139. * return_parse_end will contain a pointer to the error so will match
  140. * cJSON_GetErrorPtr(). */
  141. CJSON_PUBLIC(cJSON *)
  142. cJSON_ParseWithOpts(const char *value, const char **return_parse_end,
  143. cJSON_bool require_null_terminated);
  144. /* Render a cJSON entity to text for transfer/storage. */
  145. CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
  146. /* Render a cJSON entity to text for transfer/storage without any formatting. */
  147. CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
  148. /* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess
  149. * at the final size. guessing well reduces reallocation. fmt=0 gives
  150. * unformatted, =1 gives formatted */
  151. CJSON_PUBLIC(char *)
  152. cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
  153. /* Render a cJSON entity to text using a buffer already allocated in memory with
  154. * given length. Returns 1 on success and 0 on failure. */
  155. /* NOTE: cJSON is not always 100% accurate in estimating how much memory it will
  156. * use, so to be safe allocate 5 bytes more than you actually need */
  157. CJSON_PUBLIC(cJSON_bool)
  158. cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length,
  159. const cJSON_bool format);
  160. /* Delete a cJSON entity and all subentities. */
  161. CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
  162. /* Returns the number of items in an array (or object). */
  163. CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
  164. /* Retrieve item number "index" from array "array". Returns NULL if
  165. * unsuccessful. */
  166. CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
  167. /* Get item "string" from object. Case insensitive. */
  168. CJSON_PUBLIC(cJSON *)
  169. cJSON_GetObjectItem(const cJSON *const object, const char *const string);
  170. CJSON_PUBLIC(cJSON *)
  171. cJSON_GetObjectItemCaseSensitive(const cJSON *const object,
  172. const char *const string);
  173. CJSON_PUBLIC(cJSON_bool)
  174. cJSON_HasObjectItem(const cJSON *object, const char *string);
  175. /* For analysing failed parses. This returns a pointer to the parse error.
  176. * You'll probably need to look a few chars back to make sense of it. Defined
  177. * when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
  178. CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
  179. /* Check if the item is a string and return its valuestring */
  180. CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
  181. /* These functions check the type of an item */
  182. CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *const item);
  183. CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON *const item);
  184. CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON *const item);
  185. CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON *const item);
  186. CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON *const item);
  187. CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *const item);
  188. CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *const item);
  189. CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *const item);
  190. CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *const item);
  191. CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *const item);
  192. /* These calls create a cJSON item of the appropriate type. */
  193. CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
  194. CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
  195. CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
  196. CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
  197. CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
  198. CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
  199. /* raw json */
  200. CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
  201. CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
  202. CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
  203. /* Create a string where valuestring references a string so
  204. it will not be freed by cJSON_Delete */
  205. CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
  206. /* Create an object/arrray that only references it's elements so
  207. they will not be freed by cJSON_Delete */
  208. CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
  209. CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
  210. /* These utilities create an Array of count items. */
  211. CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
  212. CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
  213. CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
  214. CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
  215. /* Append item to the specified array/object. */
  216. CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
  217. CJSON_PUBLIC(cJSON_bool)
  218. cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
  219. /* Use this when string is definitely const (i.e. a literal, or as good as), and
  220. * will definitely survive the cJSON object. WARNING: When this function was
  221. * used, make sure to always check that (item->type & cJSON_StringIsConst) is
  222. * zero before writing to `item->string` */
  223. CJSON_PUBLIC(cJSON_bool)
  224. cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
  225. /* Append reference to item to the specified array/object. Use this when you
  226. * want to add an existing cJSON to a new cJSON, but don't want to corrupt your
  227. * existing cJSON. */
  228. CJSON_PUBLIC(cJSON_bool)
  229. cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
  230. CJSON_PUBLIC(cJSON_bool)
  231. cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
  232. /* Remove/Detatch items from Arrays/Objects. */
  233. CJSON_PUBLIC(cJSON *)
  234. cJSON_DetachItemViaPointer(cJSON *parent, cJSON *const item);
  235. CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
  236. CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
  237. CJSON_PUBLIC(cJSON *)
  238. cJSON_DetachItemFromObject(cJSON *object, const char *string);
  239. CJSON_PUBLIC(cJSON *)
  240. cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
  241. CJSON_PUBLIC(void)
  242. cJSON_DeleteItemFromObject(cJSON *object, const char *string);
  243. CJSON_PUBLIC(void)
  244. cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
  245. /* Update array items. */
  246. CJSON_PUBLIC(cJSON_bool)
  247. cJSON_InsertItemInArray(
  248. cJSON *array, int which,
  249. cJSON *newitem); /* Shifts pre-existing items to the right. */
  250. CJSON_PUBLIC(cJSON_bool)
  251. cJSON_ReplaceItemViaPointer(cJSON *const parent, cJSON *const item,
  252. cJSON *replacement);
  253. CJSON_PUBLIC(void)
  254. cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
  255. CJSON_PUBLIC(void)
  256. cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem);
  257. CJSON_PUBLIC(void)
  258. cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string,
  259. cJSON *newitem);
  260. /* Duplicate a cJSON item */
  261. CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
  262. /* Duplicate will create a new, identical cJSON item to the one you pass, in new
  263. memory that will need to be released. With recurse!=0, it will duplicate any
  264. children connected to the item. The item->next and ->prev pointers are always
  265. zero on return from Duplicate. */
  266. /* Recursively compare two cJSON items for equality. If either a or b is NULL or
  267. * invalid, they will be considered unequal.
  268. * case_sensitive determines if object keys are treated case sensitive (1) or
  269. * case insensitive (0) */
  270. CJSON_PUBLIC(cJSON_bool)
  271. cJSON_Compare(const cJSON *const a, const cJSON *const b,
  272. const cJSON_bool case_sensitive);
  273. CJSON_PUBLIC(void) cJSON_Minify(char *json);
  274. /* Helper functions for creating and adding items to an object at the same time.
  275. They return the added item or NULL on failure. */
  276. CJSON_PUBLIC(cJSON *)
  277. cJSON_AddNullToObject(cJSON *const object, const char *const name);
  278. CJSON_PUBLIC(cJSON *)
  279. cJSON_AddTrueToObject(cJSON *const object, const char *const name);
  280. CJSON_PUBLIC(cJSON *)
  281. cJSON_AddFalseToObject(cJSON *const object, const char *const name);
  282. CJSON_PUBLIC(cJSON *)
  283. cJSON_AddBoolToObject(cJSON *const object, const char *const name,
  284. const cJSON_bool boolean);
  285. CJSON_PUBLIC(cJSON *)
  286. cJSON_AddNumberToObject(cJSON *const object, const char *const name,
  287. const double number);
  288. CJSON_PUBLIC(cJSON *)
  289. cJSON_AddStringToObject(cJSON *const object, const char *const name,
  290. const char *const string);
  291. CJSON_PUBLIC(cJSON *)
  292. cJSON_AddRawToObject(cJSON *const object, const char *const name,
  293. const char *const raw);
  294. CJSON_PUBLIC(cJSON *)
  295. cJSON_AddObjectToObject(cJSON *const object, const char *const name);
  296. CJSON_PUBLIC(cJSON *)
  297. cJSON_AddArrayToObject(cJSON *const object, const char *const name);
  298. /* When assigning an integer value, it needs to be propagated to valuedouble
  299. too. */
  300. #define cJSON_SetIntValue(object, number) \
  301. ((object) ? (object)->valueint = (object)->valuedouble = (number) \
  302. : (number))
  303. /* helper for the cJSON_SetNumberValue macro */
  304. CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
  305. #define cJSON_SetNumberValue(object, number) \
  306. ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) \
  307. : (number))
  308. /* Macro for iterating over an array or object */
  309. #define cJSON_ArrayForEach(element, array) \
  310. for (element = (array != NULL) ? (array)->child : NULL; element != NULL; \
  311. element = element->next)
  312. /* malloc/free objects using the malloc/free functions that have been set with
  313. cJSON_InitHooks */
  314. CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
  315. CJSON_PUBLIC(void) cJSON_free(void *object);
  316. #ifdef __cplusplus
  317. }
  318. #endif
  319. #endif