1111
cJSON.h@1:9a1a30e27d1d, 2022-06-24 (annotated)
- Committer:
- yezhong
- Date:
- Fri Jun 24 01:46:30 2022 +0000
- Revision:
- 1:9a1a30e27d1d
- Parent:
- 0:c809010834e2
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TaylorGy | 0:c809010834e2 | 1 | /* |
TaylorGy | 0:c809010834e2 | 2 | Copyright (c) 2009 Dave Gamble |
TaylorGy | 0:c809010834e2 | 3 | |
TaylorGy | 0:c809010834e2 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
TaylorGy | 0:c809010834e2 | 5 | of this software and associated documentation files (the "Software"), to deal |
TaylorGy | 0:c809010834e2 | 6 | in the Software without restriction, including without limitation the rights |
TaylorGy | 0:c809010834e2 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
TaylorGy | 0:c809010834e2 | 8 | copies of the Software, and to permit persons to whom the Software is |
TaylorGy | 0:c809010834e2 | 9 | furnished to do so, subject to the following conditions: |
TaylorGy | 0:c809010834e2 | 10 | |
TaylorGy | 0:c809010834e2 | 11 | The above copyright notice and this permission notice shall be included in |
TaylorGy | 0:c809010834e2 | 12 | all copies or substantial portions of the Software. |
TaylorGy | 0:c809010834e2 | 13 | |
TaylorGy | 0:c809010834e2 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
TaylorGy | 0:c809010834e2 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
TaylorGy | 0:c809010834e2 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
TaylorGy | 0:c809010834e2 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
TaylorGy | 0:c809010834e2 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
TaylorGy | 0:c809010834e2 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
TaylorGy | 0:c809010834e2 | 20 | THE SOFTWARE. |
TaylorGy | 0:c809010834e2 | 21 | */ |
TaylorGy | 0:c809010834e2 | 22 | |
TaylorGy | 0:c809010834e2 | 23 | #ifndef cJSON__h |
TaylorGy | 0:c809010834e2 | 24 | #define cJSON__h |
TaylorGy | 0:c809010834e2 | 25 | |
TaylorGy | 0:c809010834e2 | 26 | #ifdef __cplusplus |
TaylorGy | 0:c809010834e2 | 27 | extern "C" |
TaylorGy | 0:c809010834e2 | 28 | { |
TaylorGy | 0:c809010834e2 | 29 | #endif |
TaylorGy | 0:c809010834e2 | 30 | |
TaylorGy | 0:c809010834e2 | 31 | /* cJSON Types: */ |
TaylorGy | 0:c809010834e2 | 32 | #define cJSON_False 0 |
TaylorGy | 0:c809010834e2 | 33 | #define cJSON_True 1 |
TaylorGy | 0:c809010834e2 | 34 | #define cJSON_NULL 2 |
TaylorGy | 0:c809010834e2 | 35 | #define cJSON_Number 3 |
TaylorGy | 0:c809010834e2 | 36 | #define cJSON_String 4 |
TaylorGy | 0:c809010834e2 | 37 | #define cJSON_Array 5 |
TaylorGy | 0:c809010834e2 | 38 | #define cJSON_Object 6 |
TaylorGy | 0:c809010834e2 | 39 | |
TaylorGy | 0:c809010834e2 | 40 | #define cJSON_IsReference 256 |
TaylorGy | 0:c809010834e2 | 41 | |
TaylorGy | 0:c809010834e2 | 42 | /* The cJSON structure: */ |
TaylorGy | 0:c809010834e2 | 43 | typedef struct cJSON { |
TaylorGy | 0:c809010834e2 | 44 | struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ |
TaylorGy | 0:c809010834e2 | 45 | struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ |
TaylorGy | 0:c809010834e2 | 46 | |
TaylorGy | 0:c809010834e2 | 47 | int type; /* The type of the item, as above. */ |
TaylorGy | 0:c809010834e2 | 48 | |
TaylorGy | 0:c809010834e2 | 49 | char *valuestring; /* The item's string, if type==cJSON_String */ |
TaylorGy | 0:c809010834e2 | 50 | int valueint; /* The item's number, if type==cJSON_Number */ |
TaylorGy | 0:c809010834e2 | 51 | double valuedouble; /* The item's number, if type==cJSON_Number */ |
TaylorGy | 0:c809010834e2 | 52 | |
TaylorGy | 0:c809010834e2 | 53 | char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ |
TaylorGy | 0:c809010834e2 | 54 | } cJSON; |
TaylorGy | 0:c809010834e2 | 55 | |
TaylorGy | 0:c809010834e2 | 56 | typedef struct cJSON_Hooks { |
TaylorGy | 0:c809010834e2 | 57 | void *(*malloc_fn)(size_t sz); |
TaylorGy | 0:c809010834e2 | 58 | void (*free_fn)(void *ptr); |
TaylorGy | 0:c809010834e2 | 59 | } cJSON_Hooks; |
TaylorGy | 0:c809010834e2 | 60 | |
TaylorGy | 0:c809010834e2 | 61 | /* Supply malloc, realloc and free functions to cJSON */ |
TaylorGy | 0:c809010834e2 | 62 | extern void cJSON_InitHooks(cJSON_Hooks* hooks); |
TaylorGy | 0:c809010834e2 | 63 | |
TaylorGy | 0:c809010834e2 | 64 | |
TaylorGy | 0:c809010834e2 | 65 | /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */ |
yezhong | 1:9a1a30e27d1d | 66 | extern cJSON *cJSON_Parse( char *value); |
TaylorGy | 0:c809010834e2 | 67 | /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */ |
TaylorGy | 0:c809010834e2 | 68 | extern char *cJSON_Print(cJSON *item); |
TaylorGy | 0:c809010834e2 | 69 | /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ |
TaylorGy | 0:c809010834e2 | 70 | extern char *cJSON_PrintUnformatted(cJSON *item); |
TaylorGy | 0:c809010834e2 | 71 | /* Delete a cJSON entity and all subentities. */ |
TaylorGy | 0:c809010834e2 | 72 | extern void cJSON_Delete(cJSON *c); |
TaylorGy | 0:c809010834e2 | 73 | |
TaylorGy | 0:c809010834e2 | 74 | /* Returns the number of items in an array (or object). */ |
TaylorGy | 0:c809010834e2 | 75 | extern int cJSON_GetArraySize(cJSON *array); |
TaylorGy | 0:c809010834e2 | 76 | /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ |
TaylorGy | 0:c809010834e2 | 77 | extern cJSON *cJSON_GetArrayItem(cJSON *array,int item); |
TaylorGy | 0:c809010834e2 | 78 | /* Get item "string" from object. Case insensitive. */ |
TaylorGy | 0:c809010834e2 | 79 | extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); |
TaylorGy | 0:c809010834e2 | 80 | |
TaylorGy | 0:c809010834e2 | 81 | /* 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. */ |
TaylorGy | 0:c809010834e2 | 82 | extern const char *cJSON_GetErrorPtr(void); |
TaylorGy | 0:c809010834e2 | 83 | |
TaylorGy | 0:c809010834e2 | 84 | /* These calls create a cJSON item of the appropriate type. */ |
TaylorGy | 0:c809010834e2 | 85 | extern cJSON *cJSON_CreateNull(void); |
TaylorGy | 0:c809010834e2 | 86 | extern cJSON *cJSON_CreateTrue(void); |
TaylorGy | 0:c809010834e2 | 87 | extern cJSON *cJSON_CreateFalse(void); |
TaylorGy | 0:c809010834e2 | 88 | extern cJSON *cJSON_CreateBool(int b); |
TaylorGy | 0:c809010834e2 | 89 | extern cJSON *cJSON_CreateNumber(double num); |
TaylorGy | 0:c809010834e2 | 90 | extern cJSON *cJSON_CreateString(const char *string); |
TaylorGy | 0:c809010834e2 | 91 | extern cJSON *cJSON_CreateArray(void); |
TaylorGy | 0:c809010834e2 | 92 | extern cJSON *cJSON_CreateObject(void); |
TaylorGy | 0:c809010834e2 | 93 | |
TaylorGy | 0:c809010834e2 | 94 | /* These utilities create an Array of count items. */ |
TaylorGy | 0:c809010834e2 | 95 | extern cJSON *cJSON_CreateIntArray(const int *numbers,int count); |
TaylorGy | 0:c809010834e2 | 96 | extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count); |
TaylorGy | 0:c809010834e2 | 97 | extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count); |
TaylorGy | 0:c809010834e2 | 98 | extern cJSON *cJSON_CreateStringArray(const char **strings,int count); |
TaylorGy | 0:c809010834e2 | 99 | |
TaylorGy | 0:c809010834e2 | 100 | /* Append item to the specified array/object. */ |
TaylorGy | 0:c809010834e2 | 101 | extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); |
TaylorGy | 0:c809010834e2 | 102 | extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item); |
TaylorGy | 0:c809010834e2 | 103 | /* 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. */ |
TaylorGy | 0:c809010834e2 | 104 | extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); |
TaylorGy | 0:c809010834e2 | 105 | extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item); |
TaylorGy | 0:c809010834e2 | 106 | |
TaylorGy | 0:c809010834e2 | 107 | /* Remove/Detatch items from Arrays/Objects. */ |
TaylorGy | 0:c809010834e2 | 108 | extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which); |
TaylorGy | 0:c809010834e2 | 109 | extern void cJSON_DeleteItemFromArray(cJSON *array,int which); |
TaylorGy | 0:c809010834e2 | 110 | extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string); |
TaylorGy | 0:c809010834e2 | 111 | extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string); |
TaylorGy | 0:c809010834e2 | 112 | |
TaylorGy | 0:c809010834e2 | 113 | /* Update array items. */ |
TaylorGy | 0:c809010834e2 | 114 | extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem); |
TaylorGy | 0:c809010834e2 | 115 | extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); |
TaylorGy | 0:c809010834e2 | 116 | |
TaylorGy | 0:c809010834e2 | 117 | /* Duplicate a cJSON item */ |
TaylorGy | 0:c809010834e2 | 118 | extern cJSON *cJSON_Duplicate(cJSON *item,int recurse); |
TaylorGy | 0:c809010834e2 | 119 | /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will |
TaylorGy | 0:c809010834e2 | 120 | need to be released. With recurse!=0, it will duplicate any children connected to the item. |
TaylorGy | 0:c809010834e2 | 121 | The item->next and ->prev pointers are always zero on return from Duplicate. */ |
TaylorGy | 0:c809010834e2 | 122 | |
TaylorGy | 0:c809010834e2 | 123 | /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ |
TaylorGy | 0:c809010834e2 | 124 | extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated); |
TaylorGy | 0:c809010834e2 | 125 | |
TaylorGy | 0:c809010834e2 | 126 | extern void cJSON_Minify(char *json); |
TaylorGy | 0:c809010834e2 | 127 | |
TaylorGy | 0:c809010834e2 | 128 | /* Macros for creating things quickly. */ |
TaylorGy | 0:c809010834e2 | 129 | #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) |
TaylorGy | 0:c809010834e2 | 130 | #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) |
TaylorGy | 0:c809010834e2 | 131 | #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) |
TaylorGy | 0:c809010834e2 | 132 | #define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) |
TaylorGy | 0:c809010834e2 | 133 | #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) |
TaylorGy | 0:c809010834e2 | 134 | #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) |
TaylorGy | 0:c809010834e2 | 135 | |
TaylorGy | 0:c809010834e2 | 136 | /* When assigning an integer value, it needs to be propagated to valuedouble too. */ |
TaylorGy | 0:c809010834e2 | 137 | #define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) |
TaylorGy | 0:c809010834e2 | 138 | |
TaylorGy | 0:c809010834e2 | 139 | #ifdef __cplusplus |
TaylorGy | 0:c809010834e2 | 140 | } |
TaylorGy | 0:c809010834e2 | 141 | #endif |
TaylorGy | 0:c809010834e2 | 142 | |
TaylorGy | 0:c809010834e2 | 143 | #endif |