cJSON:
/* cJSON Types: */
/* The cJSON structure: */
typedef struct cJSON
{
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *next;
struct cJSON *prev;
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
struct cJSON *child;
/* The type of the item, as above. */
int type;
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
/* The item's number, if type==cJSON_Number */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
} cJSON;
typedef struct cJSON_Hooks
{
void *
主要函数API:
CJSON_PUBLIC(const char*) cJSON_Version(void);
printf("cJSON Version:%s\n" ,cJSON_Version());
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
int fd = -1;
int rlen = 0;
int file_size = 0;
char *buf = NULL;
struct stat stbuf;
cJSON *root = NULL;
if (access("data.db", F_OK) == -1)
{
}
fd = open("data.db", O_RDONLY, 0666);
if (fd < 0) {
}
if((fstat(fd, &stbuf)) != 0) {
}
file_size = stbuf.st_size;
printf("FILE SIZE :%d\n", file_size);
buf = (char*)malloc(file_size+1);
if(buf == NULL) {
}
rlen = read(fd, buf, file_size);
if(rlen < 0)
{
}
close(fd);
root = cJSON_Parse(buf);
if (root == NULL) {
}
...
if (root)
cJSON_Delete(root);
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
char *data = NULL;
data = cJSON_Print(root);
printf("data:%s\n", cJSON_Print(root));
{
"name":"phenecy",
"age":26,
"weight":15.21
}
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
char *data = NULL;
data = cJSON_PrintUnformatted(root);
printf("data:%s\n", cJSON_PrintUnformatted(root));
{"name":"phenecy","age":26,"weight":15.21}
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
if(root)
cJSON_Delete(root);
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
int size = 0;
size = cJSON_GetArraySize(array);
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item);
cJSON *item;
if (cJSON_IsArray(array))
item = cJSON_GetArrayItem(array, 0);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string);
cJSON *item;
item = cJSON_GetObjectItem(root, "name");
if (item != NULL)
{
}
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON *object, const char *string);
cJSON *item;
item = cJSON_GetObjectItem(root, "name");
if (item != NULL)
{
}
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
if (cJSON_HasObjectItem(root, "name")) {
}
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
printf("cJSON Error:%s\n", cJSON_GetErrorPtr());
判断item的cJSON类型函数:
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);
if (cJSON_IsInvalid(item)) {
}
if (cJSON_IsFalse(item)) {
}
其他类型判断用法相似
创建指定cJSON数据类型item:
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);
用例:
cJSON *item = NULL;
item = cJSON_CreateNull();
item = cJSON_CreateTrue();
item = cJSON_CreateFalse();
item = cJSON_CreateBool(1);
item = cJSON_CreateNumber(18);
item = cJSON_CreateNumber(3.1415926);
item = cJSON_CreateString("hello world");
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
用例:
cJSON *item = NULL;
item = cJSON_CreateRaw("Hi this is json raw data");
item = cJSON_CreateArray();
item = cJSON_CreateObject();
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 **strings, int count);
用例:
int intarray[] = {1, 2, 3, 4, 5, 6, 7 ,8, 9, 0};
cJSON *array;
array = cJSON_CreateIntArray(intarray, sizeof(intarray)/sizeof(int));
array = cJSON_CreateIntArray(intarray, sizeof(intarray)/sizeof(5));
其他几个类型方法相似
添加元素操作:
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
cJSON *array = NULL;
cJSON *item = NULL;
array = cJSON_CreateArray();
if (array == NULL)
{
}
cJSON_AddItemToArray(array, cJSON_CreateString("Android"));
cJSON_AddItemToArray(array, cJSON_CreateString("C++"));
cJSON_AddItemToArray(array, cJSON_CreateNumber(25));
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
cJSON_AddItemToObject(tree, "name", cJSON_CreateString("henoey"));
cJSON_AddItemToObject(tree, "age", cJSON_CreateNumber(52));
cJSON_AddItemToObject(tree, "weight", cJSON_CreateNumber(45.3245500));
cJSON_AddItemToObject(tree, "m", cJSON_CreateBool(1));
cJSON_AddItemToObject(tree, "s", cJSON_CreateBool(0));
cJSON_AddItemToObject(tree, "one", cJSON_CreateFalse());
cJSON_AddItemToObject(tree, "tow", cJSON_CreateFalse());
cJSON_AddItemToObject(tree, "w", cJSON_CreateNull());
cJSON_AddItemToObject(tree, "y", cJSON_CreateRaw("123456789ljadjfkwoqwe"));
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
cJSON *item = NULL;
item = cJSON_DetachItemFromArray(array, 2);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
cJSON_DeleteItemFromArray(array, 2);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
cJSON *item;
item = cJSON_DetachItemFromObject(root, "name");
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
cJSON_DeleteItemFromObject(root, "name");
更新操作:
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem);
cJSON_InsertItemInArray(array, 3, cJSON_CreateString("Android"));
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
cJSON_ReplaceItemInArray(array, 1 cJSON_CreateNumber(12));
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
cJSON_ReplaceItemInObject(root, "newname", cJSON_CreateString("HeHe"));
/* Duplicate a cJSON item */
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error. If not, then cJSON_GetErrorPtr() does the job. */
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
CJSON_PUBLIC(void) cJSON_Minify(char *json);
cJSON_Minify(root);
示例摘自:
https:
CJSON和HPack。
CJSON压缩示例
原始JSON:
[
{
"x": 100,
"y": 100
}, {
"x": 100,
"y": 100,
"width": 200,
"height": 150
},
{},
]
CJSON压缩后:
{
"templates": [
[0, "x", "y"], [1, "width", "height"]
],
"values": [
{ "values": [ 1, 100, 100 ] },
{ "values": [2, 100, 100, 200, 150 ] },
{}
]
}
HPack压缩示例
原始JSON:
[{
name : "Andrea",
age : 31,
gender : "Male",
skilled : true
}, {
name : "Eva",
age : 27,
gender : "Female",
skilled : true
}, {
name : "Daniele",
age : 26,
gender : "Male",
skilled : false
}]
HPack压缩后:
[["name","age","gender","skilled"],["Andrea",31,"Male",true],["Eva",27,"Female",true],["Daniele",26,"Male",false]]
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s))
cJSON_AddStringToObject(item, "name", "phenecy");
cJSON_AddNumberToObject(item, "age", 26);
cJSON_AddNumberToObject(item, "weight", 15.21);
cJSON_AddBoolToObject(item, "mared", false);
cJSON_AddBoolToObject(item, "student", true);
cJSON_AddFalseToObject(item, "fale");
cJSON_AddTrueToObject(item, "tu");
cJSON_AddNullToObject(item, "nu");
cJSON_AddRawToObject(item ,"rea", "hello world, hi loge");
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
cJSON_ArrayForEach(ff, array) {
if (ff != NULL) {
if (cJSON_IsNull(ff)) {
} else if (cJSON_IsNumber(ff)) {
printf("is numer\n");
printf("%d\n", ff->valueint);
} else if (cJSON_IsString(ff)) {
printf("is string\n");
printf("%s\n", ff->valuestring);
} else if (cJSON_IsObject) {
printf("is object\n");
}
}
}