cJSON.h 16 KB

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