为了更方便使用C的JSON库,对其进行了一层封装。
H文件:
1 #ifndef __JSONHELPER__ 2 #define __JSONHELPER__ 3 4 #ifdef __cplusplus 5 extern "C" 6 { 7 #endif 8 9 10 #define JSON_NAME_MAX_SIZE 128 11 12 enum 13 { 14 JSON_OK = 0, 15 JSON_ERROR, 16 JSON_ERR_NO_SUCH_NODE, 17 JSON_ERR_INVALID_NODE, 18 JSON_ERR_NO_SUCH_ARRAY, 19 JSON_ERR_INVALID_ARRAY_INDEX, 20 JSON_ERR_UNMATCH_TYPE 21 }; 22 23 #define JSON_ERROR_STRING "Okay " 24 "Error " 25 "No Such Node " 26 "Invalid Node " 27 "No Such Array " 28 "Invalid Array Index " 29 "Unmatch Type " 30 31 32 const char *jsonNodeType(void *json); 33 const char *jsonErrorString(int ret); 34 35 void *jsonParse(const char *text); 36 void jsonDelete(void *json); 37 void jsonDumpError(const char *text); 38 void jsonDump(void *json); 39 40 char *jsonToString(void *json, int format); 41 char *jsonToBuffer(void *json, int format, char *buffer, int len); 42 43 int jsonTestNumber(void *json); 44 int jsonTestString(void *json); 45 int jsonTestArray(void *json); 46 int jsonTestObject(void *json); 47 int jsonTestBool(void *json); 48 49 void *jsonGetNode(void *json, const char *node, int *ret); 50 int jsonGetString(void *json, const char *node, char *buf, int len); 51 int jsonGetInt(void *json, const char *node, int *value); 52 int jsonGetBool(void *json, const char *node, int *value); 53 int jsonGetUint64(void *json, const char *node, unsigned long long *value); 54 int jsonGetArraySize(void *json); 55 void *jsonGetArrayItem(void *json, int item); 56 int *jsonGetIntArray(void *json, const char *node, int *size); 57 58 int jsonMatchString(void *json, const char *node, char *value); 59 int jsonMatchInt(void *json, const char *node, int value); 60 int jsonMatchBool(void *json, const char *node, int value); 61 int jsonMatchUint64(void *json, const char *node, unsigned long long value); 62 63 int jsonSetString(void *json, const char *node, const char *str); 64 int jsonSetInt(void *json, const char *node, int value); 65 int jsonSetBool(void *json, const char *node, int value); 66 int jsonSetUint64(void *json, const char *node, unsigned long long value); 67 68 void *jsonDuplicate(void *json, int withChild); 69 70 #define jsonNewRoot() jsonNewOject(NULL, NULL) 71 void *jsonNewOject(void *json, const char *name); 72 void *jsonNewArray(void *json, const char *name); 73 void *jsonArrayAddItem(void *array); 74 75 void *jsonAttach(void *json, const char *name, void *obj); 76 77 void *jsonAddBool(void *json, const char *name, int b); 78 void *jsonAddInt(void *json, const char *name, int v); 79 void *jsonAddDouble(void *json, const char *name, double v); 80 void *jsonAddUint64(void *json, const char *name, unsigned long long v); 81 void *jsonAddString(void *json, const char *name, const char *str); 82 void *jsonAddIntArray(void *json, const char *name, const int *numbers,int count); 83 void *jsonAddStringArray(void *json, const char *name, const char **str, int count); 84 85 #ifdef __cplusplus 86 } 87 #endif 88 89 #endif /* __JSONHELPER__ */
C文件:
1 /* 2 @File: jsonHelper.c 3 @Description: json helper functions 4 @Date: 2018-04-27 5 @Author: Liu Chuansen (179712066@qq.com) 6 7 */ 8 9 #include <stdio.h> 10 #include <string.h> 11 #include <stdlib.h> 12 13 #include "jsonHelper.h" 14 #include "cJSON.h" 15 16 static const char *cJsonType(int type) 17 { 18 const char *p = "False True NULL Number String Array Object "; 19 int i = 0; 20 21 if (type == 0) 22 { 23 return p; 24 } 25 26 while(*p) 27 { 28 p ++; 29 if (*p == '