說明:
本文章旨在總結備份、友善以後查詢,由于是個人總結,如有不對,歡迎指正;另外,内容大部分來自網絡、書籍、和各類手冊,如若侵權請告知,馬上删帖緻歉。
QQ 群 号:513683159 【互相學習】
内容來源:
cjson庫的使用以及源碼閱讀
函數介紹
基本功能函數
cJSON_Version()
函數原型:
CJSON_PUBLIC(const char*) cJSON_Version(void);
函數功能: 以字元串的形式傳回cJSON版本号。
參數介紹: 無。
cJSON_InitHooks()
函數原型:
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
函數功能: 為cJSON提供malloc, realloc和free函數
參數介紹:
cJSON_Hooks結構體中有兩個函數指針。
cJSON_Delete()
函數原型:
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
函數功能: 删除一個cJSON實體和所有子實體
cJSON_HasObjectItem()
函數原型:
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
函數功能:
creat 相關
原始json
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); //數組(等于建立了一個空的 [ ])
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); //對象(等于建立了一個空的 { })
調用建立相應類型的cJSON項
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
建立計數項數組(Create XXX Array)
這些實用程式建立一個計數項數組。參數計數不能大于數字數組中的元素數,否則數組通路将超出邊界。
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
Reference
建立一個valuestring引用字元串的字元串,這樣它将不會被cJSON_Delete釋放
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
建立一個隻引用它的元素的對象/數組,這樣它們就不會被cJSON_Delete釋放
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
add 相關
将item添加到指定的數組/對象(AddItemTo XXX)
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
當string明确是const(即字面量,或其他類似的常量),并且肯定會在cJSON對象中儲存下來時使用這個。
WARNING:當使用此函數時,請確定在寫入’ tem->string '之前始終檢查(item->type & cJSON_StringIsConst)是否為零。
将對item的引用附加到指定的數組/對象。當您想要将現有的cJSON添加到新的cJSON中,但又不想破壞現有的cJSON時,請使用此方法。
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
用于同時建立和添加項到對象的輔助函數。如果失敗,則傳回添加的項或NULL。
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
Parse 相關
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
cJSON_Parse()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
函數功能: 記憶體管理:調用者總是負責釋放cJSON_Parse(使用cJSON_Delete)和cJSON_Print(使用stdlib free、cJSON_Hooks.free_fn或cJSON_free)的所有變體的結果。異常是cJSON_PrintPreallocated,其中調用者對緩沖區負全部責任。
提供一個JSON塊,這将傳回一個可以查詢的cJSON對象。
cJSON_ParseWithLength ()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
函數功能:
cJSON_ParseWithOpts ()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
函數功能: ParseWithOpts允許您要求(和檢查)JSON是空終止的,并檢索到解析的最後一個位元組的指針。
如果在return_parse_end中提供一個ptr,但解析失敗,那麼return_parse_end将包含一個指向 錯誤的指針,是以将比對cJSON_GetErrorPtr()。
cJSON_ParseWithLengthOpts ()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
函數功能:
print 相關
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
cJSON_Print ()
函數原型:
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
函數功能: 将cJSON實體渲染為文本以進行傳輸/存儲。
(将 cJSON資料解析成 JSON字元串,輸出帶有空白字元,呈現為标準的 JSON格式)
cJSON_PrintUnformatted ()
函數原型:
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
函數功能: 将cJSON實體渲染為文本以進行傳輸/存儲,而不需要任何格式化
(将 cJSON資料解析成 JSON字元串,輸出沒有帶有空白字元的 JSON格式)
cJSON_PrintBuffered ()
函數原型:
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
函數功能: 使用緩沖政策将cJSON實體渲染為文本。預緩沖是對最終大小的猜測。猜測正确可以減少重新配置設定。Fmt =0表示未格式化,=1表示格式化。
(prebuffer:指定輸出 buffer的初始大小,fmt:是否使用空白字元進行格式化)
cJSON_PrintPreallocated()
函數原型:
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
函數功能: 使用已經在記憶體中配置設定的具有給定長度的緩沖區将cJSON實體渲染為文本。成功傳回1,失敗傳回0。
(輸出 JSON到靜态 buffer,進而避免動态配置設定記憶體,當buffer大小不夠時,調用失敗,傳回 0,成功時傳回 1)
注意: cJSON在估計它将使用多少記憶體時并不總是100%準确的,是以為了安全起見,比實際需要多配置設定5個位元組
get 相關
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
cJSON_GetArraySize()
函數原型:
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
函數功能: 傳回數組(或對象)中的項數。
cJSON_GetArrayItem()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
函數功能: 從數組“array”中檢索項目編号“index”。如果不成功則傳回NULL。
cJSON_GetObjectItem()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
函數功能: 從object中擷取item “string”。不分大小寫
cJSON_GetObjectItemCaseSensitive()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
函數功能:
cJSON_GetErrorPtr()
函數原型:
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
函數功能: 用于分析失敗的解析。這将傳回一個指向解析錯誤的指針。您可能需要往回看一些字元來了解它。在cJSON_Parse()傳回0時定義。cJSON_Parse()成功時,傳回0。
檢查項類型并傳回其值
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
<font color="blue" size="6">**Is 相關**</font>
### 檢查項目的類型
```c
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
Detach 相關
從數組/對象中移除/分離項
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
宏
當配置設定一個整數值時,它也需要傳播給valuedouble。
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
宏cJSON_SetNumberValue的幫助器
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
修改cJSON_String對象的值字元串,僅當“對象類型”為cJSON_String時生效
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
宏,用于在數組或對象上疊代
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
更新數組項。
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
使用使用cJSON_InitHooks設定的malloc/free函數的malloc/free對象
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
CJSON_PUBLIC(void) cJSON_free(void *object);
cJSON_Duplicate ()
函數原型:
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
函數功能: Duplicate将在需要釋放的新記憶體中建立一個新的、相同的cJSON項。遞歸!=0時,它将複制連接配接到該項的任何子項。item->next和>prev指針在從Duplicate傳回時始終為零。
cJSON_Compare()
函數原型:
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
函數功能: 遞歸地比較兩個cJSON項是否相等。如果a或b是NULL或無效,它們将被認為是不相等的。Case_sensitive确定對象鍵是區分大小寫(1)還是不區分大小寫(0)
cJSON_Minify ()
函數原型:
CJSON_PUBLIC(void) cJSON_Minify(char *json);
函數功能: 最小化字元串,從字元串中删除空白字元(如’ ‘,’\t’, ‘\r’, ‘\n’)。輸入指針json不能指向隻讀的位址區域,比如字元串常量,但應該指向可讀和可寫的位址區域。