ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cJSON.h Source File

cJSON.h

00001 /*
00002   Copyright (c) 2009 Dave Gamble
00003  
00004   Permission is hereby granted, free of charge, to any person obtaining a copy
00005   of this software and associated documentation files (the "Software"), to deal
00006   in the Software without restriction, including without limitation the rights
00007   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008   copies of the Software, and to permit persons to whom the Software is
00009   furnished to do so, subject to the following conditions:
00010  
00011   The above copyright notice and this permission notice shall be included in
00012   all copies or substantial portions of the Software.
00013  
00014   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020   THE SOFTWARE.
00021 */
00022 
00023 #ifndef cJSON__h
00024 #define cJSON__h
00025 
00026 #ifdef __cplusplus
00027 extern "C"
00028 {
00029 #endif
00030 
00031 /* cJSON Types: */
00032 #define cJSON_False 0
00033 #define cJSON_True 1
00034 #define cJSON_NULL 2
00035 #define cJSON_Number 3
00036 #define cJSON_String 4
00037 #define cJSON_Array 5
00038 #define cJSON_Object 6
00039     
00040 #define cJSON_IsReference 256
00041 #define cJSON_StringIsConst 512
00042 
00043 /* The cJSON structure: */
00044 typedef struct cJSON {
00045     struct cJSON *next,*prev;   /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
00046     struct cJSON *child;        /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
00047 
00048     int type;                   /* The type of the item, as above. */
00049 
00050     char *valuestring;          /* The item's string, if type==cJSON_String */
00051     int valueint;               /* The item's number, if type==cJSON_Number */
00052     double valuedouble;         /* The item's number, if type==cJSON_Number */
00053 
00054     char *string;               /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
00055 } cJSON;
00056 
00057 typedef struct cJSON_Hooks {
00058       void *(*malloc_fn)(size_t sz);
00059       void (*free_fn)(void *ptr);
00060 } cJSON_Hooks;
00061 
00062 /* Supply malloc, realloc and free functions to cJSON */
00063 extern void cJSON_InitHooks(cJSON_Hooks* hooks);
00064 
00065 
00066 /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
00067 extern cJSON *cJSON_Parse(const char *value);
00068 /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
00069 extern char  *cJSON_Print(cJSON *item);
00070 /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
00071 extern char  *cJSON_PrintUnformatted(cJSON *item);
00072 /* 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 */
00073 extern char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt);
00074 /* Delete a cJSON entity and all subentities. */
00075 extern void   cJSON_Delete(cJSON *c);
00076 
00077 /* Returns the number of items in an array (or object). */
00078 extern int    cJSON_GetArraySize(cJSON *array);
00079 /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
00080 extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
00081 /* Get item "string" from object. Case insensitive. */
00082 extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
00083 
00084 /* 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. */
00085 extern const char *cJSON_GetErrorPtr(void);
00086     
00087 /* These calls create a cJSON item of the appropriate type. */
00088 extern cJSON *cJSON_CreateNull(void);
00089 extern cJSON *cJSON_CreateTrue(void);
00090 extern cJSON *cJSON_CreateFalse(void);
00091 extern cJSON *cJSON_CreateBool(int b);
00092 extern cJSON *cJSON_CreateNumber(double num);
00093 extern cJSON *cJSON_CreateString(const char *string);
00094 extern cJSON *cJSON_CreateArray(void);
00095 extern cJSON *cJSON_CreateObject(void);
00096 
00097 /* These utilities create an Array of count items. */
00098 extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
00099 extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
00100 extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
00101 extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
00102 
00103 /* Append item to the specified array/object. */
00104 extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
00105 extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
00106 extern void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item);  /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
00107 /* 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. */
00108 extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
00109 extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
00110 
00111 /* Remove/Detatch items from Arrays/Objects. */
00112 extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
00113 extern void   cJSON_DeleteItemFromArray(cJSON *array,int which);
00114 extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
00115 extern void   cJSON_DeleteItemFromObject(cJSON *object,const char *string);
00116     
00117 /* Update array items. */
00118 extern void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem); /* Shifts pre-existing items to the right. */
00119 extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
00120 extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
00121 
00122 /* Duplicate a cJSON item */
00123 extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
00124 /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
00125 need to be released. With recurse!=0, it will duplicate any children connected to the item.
00126 The item->next and ->prev pointers are always zero on return from Duplicate. */
00127 
00128 /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
00129 extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
00130 
00131 extern void cJSON_Minify(char *json);
00132 
00133 /* Macros for creating things quickly. */
00134 #define cJSON_AddNullToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateNull())
00135 #define cJSON_AddTrueToObject(object,name)      cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
00136 #define cJSON_AddFalseToObject(object,name)     cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
00137 #define cJSON_AddBoolToObject(object,name,b)    cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
00138 #define cJSON_AddNumberToObject(object,name,n)  cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
00139 #define cJSON_AddStringToObject(object,name,s)  cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
00140 
00141 /* When assigning an integer value, it needs to be propagated to valuedouble too. */
00142 #define cJSON_SetIntValue(object,val)           ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
00143 #define cJSON_SetNumberValue(object,val)        ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
00144 
00145 #ifdef __cplusplus
00146 }
00147 #endif
00148 
00149 #endif