1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun Copyright (c) 2009 Dave Gamble 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 /* cJSON Types: */ 32*4882a593Smuzhiyun #define cJSON_False 0 33*4882a593Smuzhiyun #define cJSON_True 1 34*4882a593Smuzhiyun #define cJSON_NULL 2 35*4882a593Smuzhiyun #define cJSON_Number 3 36*4882a593Smuzhiyun #define cJSON_String 4 37*4882a593Smuzhiyun #define cJSON_Array 5 38*4882a593Smuzhiyun #define cJSON_Object 6 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun #define cJSON_IsReference 256 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun /* The cJSON structure: */ 43*4882a593Smuzhiyun typedef struct cJSON { 44*4882a593Smuzhiyun struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ 45*4882a593Smuzhiyun struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun int type; /* The type of the item, as above. */ 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun char *valuestring; /* The item's string, if type==cJSON_String */ 50*4882a593Smuzhiyun int valueint; /* The item's number, if type==cJSON_Number */ 51*4882a593Smuzhiyun double valuedouble; /* The item's number, if type==cJSON_Number */ 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ 54*4882a593Smuzhiyun } cJSON; 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun typedef struct cJSON_Hooks { 57*4882a593Smuzhiyun void *(*malloc_fn)(size_t sz); 58*4882a593Smuzhiyun void (*free_fn)(void *ptr); 59*4882a593Smuzhiyun } cJSON_Hooks; 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun /* Supply malloc, realloc and free functions to cJSON */ 62*4882a593Smuzhiyun extern void cJSON_InitHooks(cJSON_Hooks* hooks); 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */ 66*4882a593Smuzhiyun extern cJSON *cJSON_Parse(const char *value); 67*4882a593Smuzhiyun /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */ 68*4882a593Smuzhiyun extern char *cJSON_Print(cJSON *item); 69*4882a593Smuzhiyun /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ 70*4882a593Smuzhiyun extern char *cJSON_PrintUnformatted(cJSON *item); 71*4882a593Smuzhiyun /* Delete a cJSON entity and all subentities. */ 72*4882a593Smuzhiyun extern void cJSON_Delete(cJSON *c); 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun /* Returns the number of items in an array (or object). */ 75*4882a593Smuzhiyun extern int cJSON_GetArraySize(cJSON *array); 76*4882a593Smuzhiyun /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ 77*4882a593Smuzhiyun extern cJSON *cJSON_GetArrayItem(cJSON *array,int item); 78*4882a593Smuzhiyun /* Get item "string" from object. Case insensitive. */ 79*4882a593Smuzhiyun extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); 80*4882a593Smuzhiyun 81*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. */ 82*4882a593Smuzhiyun extern const char *cJSON_GetErrorPtr(void); 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun /* These calls create a cJSON item of the appropriate type. */ 85*4882a593Smuzhiyun extern cJSON *cJSON_CreateNull(void); 86*4882a593Smuzhiyun extern cJSON *cJSON_CreateTrue(void); 87*4882a593Smuzhiyun extern cJSON *cJSON_CreateFalse(void); 88*4882a593Smuzhiyun extern cJSON *cJSON_CreateBool(int b); 89*4882a593Smuzhiyun extern cJSON *cJSON_CreateNumber(double num); 90*4882a593Smuzhiyun extern cJSON *cJSON_CreateString(const char *string); 91*4882a593Smuzhiyun extern cJSON *cJSON_CreateArray(void); 92*4882a593Smuzhiyun extern cJSON *cJSON_CreateObject(void); 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun /* These utilities create an Array of count items. */ 95*4882a593Smuzhiyun extern cJSON *cJSON_CreateIntArray(const int *numbers,int count); 96*4882a593Smuzhiyun extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count); 97*4882a593Smuzhiyun extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count); 98*4882a593Smuzhiyun extern cJSON *cJSON_CreateStringArray(const char **strings,int count); 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun /* Append item to the specified array/object. */ 101*4882a593Smuzhiyun extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); 102*4882a593Smuzhiyun extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item); 103*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. */ 104*4882a593Smuzhiyun extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); 105*4882a593Smuzhiyun extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item); 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun /* Remove/Detatch items from Arrays/Objects. */ 108*4882a593Smuzhiyun extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which); 109*4882a593Smuzhiyun extern void cJSON_DeleteItemFromArray(cJSON *array,int which); 110*4882a593Smuzhiyun extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string); 111*4882a593Smuzhiyun extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string); 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun /* Update array items. */ 114*4882a593Smuzhiyun extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem); 115*4882a593Smuzhiyun extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun /* Duplicate a cJSON item */ 118*4882a593Smuzhiyun extern cJSON *cJSON_Duplicate(cJSON *item,int recurse); 119*4882a593Smuzhiyun /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will 120*4882a593Smuzhiyun need to be released. With recurse!=0, it will duplicate any children connected to the item. 121*4882a593Smuzhiyun The item->next and ->prev pointers are always zero on return from Duplicate. */ 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ 124*4882a593Smuzhiyun extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated); 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun extern void cJSON_Minify(char *json); 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun /* Macros for creating things quickly. */ 129*4882a593Smuzhiyun #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) 130*4882a593Smuzhiyun #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) 131*4882a593Smuzhiyun #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) 132*4882a593Smuzhiyun #define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) 133*4882a593Smuzhiyun #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) 134*4882a593Smuzhiyun #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun /* When assigning an integer value, it needs to be propagated to valuedouble too. */ 137*4882a593Smuzhiyun #define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun #ifdef __cplusplus 140*4882a593Smuzhiyun } 141*4882a593Smuzhiyun #endif 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun #endif 144