cJSON.h 16 KB

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