Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-03 08:32:23

0001 /*
0002   Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
0003 
0004   Permission is hereby granted, free of charge, to any person obtaining a copy
0005   of this software and associated documentation files (the "Software"), to deal
0006   in the Software without restriction, including without limitation the rights
0007   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008   copies of the Software, and to permit persons to whom the Software is
0009   furnished to do so, subject to the following conditions:
0010 
0011   The above copyright notice and this permission notice shall be included in
0012   all copies or substantial portions of the Software.
0013 
0014   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0020   THE SOFTWARE.
0021 */
0022 
0023 #ifndef cJSON__h
0024 #define cJSON__h
0025 
0026 #ifdef __cplusplus
0027 extern "C"
0028 {
0029 #endif
0030 
0031 #if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
0032 #define __WINDOWS__
0033 #endif
0034 
0035 #ifdef __WINDOWS__
0036 
0037 /* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention.  For windows you have 3 define options:
0038 
0039 CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
0040 CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
0041 CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
0042 
0043 For *nix builds that support visibility attribute, you can define similar behavior by
0044 
0045 setting default visibility to hidden by adding
0046 -fvisibility=hidden (for gcc)
0047 or
0048 -xldscope=hidden (for sun cc)
0049 to CFLAGS
0050 
0051 then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
0052 
0053 */
0054 
0055 #define CJSON_CDECL __cdecl
0056 #define CJSON_STDCALL __stdcall
0057 
0058 /* export symbols by default, this is necessary for copy pasting the C and header file */
0059 #if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
0060 #define CJSON_EXPORT_SYMBOLS
0061 #endif
0062 
0063 #if defined(CJSON_HIDE_SYMBOLS)
0064 #define CJSON_PUBLIC(type)   type CJSON_STDCALL
0065 #elif defined(CJSON_EXPORT_SYMBOLS)
0066 #define CJSON_PUBLIC(type)   __declspec(dllexport) type CJSON_STDCALL
0067 #elif defined(CJSON_IMPORT_SYMBOLS)
0068 #define CJSON_PUBLIC(type)   __declspec(dllimport) type CJSON_STDCALL
0069 #endif
0070 #else /* !__WINDOWS__ */
0071 #define CJSON_CDECL
0072 #define CJSON_STDCALL
0073 
0074 #if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
0075 #define CJSON_PUBLIC(type)   __attribute__((visibility("default"))) type
0076 #else
0077 #define CJSON_PUBLIC(type) type
0078 #endif
0079 #endif
0080 
0081 /* project version */
0082 #define CJSON_VERSION_MAJOR 1
0083 #define CJSON_VERSION_MINOR 7
0084 #define CJSON_VERSION_PATCH 18
0085 
0086 #include <stddef.h>
0087 
0088 /* cJSON Types: */
0089 #define cJSON_Invalid (0)
0090 #define cJSON_False  (1 << 0)
0091 #define cJSON_True   (1 << 1)
0092 #define cJSON_NULL   (1 << 2)
0093 #define cJSON_Number (1 << 3)
0094 #define cJSON_String (1 << 4)
0095 #define cJSON_Array  (1 << 5)
0096 #define cJSON_Object (1 << 6)
0097 #define cJSON_Raw    (1 << 7) /* raw json */
0098 
0099 #define cJSON_IsReference 256
0100 #define cJSON_StringIsConst 512
0101 
0102 /* The cJSON structure: */
0103 typedef struct cJSON
0104 {
0105     /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
0106     struct cJSON *next;
0107     struct cJSON *prev;
0108     /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
0109     struct cJSON *child;
0110 
0111     /* The type of the item, as above. */
0112     int type;
0113 
0114     /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
0115     char *valuestring;
0116     /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
0117     int valueint;
0118     /* The item's number, if type==cJSON_Number */
0119     double valuedouble;
0120 
0121     /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
0122     char *string;
0123 } cJSON;
0124 
0125 typedef struct cJSON_Hooks
0126 {
0127       /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
0128       void *(CJSON_CDECL *malloc_fn)(size_t sz);
0129       void (CJSON_CDECL *free_fn)(void *ptr);
0130 } cJSON_Hooks;
0131 
0132 typedef int cJSON_bool;
0133 
0134 /* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
0135  * This is to prevent stack overflows. */
0136 #ifndef CJSON_NESTING_LIMIT
0137 #define CJSON_NESTING_LIMIT 1000
0138 #endif
0139 
0140 /* returns the version of cJSON as a string */
0141 CJSON_PUBLIC(const char*) cJSON_Version(void);
0142 
0143 /* Supply malloc, realloc and free functions to cJSON */
0144 CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
0145 
0146 /* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
0147 /* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
0148 CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
0149 CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
0150 /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
0151 /* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
0152 CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
0153 CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
0154 
0155 /* Render a cJSON entity to text for transfer/storage. */
0156 CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
0157 /* Render a cJSON entity to text for transfer/storage without any formatting. */
0158 CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
0159 /* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
0160 CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
0161 /* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
0162 /* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
0163 CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
0164 /* Delete a cJSON entity and all subentities. */
0165 CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
0166 
0167 /* Returns the number of items in an array (or object). */
0168 CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
0169 /* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
0170 CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
0171 /* Get item "string" from object. Case insensitive. */
0172 CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
0173 CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
0174 CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
0175 /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
0176 CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
0177 
0178 /* Check item type and return its value */
0179 CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
0180 CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
0181 
0182 /* These functions check the type of an item */
0183 CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
0184 CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
0185 CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
0186 CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
0187 CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
0188 CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
0189 CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
0190 CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
0191 CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
0192 CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
0193 
0194 /* These calls create a cJSON item of the appropriate type. */
0195 CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
0196 CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
0197 CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
0198 CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
0199 CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
0200 CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
0201 /* raw json */
0202 CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
0203 CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
0204 CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
0205 
0206 /* Create a string where valuestring references a string so
0207  * it will not be freed by cJSON_Delete */
0208 CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
0209 /* Create an object/array that only references it's elements so
0210  * they will not be freed by cJSON_Delete */
0211 CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
0212 CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
0213 
0214 /* These utilities create an Array of count items.
0215  * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
0216 CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
0217 CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
0218 CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
0219 CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
0220 
0221 /* Append item to the specified array/object. */
0222 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
0223 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
0224 /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
0225  * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
0226  * writing to `item->string` */
0227 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
0228 /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
0229 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
0230 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
0231 
0232 /* Remove/Detach items from Arrays/Objects. */
0233 CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
0234 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
0235 CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
0236 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
0237 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
0238 CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
0239 CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
0240 
0241 /* Update array items. */
0242 CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
0243 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
0244 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
0245 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
0246 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
0247 
0248 /* Duplicate a cJSON item */
0249 CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
0250 /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
0251  * need to be released. With recurse!=0, it will duplicate any children connected to the item.
0252  * The item->next and ->prev pointers are always zero on return from Duplicate. */
0253 /* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
0254  * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
0255 CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
0256 
0257 /* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
0258  * The input pointer json cannot point to a read-only address area, such as a string constant, 
0259  * but should point to a readable and writable address area. */
0260 CJSON_PUBLIC(void) cJSON_Minify(char *json);
0261 
0262 /* Helper functions for creating and adding items to an object at the same time.
0263  * They return the added item or NULL on failure. */
0264 CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
0265 CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
0266 CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
0267 CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
0268 CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
0269 CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
0270 CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
0271 CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
0272 CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
0273 
0274 /* When assigning an integer value, it needs to be propagated to valuedouble too. */
0275 #define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
0276 /* helper for the cJSON_SetNumberValue macro */
0277 CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
0278 #define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
0279 /* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
0280 CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
0281 
0282 /* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/
0283 #define cJSON_SetBoolValue(object, boolValue) ( \
0284     (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \
0285     (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \
0286     cJSON_Invalid\
0287 )
0288 
0289 /* Macro for iterating over an array or object */
0290 #define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
0291 
0292 /* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
0293 CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
0294 CJSON_PUBLIC(void) cJSON_free(void *object);
0295 
0296 #ifdef __cplusplus
0297 }
0298 #endif
0299 
0300 #endif