1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun Copyright (c) 2009-2017 Dave Gamble and cJSON contributors 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun Permission is hereby granted, free of charge, to any person obtaining a copy 5*4882a593Smuzhiyun of this software and associated documentation files (the "Software"), to deal 6*4882a593Smuzhiyun in the Software without restriction, including without limitation the rights 7*4882a593Smuzhiyun to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8*4882a593Smuzhiyun copies of the Software, and to permit persons to whom the Software is 9*4882a593Smuzhiyun furnished to do so, subject to the following conditions: 10*4882a593Smuzhiyun 11*4882a593Smuzhiyun The above copyright notice and this permission notice shall be included in 12*4882a593Smuzhiyun all copies or substantial portions of the Software. 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15*4882a593Smuzhiyun IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16*4882a593Smuzhiyun FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17*4882a593Smuzhiyun AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18*4882a593Smuzhiyun LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19*4882a593Smuzhiyun OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20*4882a593Smuzhiyun THE SOFTWARE. 21*4882a593Smuzhiyun */ 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun #ifndef cJSON__h 24*4882a593Smuzhiyun #define cJSON__h 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun #ifdef __cplusplus 27*4882a593Smuzhiyun extern "C" 28*4882a593Smuzhiyun { 29*4882a593Smuzhiyun #endif 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun #if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) 32*4882a593Smuzhiyun #define __WINDOWS__ 33*4882a593Smuzhiyun #endif 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun #ifdef __WINDOWS__ 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun /* 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: 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols 40*4882a593Smuzhiyun CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) 41*4882a593Smuzhiyun CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun For *nix builds that support visibility attribute, you can define similar behavior by 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun setting default visibility to hidden by adding 46*4882a593Smuzhiyun -fvisibility=hidden (for gcc) 47*4882a593Smuzhiyun or 48*4882a593Smuzhiyun -xldscope=hidden (for sun cc) 49*4882a593Smuzhiyun to CFLAGS 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun */ 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun #define CJSON_CDECL __cdecl 56*4882a593Smuzhiyun #define CJSON_STDCALL __stdcall 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun /* export symbols by default, this is necessary for copy pasting the C and header file */ 59*4882a593Smuzhiyun #if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) 60*4882a593Smuzhiyun #define CJSON_EXPORT_SYMBOLS 61*4882a593Smuzhiyun #endif 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun #if defined(CJSON_HIDE_SYMBOLS) 64*4882a593Smuzhiyun #define CJSON_PUBLIC(type) type CJSON_STDCALL 65*4882a593Smuzhiyun #elif defined(CJSON_EXPORT_SYMBOLS) 66*4882a593Smuzhiyun #define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL 67*4882a593Smuzhiyun #elif defined(CJSON_IMPORT_SYMBOLS) 68*4882a593Smuzhiyun #define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL 69*4882a593Smuzhiyun #endif 70*4882a593Smuzhiyun #else /* !__WINDOWS__ */ 71*4882a593Smuzhiyun #define CJSON_CDECL 72*4882a593Smuzhiyun #define CJSON_STDCALL 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun #if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) 75*4882a593Smuzhiyun #define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type 76*4882a593Smuzhiyun #else 77*4882a593Smuzhiyun #define CJSON_PUBLIC(type) type 78*4882a593Smuzhiyun #endif 79*4882a593Smuzhiyun #endif 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun /* project version */ 82*4882a593Smuzhiyun #define CJSON_VERSION_MAJOR 1 83*4882a593Smuzhiyun #define CJSON_VERSION_MINOR 7 84*4882a593Smuzhiyun #define CJSON_VERSION_PATCH 15 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun #include <stddef.h> 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun /* cJSON Types: */ 89*4882a593Smuzhiyun #define cJSON_Invalid (0) 90*4882a593Smuzhiyun #define cJSON_False (1 << 0) 91*4882a593Smuzhiyun #define cJSON_True (1 << 1) 92*4882a593Smuzhiyun #define cJSON_NULL (1 << 2) 93*4882a593Smuzhiyun #define cJSON_Number (1 << 3) 94*4882a593Smuzhiyun #define cJSON_String (1 << 4) 95*4882a593Smuzhiyun #define cJSON_Array (1 << 5) 96*4882a593Smuzhiyun #define cJSON_Object (1 << 6) 97*4882a593Smuzhiyun #define cJSON_Raw (1 << 7) /* raw json */ 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun #define cJSON_IsReference 256 100*4882a593Smuzhiyun #define cJSON_StringIsConst 512 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun /* The cJSON structure: */ 103*4882a593Smuzhiyun typedef struct cJSON 104*4882a593Smuzhiyun { 105*4882a593Smuzhiyun /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ 106*4882a593Smuzhiyun struct cJSON *next; 107*4882a593Smuzhiyun struct cJSON *prev; 108*4882a593Smuzhiyun /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ 109*4882a593Smuzhiyun struct cJSON *child; 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun /* The type of the item, as above. */ 112*4882a593Smuzhiyun int type; 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun /* The item's string, if type==cJSON_String and type == cJSON_Raw */ 115*4882a593Smuzhiyun char *valuestring; 116*4882a593Smuzhiyun /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ 117*4882a593Smuzhiyun int valueint; 118*4882a593Smuzhiyun /* The item's number, if type==cJSON_Number */ 119*4882a593Smuzhiyun double valuedouble; 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ 122*4882a593Smuzhiyun char *string; 123*4882a593Smuzhiyun } cJSON; 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun typedef struct cJSON_Hooks 126*4882a593Smuzhiyun { 127*4882a593Smuzhiyun /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ 128*4882a593Smuzhiyun void *(CJSON_CDECL *malloc_fn)(size_t sz); 129*4882a593Smuzhiyun void (CJSON_CDECL *free_fn)(void *ptr); 130*4882a593Smuzhiyun } cJSON_Hooks; 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun typedef int cJSON_bool; 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun /* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. 135*4882a593Smuzhiyun * This is to prevent stack overflows. */ 136*4882a593Smuzhiyun #ifndef CJSON_NESTING_LIMIT 137*4882a593Smuzhiyun #define CJSON_NESTING_LIMIT 1000 138*4882a593Smuzhiyun #endif 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun /* returns the version of cJSON as a string */ 141*4882a593Smuzhiyun CJSON_PUBLIC(const char*) cJSON_Version(void); 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun /* Supply malloc, realloc and free functions to cJSON */ 144*4882a593Smuzhiyun CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun /* 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. */ 147*4882a593Smuzhiyun /* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ 148*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); 149*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); 150*4882a593Smuzhiyun /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ 151*4882a593Smuzhiyun /* 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(). */ 152*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); 153*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated); 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun /* Render a cJSON entity to text for transfer/storage. */ 156*4882a593Smuzhiyun CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); 157*4882a593Smuzhiyun /* Render a cJSON entity to text for transfer/storage without any formatting. */ 158*4882a593Smuzhiyun CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); 159*4882a593Smuzhiyun /* 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 */ 160*4882a593Smuzhiyun CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); 161*4882a593Smuzhiyun /* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ 162*4882a593Smuzhiyun /* 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 */ 163*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); 164*4882a593Smuzhiyun /* Delete a cJSON entity and all subentities. */ 165*4882a593Smuzhiyun CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun /* Returns the number of items in an array (or object). */ 168*4882a593Smuzhiyun CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); 169*4882a593Smuzhiyun /* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ 170*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); 171*4882a593Smuzhiyun /* Get item "string" from object. Case insensitive. */ 172*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); 173*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); 174*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); 175*4882a593Smuzhiyun /* 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. */ 176*4882a593Smuzhiyun CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun /* Check item type and return its value */ 179*4882a593Smuzhiyun CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); 180*4882a593Smuzhiyun CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item); 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun /* These functions check the type of an item */ 183*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); 184*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); 185*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); 186*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); 187*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); 188*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); 189*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); 190*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); 191*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); 192*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun /* These calls create a cJSON item of the appropriate type. */ 195*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); 196*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); 197*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); 198*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); 199*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); 200*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); 201*4882a593Smuzhiyun /* raw json */ 202*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); 203*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); 204*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); 205*4882a593Smuzhiyun 206*4882a593Smuzhiyun /* Create a string where valuestring references a string so 207*4882a593Smuzhiyun * it will not be freed by cJSON_Delete */ 208*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); 209*4882a593Smuzhiyun /* Create an object/array that only references it's elements so 210*4882a593Smuzhiyun * they will not be freed by cJSON_Delete */ 211*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); 212*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun /* These utilities create an Array of count items. 215*4882a593Smuzhiyun * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ 216*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); 217*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); 218*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); 219*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun /* Append item to the specified array/object. */ 222*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); 223*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); 224*4882a593Smuzhiyun /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. 225*4882a593Smuzhiyun * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before 226*4882a593Smuzhiyun * writing to `item->string` */ 227*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); 228*4882a593Smuzhiyun /* 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. */ 229*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); 230*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun /* Remove/Detach items from Arrays/Objects. */ 233*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); 234*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); 235*4882a593Smuzhiyun CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); 236*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); 237*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); 238*4882a593Smuzhiyun CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); 239*4882a593Smuzhiyun CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); 240*4882a593Smuzhiyun 241*4882a593Smuzhiyun /* Update array items. */ 242*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ 243*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); 244*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); 245*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); 246*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun /* Duplicate a cJSON item */ 249*4882a593Smuzhiyun CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); 250*4882a593Smuzhiyun /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will 251*4882a593Smuzhiyun * need to be released. With recurse!=0, it will duplicate any children connected to the item. 252*4882a593Smuzhiyun * The item->next and ->prev pointers are always zero on return from Duplicate. */ 253*4882a593Smuzhiyun /* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. 254*4882a593Smuzhiyun * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ 255*4882a593Smuzhiyun CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun /* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. 258*4882a593Smuzhiyun * The input pointer json cannot point to a read-only address area, such as a string constant, 259*4882a593Smuzhiyun * but should point to a readable and writable address area. */ 260*4882a593Smuzhiyun CJSON_PUBLIC(void) cJSON_Minify(char *json); 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun /* Helper functions for creating and adding items to an object at the same time. 263*4882a593Smuzhiyun * They return the added item or NULL on failure. */ 264*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); 265*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); 266*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); 267*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); 268*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); 269*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); 270*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); 271*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); 272*4882a593Smuzhiyun CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); 273*4882a593Smuzhiyun 274*4882a593Smuzhiyun /* When assigning an integer value, it needs to be propagated to valuedouble too. */ 275*4882a593Smuzhiyun #define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) 276*4882a593Smuzhiyun /* helper for the cJSON_SetNumberValue macro */ 277*4882a593Smuzhiyun CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); 278*4882a593Smuzhiyun #define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) 279*4882a593Smuzhiyun /* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ 280*4882a593Smuzhiyun CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun /* Macro for iterating over an array or object */ 283*4882a593Smuzhiyun #define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun /* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ 286*4882a593Smuzhiyun CJSON_PUBLIC(void *) cJSON_malloc(size_t size); 287*4882a593Smuzhiyun CJSON_PUBLIC(void) cJSON_free(void *object); 288*4882a593Smuzhiyun 289*4882a593Smuzhiyun #ifdef __cplusplus 290*4882a593Smuzhiyun } 291*4882a593Smuzhiyun #endif 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun #endif 294