├── example.c ├── LICENSE ├── md5.h ├── jsonDataStruct.h ├── README.md ├── md5.c ├── jsonLib.h └── jsonLib.c /example.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MaskingM/JsonLib/HEAD/example.c -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 洪亮 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /md5.h: -------------------------------------------------------------------------------- 1 | #ifndef MD5_H 2 | #define MD5_H 3 | 4 | typedef struct 5 | { 6 | unsigned int count[2]; 7 | unsigned int state[4]; 8 | unsigned char buffer[64]; 9 | }MD5_CTX; 10 | 11 | 12 | #define F(x,y,z) ((x & y) | (~x & z)) 13 | #define G(x,y,z) ((x & z) | (y & ~z)) 14 | #define H(x,y,z) (x^y^z) 15 | #define I(x,y,z) (y ^ (x | ~z)) 16 | #define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n))) 17 | #define FF(a,b,c,d,x,s,ac) \ 18 | { \ 19 | a += F(b,c,d) + x + ac; \ 20 | a = ROTATE_LEFT(a,s); \ 21 | a += b; \ 22 | } 23 | #define GG(a,b,c,d,x,s,ac) \ 24 | { \ 25 | a += G(b,c,d) + x + ac; \ 26 | a = ROTATE_LEFT(a,s); \ 27 | a += b; \ 28 | } 29 | #define HH(a,b,c,d,x,s,ac) \ 30 | { \ 31 | a += H(b,c,d) + x + ac; \ 32 | a = ROTATE_LEFT(a,s); \ 33 | a += b; \ 34 | } 35 | #define II(a,b,c,d,x,s,ac) \ 36 | { \ 37 | a += I(b,c,d) + x + ac; \ 38 | a = ROTATE_LEFT(a,s); \ 39 | a += b; \ 40 | } 41 | void MD5Init(MD5_CTX *context); 42 | void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen); 43 | void MD5Final(MD5_CTX *context, unsigned char digest[16]); 44 | void MD5Transform(unsigned int state[4], unsigned char block[64]); 45 | void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len); 46 | void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len); 47 | 48 | #endif 49 | 50 | 51 | -------------------------------------------------------------------------------- /jsonDataStruct.h: -------------------------------------------------------------------------------- 1 | #ifndef __JSON_DATA_STRUCT 2 | #define __JSON_DATA_STRUCT 3 | 4 | #define HASH_MAX 64 // hash table 大小 5 | #define ARRAY_MIN_SIZE 32 // 数组节点最小值,如果追加元素的时候超出范围,则继续分配内存 6 | #define TMP_BUF_SIZE 32 // 临时缓冲区大小,用于存放临时数据,避免频繁开辟小内存 7 | 8 | #define BFALSE 0 9 | #define BTRUE 1 10 | typedef int BOOLEAN; 11 | #define null NULL 12 | 13 | 14 | // 定义无符号64位长整型 15 | typedef long long unsigned int UINT64; 16 | 17 | 18 | // JSON 的基本数据类型 19 | typedef enum { 20 | JSONTYPEUNDEFINED = 0, 21 | JSONTYPENUMBER = 1, 22 | JSONTYPELONGNUMBER = 2, 23 | JSONTYPESTRING, 24 | JSONTYPEARRAY, 25 | JSONTYPEOBJECT, 26 | JSONTYPEBOOLEAN, 27 | JSONTYPENULL 28 | } JSON_DATA_TYPE; 29 | 30 | // JSON 基本节点,如果 hash 冲突,则通过则通过链表方式追加属性 31 | typedef struct __json_data_node{ 32 | JSON_DATA_TYPE type; // JSON 节点的数据类型 33 | char* keyName; 34 | struct __json_data_node* next; 35 | } JsonNode, *pJsonNode; 36 | 37 | // JSON Number 数据 38 | typedef struct { 39 | JsonNode node; 40 | double value; 41 | } JsonNumberNode, *pJsonNumberNode; 42 | 43 | // JSON Long Number 数据 44 | typedef struct { 45 | JsonNode node; 46 | UINT64 value; 47 | } JsonLongNumberNode, *pJsonLongNumberNode; 48 | 49 | // JSON String 数据 50 | typedef struct { 51 | JsonNode node; 52 | char* value; 53 | } JsonStringNode, *pJsonStringNode; 54 | 55 | // JSON Arrary 数据 56 | typedef struct { 57 | JsonNode node; 58 | // 数组类型,需要存放数组大小,数组指针,以及存放的数据类型 59 | int size; 60 | JSON_DATA_TYPE type; 61 | pJsonNode *array; 62 | } JsonArrayNode, *pJsonArrayNode; 63 | 64 | // JSON Object 数据 65 | typedef struct { 66 | JsonNode node; 67 | pJsonNode table[HASH_MAX]; 68 | } JsonObjectNode, *pJsonObjectNode; 69 | 70 | // JSON null 数据 71 | typedef struct { 72 | JsonNode node; 73 | int value; 74 | } JsonNullNode, *pJsonNullNode; 75 | 76 | // JSON Boolean 数据 77 | typedef struct { 78 | JsonNode node; 79 | BOOLEAN value; 80 | } JsonBooleanNode, *pJsonBooleanNode; 81 | 82 | #endif 83 | 84 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JsonLib 2 | 纯C语言编写的json库,可以进行json序列化和反序列化 3 | 4 | 该库主要用于 **json** 字符串的反序列化,以及将 **json** 对象进行序列化。 5 | **json** 包含的数据类型可以分为: 6 | **Object** 7 | **Array** 8 | **Number** 9 | **String** 10 | **null** 11 | **Boolean** 12 | 通过修改 **hash** 表,及数组最小尺寸大小,可以适用于嵌入式设备进行 **json** 处理。例如: 13 | #define HASH_MAX 32 14 | #define ARRAY_MIN_SIZE 32 15 | 16 | **主要功能函数介绍:** 17 | **1.JSONCreate**:创建一个 **json** 对象,函数的返回值是一个 **json** 对象指针; 18 | **2.JSONStringify**:将 **json** 对象序列化为 **json** 字符串; 19 | **3.JSONParse**:将 **json** 字符串反序列化成 **json** 对象; 20 | **4.JSONPrint**:以结构化的方式进行打印 **json** 对象,方便查看; 21 | **5.JSONDestroy**:销毁 **json** 对象,进行内存回收; 22 | **6.JSONIsContainsAttr**:检测 **json**对象是否包含某个属性; 23 | **7.JSONSetNumberAttr**:为 **json** 对象添加一个数字属性节点,如果该属性节点已存在,则进行设置成数字类型; 24 | **8.JSONSetStringAttr**:为 **json** 对象添加一个字符串属性节点,如果该属性节点已存在,则进行设置成字符串类型; 25 | **9.JSONSetEmptyArrayAttr**:为 **json** 对象添加一个空数组属性节点,如果该属性节点已存在,则进行设置成数组类型; 26 | **10.JSONSetArrayAttr**:为 **json** 对象添加一个数组属性节点,如果该属性节点已存在,则进行设置成数组类型; 27 | **11.JSONSetObjectAttr**:为 **json** 对象添加一个数字属性节点,如果该属性节点已存在,则进行设置成数字类型; 28 | **12.JSONSetNullAttr**:为 **json** 对象添加一个null属性节点,如果该属性节点已存在,则进行设置成null类型; 29 | **13.JSONSetBooleanAttr**:为 **json** 对象添加一个布尔属性节点,如果该属性节点已存在,则进行设置成布尔类型; 30 | **13.JSONRemoveAttr**:将 **json** 对象某个属性进行移除; 31 | **14.JSONObjectDeepClone**:对 **json** 对象进行一次深拷贝; 32 | **15.JSONArrayDeepClone**:对 **json** 数组进行一次深拷贝; 33 | **16.JSONObjectGetAttrType**:获取 **json** 对象某个属性的数据类型; 34 | **17.JSONNodeGetType**:获取 **json** 节点的数值类型; 35 | **18.JSONNodeGetNumberValue**:从 **json** 数据节点中取出具体的数值; 36 | **19.JSONNodeGetStringValue**:从 **json** 字符串节点中取出具体的字符串; 37 | **20.JSONNodeGetBooleanValue**:从 **json** 布尔节点中取出具体的布尔值; 38 | **21.JSONObjectGetNumberAttr**:从 **json** 对象中取出数字属性节点具体的数值; 39 | **22.JSONObjectGetStringAttr**:从 **json** 对象中取出数字属性节点具体的字符串; 40 | **23.JSONObjectGetArrayAttr**:从 **json** 对象中取出某个数组属性节点; 41 | **24.JSONObjectGetObjectAttr**:从 **json** 对象中取出某个对象属性节点; 42 | **25.JSONObjectGetNullAttr**:从 **json** 对象中取出某个null属性节点; 43 | **26.JSONObjectGetBooleanAttr**:从 **json** 对象中取出某个boolean属性节点的布尔值; 44 | **27.JSONArrayPushNumber**:向 **json** 数组中尾部添加数字元素; 45 | **28.JSONArrayPushString**:向 **json** 数组中尾部添加字符串元素; 46 | **29.JSONArrayPushArray**:向 **json** 数组中尾部添加数组元素; 47 | **30.JSONArrayPushObject**:向 **json** 数组中尾部添加对象元素; 48 | **31.JSONArrayPushNull**:向 **json** 数组中尾部添加null元素; 49 | **32.JSONArrayPushBoolean**:向 **json** 数组中尾部添加Boolean元素; 50 | **33.JSONArrayUnshiftNumber**:向 **json** 数组中头部添加数字元素; 51 | **34.JSONArrayUnshiftString**:向 **json** 数组中头部添加字符串元素; 52 | **35.JSONArrayUnshiftArray**:向 **json** 数组中头部添加数组元素; 53 | **36.JSONArrayUnshiftObject**:向 **json** 数组中头部添加对象元素; 54 | **37.JSONArrayUnshiftNull**:向 **json** 数组中头部添加null元素; 55 | **38.JSONArrayUnshiftBoolean**:向 **json** 数组中头部添加Boolean元素; 56 | **39.JSONArrayGetLength**:获取 **json** 数组长度; 57 | **40.JSONArrayGetNode**:获取 **json** 数组长某个元素; 58 | **41.JSONArrayPop**:移除 **json** 数组尾部元素; 59 | **42.JSONArrayShift**:移除 **json** 数组头部元素; 60 | -------------------------------------------------------------------------------- /md5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "md5.h" 3 | 4 | unsigned char PADDING[] = { 0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 5 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 6 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 7 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; 8 | 9 | void MD5Init(MD5_CTX *context) 10 | { 11 | context->count[0] = 0; 12 | context->count[1] = 0; 13 | context->state[0] = 0x67452301; 14 | context->state[1] = 0xEFCDAB89; 15 | context->state[2] = 0x98BADCFE; 16 | context->state[3] = 0x10325476; 17 | } 18 | void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen) 19 | { 20 | unsigned int i = 0, index = 0, partlen = 0; 21 | index = (context->count[0] >> 3) & 0x3F; 22 | partlen = 64 - index; 23 | context->count[0] += inputlen << 3; 24 | if (context->count[0] < (inputlen << 3)) 25 | context->count[1]++; 26 | context->count[1] += inputlen >> 29; 27 | 28 | if (inputlen >= partlen) 29 | { 30 | memcpy(&context->buffer[index], input, partlen); 31 | MD5Transform(context->state, context->buffer); 32 | for (i = partlen; i + 64 <= inputlen; i += 64) 33 | MD5Transform(context->state, &input[i]); 34 | index = 0; 35 | } 36 | else 37 | { 38 | i = 0; 39 | } 40 | memcpy(&context->buffer[index], &input[i], inputlen - i); 41 | } 42 | void MD5Final(MD5_CTX *context, unsigned char digest[16]) 43 | { 44 | unsigned int index = 0, padlen = 0; 45 | unsigned char bits[8]; 46 | index = (context->count[0] >> 3) & 0x3F; 47 | padlen = (index < 56) ? (56 - index) : (120 - index); 48 | MD5Encode(bits, context->count, 8); 49 | MD5Update(context, PADDING, padlen); 50 | MD5Update(context, bits, 8); 51 | MD5Encode(digest, context->state, 16); 52 | } 53 | void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len) 54 | { 55 | unsigned int i = 0, j = 0; 56 | while (j < len) 57 | { 58 | output[j] = input[i] & 0xFF; 59 | output[j + 1] = (input[i] >> 8) & 0xFF; 60 | output[j + 2] = (input[i] >> 16) & 0xFF; 61 | output[j + 3] = (input[i] >> 24) & 0xFF; 62 | i++; 63 | j += 4; 64 | } 65 | } 66 | void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len) 67 | { 68 | unsigned int i = 0, j = 0; 69 | while (j < len) 70 | { 71 | output[i] = (input[j]) | 72 | (input[j + 1] << 8) | 73 | (input[j + 2] << 16) | 74 | (input[j + 3] << 24); 75 | i++; 76 | j += 4; 77 | } 78 | } 79 | void MD5Transform(unsigned int state[4], unsigned char block[64]) 80 | { 81 | unsigned int a = state[0]; 82 | unsigned int b = state[1]; 83 | unsigned int c = state[2]; 84 | unsigned int d = state[3]; 85 | unsigned int x[64]; 86 | MD5Decode(x, block, 64); 87 | FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */ 88 | FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */ 89 | FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */ 90 | FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */ 91 | FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */ 92 | FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */ 93 | FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */ 94 | FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */ 95 | FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */ 96 | FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */ 97 | FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */ 98 | FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */ 99 | FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */ 100 | FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */ 101 | FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */ 102 | FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */ 103 | 104 | /* Round 2 */ 105 | GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */ 106 | GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */ 107 | GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */ 108 | GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */ 109 | GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */ 110 | GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */ 111 | GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */ 112 | GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */ 113 | GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */ 114 | GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */ 115 | GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */ 116 | GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */ 117 | GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */ 118 | GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */ 119 | GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */ 120 | GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */ 121 | 122 | /* Round 3 */ 123 | HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */ 124 | HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */ 125 | HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */ 126 | HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */ 127 | HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */ 128 | HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */ 129 | HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */ 130 | HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */ 131 | HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */ 132 | HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */ 133 | HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */ 134 | HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */ 135 | HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */ 136 | HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */ 137 | HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */ 138 | HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */ 139 | 140 | /* Round 4 */ 141 | II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */ 142 | II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */ 143 | II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */ 144 | II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */ 145 | II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */ 146 | II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */ 147 | II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */ 148 | II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */ 149 | II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */ 150 | II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */ 151 | II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */ 152 | II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */ 153 | II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */ 154 | II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */ 155 | II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */ 156 | II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */ 157 | state[0] += a; 158 | state[1] += b; 159 | state[2] += c; 160 | state[3] += d; 161 | } 162 | -------------------------------------------------------------------------------- /jsonLib.h: -------------------------------------------------------------------------------- 1 | /** 2 | * json 数据类型可以分为 5 种基本类型: 3 | * LongNumber Number Array String Object 4 | * json 自身可以看作一个 Object 5 | * 键值对采用hashMap的方式进行存放 6 | * 通过 数组链表 方式解决 hash 冲突 7 | * 添加属性时,如果属性值已经存在,则替换 8 | */ 9 | #define _CRT_SECURE_NO_WARNINGS 10 | #ifndef __JSON_LIB__H_ 11 | #define __JSON_LIB__H_ 12 | #include 13 | #include 14 | #include 15 | #include "md5.h" 16 | #include "jsonDataStruct.h" 17 | 18 | /** 19 | * 创建 Object 空对象 20 | * @return pJsonObjectNode 21 | */ 22 | pJsonObjectNode JSONCreate(void); 23 | 24 | /* 25 | * 创建 Array 空数组 26 | * @return pJsonArrayNode 27 | */ 28 | pJsonArrayNode JSONCreateArray(void); 29 | 30 | /* 31 | * json 格式化函数,将 json 字符串转换成 json 对象 32 | * @param input json 字符串 33 | * @return pJsonObjectNode 34 | */ 35 | pJsonObjectNode JSONParse(char* input); 36 | 37 | /* 38 | * json 序列化,将 json 对象转换成 json 字符串 39 | * @param pNode pJsonNode json 节点 40 | * @param output char** 接收字符串的指针 41 | * @return void 42 | */ 43 | void JSONStringify(pJsonNode pNode /* in */, char** output /* out */); 44 | 45 | /* 46 | * 打印 json 节点 47 | * @param pNode pJsonNode json 节点 48 | * @return void 49 | */ 50 | void JSONPrint(pJsonNode pNode /* in */); 51 | 52 | /* 53 | * 销毁 json 节点 54 | * @param pObject json 节点 55 | * @return void 56 | */ 57 | void JSONDestroy(pJsonNode pNode /* in */); 58 | 59 | /* 60 | * 判断 json 是否包含属性 61 | * @param pObject json 对象 62 | * @param key 属性名称 63 | * @return BOOLEAN 是否包含属性 64 | */ 65 | BOOLEAN JSONIsContainsAttr(pJsonObjectNode pObject /* in */, char* key /* in */); 66 | 67 | /* 68 | * 向 json 对象中添加 Number类型 键值对象 69 | * @param pObject json 对象 70 | * @param key 属性名称 71 | * @param value 属性值 72 | * @return void 73 | */ 74 | void JSONSetNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, double value /* in */); 75 | 76 | /* 77 | * 向 json 对象中添加 Long Number类型 键值对象 78 | * @param pObject json 对象 79 | * @param key 属性名称 80 | * @param value 属性值 81 | * @return void 82 | */ 83 | void JSONSetLongNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, UINT64 value /* in */); 84 | 85 | /* 86 | * 向 json 对象中添加 String类型 键值对象 87 | * @param pObject json 对象 88 | * @param key 属性名称 89 | * @param value 属性值 90 | * @return void 91 | */ 92 | void JSONSetStringAttr(pJsonObjectNode pObject /* in */, char* key /* in */, char* value /* in */); 93 | 94 | /* 95 | * 向 json 对象中添加 Array类型 键值对象,添加的数组为空数组 96 | * @param pObject json 对象 97 | * @param key 属性名称 98 | * @return void 99 | */ 100 | void JSONSetEmptyArrayAttr(pJsonObjectNode pObject /* in */, char* key /* in */); 101 | 102 | /* 103 | * 向 json 对象中添加 Array类型 键值对象 104 | * @param pObject json 对象 105 | * @param key 属性名称 106 | * @param value pJsonArrayNode 数组节点 107 | * @param isDeepClone BOOLEAN 是否进行深度拷贝 108 | * @return void 109 | */ 110 | void JSONSetArrayAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonArrayNode pArray, BOOLEAN isDeepClone); 111 | 112 | /* 113 | * 向 json 对象中添加 Object类型 键值对象 114 | * @param pObject json 对象 115 | * @param key 属性名称 116 | * @param value 属性值 117 | * @param isDeepClone BOOLEAN 是否进行深度拷贝 118 | * @return void 119 | */ 120 | void JSONSetObjectAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonObjectNode value /* in */, BOOLEAN isDeepClone); 121 | 122 | /* 123 | * 向 json 对象中添加 null类型 键值对象 124 | * @param pObject json 对象 125 | * @param key 属性名称 126 | * @return void 127 | */ 128 | void JSONSetNullAttr(pJsonObjectNode pObject /* in */, char* key /* in */); 129 | 130 | /* 131 | * 向 json 对象中添加 Boolean类型 键值对象 132 | * @param pObject json 对象 133 | * @param key char* 属性名称 134 | * @param value Boolean 属性值 135 | * @return void 136 | */ 137 | void JSONSetBooleanAttr(pJsonObjectNode pObject /* in */, char* key /* in */, BOOLEAN value /* in */); 138 | 139 | /* 140 | * 移除 json 对象中的属性 141 | * @param pObject json 对象 142 | * @param key 属性名称 143 | * @return void 144 | */ 145 | void JSONRemoveAttr(pJsonObjectNode pObject /* in */, char* key /* in */); 146 | 147 | /* 148 | * json 对象深拷贝 149 | * @param pObject json对象 150 | * @return pJsonObjectNode 新创建的json对象 151 | */ 152 | pJsonObjectNode JSONObjectDeepClone(pJsonObjectNode pObject /* in */); 153 | 154 | /* 155 | * json 数组深拷贝 156 | * @param pArray json数组 157 | * @return pJsonArrayNode 新创建的json Array 158 | */ 159 | pJsonArrayNode JSONArrayDeepClone(pJsonArrayNode pArray /* in */); 160 | 161 | /* 162 | * 向 json 数组节点中数组尾部追加数字 163 | * @param pArrayNode pJsonArrayNode 数组节点 164 | * @param number double 需要添加的数字 165 | * @return void 166 | */ 167 | void JSONArrayPushNumber(pJsonArrayNode pArrayNode /* in */, double number /* in */); 168 | 169 | /* 170 | * 向 json 数组节点中数组尾部追加长数字 171 | * @param pArrayNode pJsonArrayNode 数组节点 172 | * @param number UINT64 需要添加的数字 173 | * @return void 174 | */ 175 | void JSONArrayPushLongNumber(pJsonArrayNode pArrayNode /* in */, UINT64 number /* in */); 176 | 177 | /* 178 | * 向 json 数组节点中数组尾部追加字符串 179 | * @param pArrayNode pJsonArrayNode 数组节点 180 | * @param pString char* 需要添加的字符串 181 | * @return void 182 | */ 183 | void JSONArrayPushString(pJsonArrayNode pArrayNode /* in */, char* pString /* in */); 184 | 185 | /* 186 | * 向 json 数组节点中数组尾部追加数组 187 | * @param pArrayNode pJsonArrayNode 数组节点 188 | * @param pArr pJsonArrayNode 需要添加的数组节点 189 | * @return void 190 | */ 191 | void JSONArrayPushArray(pJsonArrayNode pArrayNode /* in */, pJsonArrayNode pArr /* in */); 192 | 193 | /* 194 | * 向 json 数组节点中数组尾部追加对象 195 | * @param pArrayNode pJsonArrayNode 数组节点 196 | * @param pObject pJsonObjectNode 需要添加的对象节点 197 | * @return void 198 | */ 199 | void JSONArrayPushObject(pJsonArrayNode pArrayNode /* in */, pJsonObjectNode pObject /* in */); 200 | 201 | /* 202 | * 向 json 数组节点中数组尾部追加 null 203 | * @param pArrayNode pJsonArrayNode 数组节点 204 | * @return void 205 | */ 206 | void JSONArrayPushNull(pJsonArrayNode pArrayNode /* in */); 207 | 208 | /* 209 | * 向 json 数组节点中数组尾部追加 Boolean 210 | * @param pArrayNode pJsonArrayNode 数组节点 211 | * @param value BOOLEAN 需要添加的 Boolean 212 | * @return void 213 | */ 214 | void JSONArrayPushBoolean(pJsonArrayNode pArrayNode /* in */, BOOLEAN value /* in */); 215 | 216 | /* 217 | * 向 json 数组节点中数组头部追加数字 218 | * @param pArrayNode pJsonArrayNode 数组节点 219 | * @param number double 需要添加的数字 220 | * @return void 221 | */ 222 | void JSONArrayUnshiftNumber(pJsonArrayNode pArrayNode /* in */, double number /* in */); 223 | 224 | /* 225 | * 向 json 数组节点中数组头部追加长数字 226 | * @param pArrayNode pJsonArrayNode 数组节点 227 | * @param number UINT64 需要添加的数字 228 | * @return void 229 | */ 230 | void JSONArrayUnshiftLongNumber(pJsonArrayNode pArrayNode /* in */, UINT64 number /* in */); 231 | 232 | /* 233 | * 向 json 数组节点中数组头部追加字符串 234 | * @param pArrayNode pJsonArrayNode 数组节点 235 | * @param pString char* 需要添加的字符串 236 | * @return void 237 | */ 238 | void JSONArrayUnshiftString(pJsonArrayNode pArrayNode /* in */, char* pString /* in */); 239 | 240 | /* 241 | * 向 json 数组节点中数组头部追加数组 242 | * @param pArrayNode pJsonArrayNode 数组节点 243 | * @param pArr pJsonArrayNode 需要添加的数组 244 | * @return void 245 | */ 246 | void JSONArrayUnshiftArray(pJsonArrayNode pArrayNode /* in */, pJsonArrayNode pArr /* in */); 247 | 248 | /* 249 | * 向 json 数组节点中数组头部追加对象 250 | * @param pArrayNode pJsonArrayNode 数组节点 251 | * @param pObject pJsonObjectNode 需要添加的对象 252 | * @return void 253 | */ 254 | void JSONArrayUnshiftObject(pJsonArrayNode pArrayNode /* in */, pJsonObjectNode pObject /* in */); 255 | 256 | /* 257 | * 向 json 数组节点中数组头部追加 null 258 | * @param pArrayNode pJsonArrayNode 数组节点 259 | * @return void 260 | */ 261 | void JSONArrayUnshiftNull(pJsonArrayNode pArrayNode /* in */); 262 | 263 | /* 264 | * 向 json 数组节点中数组头部追加 Boolean 265 | * @param pArrayNode pJsonArrayNode 数组节点 266 | * @param value BOOLEAN 需要添加的 Boolean 267 | * @return void 268 | */ 269 | void JSONArrayUnshiftBoolean(pJsonArrayNode pArrayNode /* in */, BOOLEAN value /* in */); 270 | 271 | /* 272 | * 获取数组类型的数据长度 273 | * @param pArray pJsonArrayNode 数组节点 274 | * @return unsigned int 数组长度 275 | */ 276 | unsigned int JSONArrayGetLength(pJsonArrayNode pArray /* in */); 277 | 278 | /* 279 | * 获取数组指定位置元素 280 | * @param pArray pJsonArrayNode 数组节点 281 | * @param pos int 282 | * @return pJsonNode 数组元素 283 | */ 284 | pJsonNode JSONArrayGetNode(pJsonArrayNode pArray /* in */, int pos /* in */); 285 | 286 | /* 287 | * 向 json 数组节点中数组尾部删除元素,并销毁该元素 288 | * @param pArrayNode pJsonArrayNode 数组节点 289 | * @return void 290 | */ 291 | void JSONArrayPop(pJsonArrayNode pArrayNode /* in */); 292 | 293 | /* 294 | * 向 json 数组节点中数组头部删除元素,并销毁该元素 295 | * @param pArrayNode pJsonArrayNode 数组节点 296 | * @return void 297 | */ 298 | void JSONArrayShift(pJsonArrayNode pArrayNode /* in */); 299 | 300 | /* 301 | * 获取 Object 属性数据类型 302 | * @param pObject pJsonObjectNode json 对象 303 | * @param key char* 键值 304 | * @return JSON_DATA_TYPE json 节点的数据类型 305 | */ 306 | JSON_DATA_TYPE JSONObjectGetAttrType(pJsonObjectNode pObject /* in */, char* key /* in */); 307 | 308 | /* 309 | * 获取 节点 数据类型 310 | * @param pNode pJsonNode 节点 311 | * @return JSON_DATA_TYPE json 节点的数据类型 312 | */ 313 | JSON_DATA_TYPE JSONNodeGetType(pJsonNode pNode /* in */); 314 | 315 | /* 316 | * 获取数字节点的数值 317 | * @param pNode pJsonNumberNode 数字节点 318 | * @param pValue double* 319 | * @return void 320 | */ 321 | void JSONNodeGetNumberValue(pJsonNumberNode pNode /* in */, double* pValue /* out */); 322 | 323 | /* 324 | * 获取长数字节点的数值 325 | * @param pNode pJsonNumberNode 数字节点 326 | * @param pValue UINT64* 327 | * @return void 328 | */ 329 | void JSONNodeGetLongNumberValue(pJsonNumberNode pNode /* in */, UINT64* pValue /* out */); 330 | 331 | /* 332 | * 获取字符串节点的的字符串 333 | * @param pNode pJsonStringNode 字符串节点 334 | * @param ppValue char** 335 | * @return void 336 | */ 337 | void JSONNodeGetStringValue(pJsonStringNode pNode /* in */, char** ppValue /* out */); 338 | 339 | /* 340 | * 获取 Boolean 节点的值 341 | * @param pNode pJsonBooleanNode Boolean节点 342 | * @param pValue BOOLEAN* 343 | * @return void 344 | */ 345 | void JSONNodeGetBooleanValue(pJsonBooleanNode pNode /* in */, BOOLEAN* pValue /* out */); 346 | 347 | /* 348 | * 获取 json 数字属性节点的数值 349 | * @param pObject pJsonObjectNode json 对象 350 | * @param key char* 键 351 | * @param pValue double* 值 352 | * @return void 353 | */ 354 | void JSONObjectGetNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, double* pValue /* out */); 355 | 356 | /* 357 | * 获取 json 长数字属性节点的数值 358 | * @param pObject pJsonObjectNode json 对象 359 | * @param key char* 键 360 | * @param pValue UINT64* 值 361 | * @return void 362 | */ 363 | void JSONObjectGetLongNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, UINT64* pValue /* out */); 364 | 365 | /* 366 | * 获取 json 字符串属性节点的字符串 367 | * @param pObject pJsonObjectNode json 对象 368 | * @param key char* 键 369 | * @param ppValue char** 值 370 | * @return void 371 | */ 372 | void JSONObjectGetStringAttr(pJsonObjectNode pObject /* in */, char* key /* in */, char** ppValue /* out */); 373 | 374 | /* 375 | * 获取 json 数组属性节点的值 376 | * @param pObject pJsonObjectNode json 对象 377 | * @param key char* 键 378 | * @param ppValue pJsonArrayNode* 值 379 | * @return void 380 | */ 381 | void JSONObjectGetArrayAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonArrayNode* ppValue /* out */); 382 | 383 | /* 384 | * 获取 json 对象属性节点的值 385 | * @param pObject pJsonObjectNode json 对象 386 | * @param key char* 键 387 | * @param ppValue pJsonObjectNode* 值 388 | * @return void 389 | */ 390 | void JSONObjectGetObjectAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonObjectNode* ppValue /* out */); 391 | 392 | /* 393 | * 获取 json null 属性节点的值 394 | * @param pObject pJsonObjectNode json 对象 395 | * @param key char* 键 396 | * @param pValue pJsonNullNode* 397 | * @return void 398 | */ 399 | void JSONObjectGetNullAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonNullNode* ppValue /* out */); 400 | 401 | /* 402 | * 获取 json Boolean 属性节点的值 403 | * @param pObject pJsonObjectNode json 对象 404 | * @param key char* 键 405 | * @param pValue BOOLEAN* 406 | * @return void 407 | */ 408 | void JSONObjectGetBooleanAttr(pJsonObjectNode pObject /* in */, char* key /* in */, BOOLEAN* pValue /* out */); 409 | 410 | #endif 411 | -------------------------------------------------------------------------------- /jsonLib.c: -------------------------------------------------------------------------------- 1 | #include "jsonLib.h" 2 | 3 | /*定义一些不需要暴露的功能*/ 4 | /* 5 | * 计算 md5 加密后的字符串 6 | * @param input 需要加密的字符串 7 | * @param output 加密后的字符串,内存大小必须在33个字节 8 | * @return void 9 | */ 10 | void encryptionStr(char *input /* in */, char *output /* out */); 11 | 12 | /* 13 | * 根据字符串,计算hash值 14 | * @param input 需要计算的字符串 15 | * @return unsigned long int 计算结果 16 | */ 17 | unsigned long int getHashVal(char *input /* in */); 18 | 19 | /* 20 | * 创建 Number 节点 21 | * @param key char* 键名称 22 | * @param value double 键值 23 | * @return pJsonNumberNode Number节点指针 24 | */ 25 | pJsonNumberNode JSONCreateNumberNode(char* key /* in */, double value /* in */); 26 | 27 | /* 28 | * 创建 String 节点 29 | * @param key char* 键名称 30 | * @param value char* 键值 31 | * @return pJsonStringNode String节点指针 32 | */ 33 | pJsonStringNode JSONCreateStringNode(char* key /* in */, char* value /* in */); 34 | 35 | /* 36 | * 创建 Array 空数组对象 37 | * @param key char* 键名称 38 | * @return pJsonArrayNode Array节点指针 39 | */ 40 | pJsonArrayNode JSONCreateArrayNode(char* key /* in */); 41 | 42 | /* 43 | * 创建 Object 空对象 44 | * @param key char* 键名称 45 | * @return pJsonObjectNode 46 | */ 47 | pJsonObjectNode JSONCreateObjectNode(char* key /* in */); 48 | 49 | /* 50 | * 创建 null 节点 51 | * @param key char* 键名称 52 | * @return pJsonNullNode null节点指针 53 | */ 54 | pJsonNullNode JSONCreateNullNode(char* key /* in */); 55 | 56 | /* 57 | * 创建 Boolean 节点 58 | * @param key char* 键名称 59 | * @param value BOOLEAN 键值 60 | * @return pJsonBooleanNode Boolean节点指针 61 | */ 62 | pJsonBooleanNode JSONCreateBooleanNode(char* key /* in */, BOOLEAN value /* in */); 63 | 64 | /* 65 | * 打印 Number 66 | * @param pNode pJsonNumberNode 67 | * @return void 68 | */ 69 | void JSONPrintNumberNode(pJsonNumberNode pNode /* in */); 70 | 71 | /* 72 | * 打印 String 73 | * @param pNode pJsonStringNode 74 | * @return void 75 | */ 76 | void JSONPrintStringNode(pJsonStringNode pNode /* in */); 77 | 78 | /* 79 | * 打印 Array 80 | * @param pNode pJsonArrayNode 81 | * @return void 82 | */ 83 | void JSONPrintArrayNode(pJsonArrayNode pNode /* in */); 84 | 85 | /* 86 | * 打印 json 对象 87 | * @param pObject json 对象 88 | * @return void 89 | */ 90 | void JSONPrintObjectNode(pJsonObjectNode pObject /* in */); 91 | 92 | /* 93 | * 打印 null 94 | * @param pNode pJsonNullNode 95 | * @return void 96 | */ 97 | void JSONPrintNullNode(pJsonNullNode pNode /* in */); 98 | 99 | /* 100 | * 打印 Boolean 101 | * @param pNode pJsonBooleanNode 102 | * @return void 103 | */ 104 | void JSONPrintBooleanNode(pJsonBooleanNode pNode /* in */); 105 | 106 | /* 107 | * 销毁 Number 类型类型节点 108 | * @param pNode Number类型节点 109 | * @return void 110 | */ 111 | void JSONDestroyNumberNode(pJsonNumberNode pNode /* in */); 112 | 113 | /* 114 | * 销毁 Long Number 类型类型节点 115 | * @param pNode Long Number类型节点 116 | * @return void 117 | */ 118 | void JSONDestroyLongNumberNode(pJsonLongNumberNode pNode /* in */); 119 | 120 | /* 121 | * 销毁 String 类型类型节点 122 | * @param pNode String类型节点 123 | * @return void 124 | */ 125 | void JSONDestroyStringNode(pJsonStringNode pNode /* in */); 126 | 127 | /* 128 | * 销毁 Array 类型类型节点 129 | * @param pNode Array类型节点 130 | * @return void 131 | */ 132 | void JSONDestroyArrayNode(pJsonArrayNode pNode /* in */); 133 | 134 | /* 135 | * 销毁 Object 类型类型节点 136 | * @param pNode Object类型节点 137 | * @return void 138 | */ 139 | void JSONDestroyObjectNode(pJsonObjectNode pNode /* in */); 140 | 141 | /* 142 | * 销毁 null 类型类型节点 143 | * @param pNode null类型节点 144 | * @return void 145 | */ 146 | void JSONDestroyNullNode(pJsonNullNode pNode /* in */); 147 | 148 | /* 149 | * 销毁 Boolean 类型类型节点 150 | * @param pNode Boolean类型节点 151 | * @return void 152 | */ 153 | void JSONDestroyBooleanNode(pJsonBooleanNode pNode /* in */); 154 | 155 | /* 156 | * json 数字对象深拷贝 157 | * @param pNumber jsonNumber 158 | * @param isCopyKey char* 是否进行键名称拷贝 159 | * @return pJsonNumberNode 新创建的json Number 160 | */ 161 | pJsonNumberNode JSONNumberDeepClone(pJsonNumberNode pNumber /* in */, BOOLEAN isCopyKey /* in */); 162 | 163 | /* 164 | * json 字符串对象深拷贝 165 | * @param pString jsonString 166 | * @param isCopyKey char* 是否进行键名称拷贝 167 | * @return pJsonNumberNode 新创建的json Number 168 | */ 169 | pJsonStringNode JSONStringDeepClone(pJsonStringNode pString /* in */, BOOLEAN isCopyKey /* in */); 170 | 171 | /* 172 | * json null 对象深拷贝 173 | * @param pNull pJsonNullNode 174 | * @param isCopyKey char* 是否进行键名称拷贝 175 | * @return pJsonNullNode 新创建的json null 176 | */ 177 | pJsonNullNode JSONNullDeepClone(pJsonNullNode pNull /* in */, BOOLEAN isCopyKey /* in */); 178 | 179 | /* 180 | * json Boolean 对象深拷贝 181 | * @param pNull pJsonBooleanNode 182 | * @param isCopyKey char* 是否进行键名称拷贝 183 | * @return pJsonBooleanNode 新创建的json Boolean 184 | */ 185 | pJsonBooleanNode JSONBooleanDeepClone(pJsonBooleanNode pBoolean /* in */, BOOLEAN isCopyKey /* in */); 186 | 187 | /* 188 | * 向 json 数组节点中数组尾部追加元素 189 | * @param pArrayNode pJsonArrayNode 数组节点 190 | * @param pNode pJsonNode 需要添加的节点 191 | * @return void 192 | */ 193 | void JSONArrayPush(pJsonArrayNode pArrayNode /* in */, pJsonNode pNode /* in */); 194 | 195 | /* 196 | * 向 json 数组节点中数组头部追加元素 197 | * @param pArrayNode pJsonArrayNode 数组节点 198 | * @param pNode pJsonNode 需要添加的节点 199 | * @return void 200 | */ 201 | void JSONArrayUnshift(pJsonArrayNode pArrayNode /* in */, pJsonNode pNode /* in */); 202 | 203 | /* 204 | * 数字节点序列化,做数组元素,不需要键名称 205 | * @param pNode pJsonNumberNode 数字节点 206 | * @param isContainsKey BOOLEAN 是否包含键 207 | * @return char* 序列化字符串 208 | */ 209 | char* JSONStringifyNumberNode(pJsonNumberNode pNode, BOOLEAN isContainsKey); 210 | 211 | /* 212 | * 数字节点序列化,做数组元素,不需要键名称 213 | * @param pNode pJsonLongNumberNode 数字节点 214 | * @param isContainsKey BOOLEAN 是否包含键 215 | * @return char* 序列化字符串 216 | */ 217 | char* JSONStringifyLongNumberNode(pJsonLongNumberNode pNode, BOOLEAN isContainsKey); 218 | 219 | /* 220 | * 字符串节点序列化,做数组元素,不需要键名称 221 | * @param pNode pJsonStringNode 字符串节点 222 | * @param isContainsKey BOOLEAN 是否包含键 223 | * @return char* 序列化字符串 224 | */ 225 | char* JSONStringifyStringNode(pJsonStringNode pNode, BOOLEAN isContainsKey); 226 | 227 | /* 228 | * 数组节点序列化,做数组元素,不需要键名称 229 | * @param pNode pJsonArrayNode 数组节点 230 | * @param isContainsKey BOOLEAN 是否包含键 231 | * @return char* 序列化字符串 232 | */ 233 | char* JSONStringifyArrayNode(pJsonArrayNode pNode, BOOLEAN isContainsKey); 234 | 235 | /* 236 | * 对象节点序列化,做数组元素,不需要键名称 237 | * @param pNode pJsonObjectNode 数组节点 238 | * @param isContainsKey BOOLEAN 是否包含键 239 | * @return char* 序列化字符串 240 | */ 241 | char* JSONStringifyObjectNode(pJsonObjectNode pNode, BOOLEAN isContainsKey); 242 | 243 | /* 244 | * null 节点序列化,做数组元素,不需要键名称 245 | * @param pNode pJsonNullNode null节点 246 | * @param isContainsKey BOOLEAN 是否包含键 247 | * @return char* 序列化字符串 248 | */ 249 | char* JSONStringifyNullNode(pJsonNullNode pNode, BOOLEAN isContainsKey); 250 | 251 | /* 252 | * Boolean 节点序列化,做数组元素,不需要键名称 253 | * @param pNode pJsonBooleanNode Boolean节点 254 | * @param isContainsKey BOOLEAN 是否包含键 255 | * @return char* 序列化字符串 256 | */ 257 | char* JSONStringifyBooleanNode(pJsonBooleanNode pNode, BOOLEAN isContainsKey); 258 | 259 | /* 260 | * 去掉字符串中多余的逗号 261 | * @param input char* 需要去逗号的字符串 262 | * @return char* 去逗号后的字符串 263 | */ 264 | char* JSONStringRemoveComma(char* input /* in */); 265 | 266 | /* 267 | * 判断 键是否有效 268 | * @param input char* 输入的键 269 | * @param len int 长度 270 | * @return BOOLEAN 键名称是否合法 271 | */ 272 | BOOLEAN JSONKeyIsOk(char* input /* in */, int len /* in */); 273 | 274 | /* 275 | * json 字符串去空格等不可见字符 276 | * @param input char* 需要去空格的字符串 277 | * @return char* 去空格后的字符串 278 | */ 279 | char* JSONStringTrim(char* input /* in */); 280 | 281 | /* 282 | * null 类型键值对字符串格式是否合法 283 | * @param input char* 输入的字符串 284 | * @param len int 长度 285 | * @param isHasKey BOOLEAN 是否包含键 286 | * @return BOOLEAN 字符串合法 287 | */ 288 | BOOLEAN JSONStringNullIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */); 289 | 290 | /* 291 | * Boolean 类型键值对字符串格式是否合法 292 | * @param input char* 输入的字符串 293 | * @param len int 长度 294 | * @param isHasKey BOOLEAN 是否包含键 295 | * @return BOOLEAN 字符串合法 296 | */ 297 | BOOLEAN JSONStringBooleanIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */); 298 | 299 | /* 300 | * String 类型键值对字符串格式是否合法 301 | * @param input char* 输入的字符串 302 | * @param len int 长度 303 | * @param isHasKey BOOLEAN 是否包含键 304 | * @return BOOLEAN 字符串合法 305 | */ 306 | BOOLEAN JSONStringStringIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */); 307 | 308 | /* 309 | * 数字类型键值对字符串格式是否合法 310 | * @param input char* 输入的字符串 311 | * @param len int 长度 312 | * @param isHasKey BOOLEAN 是否包含键 313 | * @return BOOLEAN 字符串合法 314 | */ 315 | BOOLEAN JSONStringNumberIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */); 316 | 317 | /* 318 | * Array 类型键值对字符串格式是否合法 319 | * @param input char* 输入的字符串 320 | * @param len int 长度 321 | * @param isHasKey BOOLEAN 是否包含键 322 | * @return BOOLEAN 字符串合法 323 | */ 324 | BOOLEAN JSONStringArrayIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */); 325 | 326 | /* 327 | * Object 类型键值对字符串格式是否合法 328 | * @param input char* 输入的字符串 329 | * @param len int 长度 330 | * @param isHasKey BOOLEAN 是否包含键 331 | * @return BOOLEAN 字符串合法 332 | */ 333 | BOOLEAN JSONStringObjectIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */); 334 | 335 | /* 336 | * 判断 json 字符串是否合法 337 | * @param input char* 需要解析的字符串 338 | * @return BOOLEAN 符合格式要求,返回 BTRUE 否则返回 BFALSE 339 | */ 340 | BOOLEAN JSONStringIsFormat(char* input /* in */); 341 | 342 | /* 343 | * 字符串是否是数组 344 | * @param input char* 输入的字符串 345 | * @param len int 长度 346 | * @return BOOLEAN 字符串合法 347 | */ 348 | BOOLEAN strIsArray(char* input /* in */, int len /* in */); 349 | 350 | /* 351 | * 字符串是否是对象 352 | * @param input char* 输入的字符串 353 | * @param len int 长度 354 | * @return BOOLEAN 字符串合法 355 | */ 356 | BOOLEAN strIsObject(char* input /* in */, int len /* in */); 357 | 358 | /* 359 | * 格式化 json 数组 360 | * @param input char* 需要格式化的字符串 361 | * @return pJsonArrayNode 362 | */ 363 | pJsonArrayNode JSONParseArray(char* input /* in */); 364 | 365 | /* 366 | * 格式化 json 对象 367 | * @param input char* 需要格式化的字符串 368 | * @return pJsonObjectNode 369 | */ 370 | pJsonObjectNode JSONParseObject(char* input /* in */); 371 | 372 | ////////////////////////////////////////////////////////////////////////// 373 | ////////////////////////////////////////////////////////////////////////// 374 | ////////////////////////////////////////////////////////////////////////// 375 | // 格式化的时候,针对不同的结构进行分类格式化 376 | 377 | /* 378 | * 字符串是否是数字 379 | * @param input char* 输入的字符串 380 | * @param len int 长度 381 | * @return BOOLEAN 字符串合法 382 | */ 383 | BOOLEAN strIsNumber(char* input /* in */, int len /* in */) { 384 | BOOLEAN res = BFALSE; 385 | do { 386 | if (len <= 0 || !input) { 387 | break; 388 | } 389 | if (input[0] == '-' && len == 1) { 390 | break; 391 | } 392 | char* buf = malloc(len + 1); 393 | if (!buf) { 394 | break; 395 | } 396 | memset(buf, 0, len + 1); 397 | if (input[0] == '-') { 398 | len -= 1; 399 | memcpy(buf, input+1, len); 400 | } 401 | else { 402 | memcpy(buf, input, len); 403 | } 404 | 405 | char ch = 0; 406 | BOOLEAN badChar = BFALSE; // 不合法字符 407 | // 则此时为纯数字 408 | for (int j = 0; j < len; j++) { 409 | ch = buf[j] & 0xFF; 410 | if ((ch < '0' && (ch != '.' && ch != '+')) || (ch > '9' && (ch != 'e' && ch != 'E'))) { 411 | badChar = BTRUE; 412 | break; 413 | } 414 | } 415 | if (badChar) { 416 | free(buf); 417 | break; 418 | } 419 | char* p = NULL; 420 | // 科学记数法 421 | if ((p = strstr(buf, "e+")) != NULL || (p = strstr(buf, "E+")) != NULL) { 422 | if (strlen(p) == 2) { 423 | badChar = BTRUE; 424 | goto STR_IS_NUMBER_LABLE; 425 | } 426 | // 判断科学记数法指数是否合法 427 | if (strstr(p + 2, "e") || strstr(p + 2, "E") || strstr(p + 2, "+") || strstr(p + 2, ".")) { 428 | badChar = BTRUE; 429 | goto STR_IS_NUMBER_LABLE; 430 | } 431 | // 判断科学记数法倍数是否合法 432 | p[0] = 0; 433 | if (strstr(buf, "e") || strstr(buf, "E") || strstr(buf, "+")) { 434 | badChar = BTRUE; 435 | goto STR_IS_NUMBER_LABLE; 436 | } 437 | if (strlen(buf) == 0) { 438 | badChar = BTRUE; 439 | goto STR_IS_NUMBER_LABLE; 440 | } 441 | if ((buf[strlen(buf) - 1] == '.') || (buf[0] == '.')) { 442 | badChar = BTRUE; 443 | goto STR_IS_NUMBER_LABLE; 444 | } 445 | if ((p = strstr(buf, ".")) != NULL) { 446 | if (strstr(p + 1, ".")) { 447 | badChar = BTRUE; 448 | goto STR_IS_NUMBER_LABLE; 449 | } 450 | } 451 | if (strlen(buf) > 1) { 452 | if (buf[0] == '0' && buf[1] != '.') { 453 | badChar = BTRUE; 454 | goto STR_IS_NUMBER_LABLE; 455 | } 456 | } 457 | } 458 | else { 459 | // 十进制表示法 460 | char* p = NULL; 461 | if (strstr(buf, "e") || strstr(buf, "E") || strstr(buf, "+")) { 462 | badChar = BTRUE; 463 | goto STR_IS_NUMBER_LABLE; 464 | } 465 | if ((p = strstr(buf, ".")) != NULL) { 466 | if (strstr(p + 1, ".")) { 467 | badChar = BTRUE; 468 | goto STR_IS_NUMBER_LABLE; 469 | } 470 | } 471 | if ((buf[strlen(buf) - 1] == '.') || (buf[0] == '.')) { 472 | badChar = BTRUE; 473 | goto STR_IS_NUMBER_LABLE; 474 | } 475 | if (strlen(buf) > 1) { 476 | if (buf[0] == '0' && buf[1] != '.') { 477 | badChar = BTRUE; 478 | goto STR_IS_NUMBER_LABLE; 479 | } 480 | } 481 | } 482 | STR_IS_NUMBER_LABLE: 483 | free(buf); 484 | if (badChar) { 485 | break; 486 | } 487 | res = BTRUE; 488 | } while (0); 489 | return res; 490 | } 491 | 492 | /* 493 | * 字符串是否是字符串 494 | * @param input char* 输入的字符串 495 | * @param len int 长度 496 | * @return BOOLEAN 字符串合法 497 | */ 498 | BOOLEAN strIsString(char* input /* in */, int len /* in */) { 499 | BOOLEAN res = BFALSE; 500 | do { 501 | if (len <= 1 || !input) { 502 | break; 503 | } 504 | char* buf = malloc(len + 1); 505 | if (!buf) { 506 | break; 507 | } 508 | memset(buf, 0, len + 1); 509 | memcpy(buf, input, len); 510 | int len = strlen(buf); 511 | if (len < 2) { 512 | free(buf); 513 | break; 514 | } 515 | if (buf[0] != '"' || buf[len - 1] != '"') { 516 | free(buf); 517 | break; 518 | } 519 | if (strstr(buf+1, "\"") != (buf + (len - 1))) { 520 | free(buf); 521 | break; 522 | } 523 | free(buf); 524 | res = BTRUE; 525 | } while (0); 526 | return res; 527 | } 528 | 529 | /* 530 | * 字符串是否是 null 531 | * @param input char* 输入的字符串 532 | * @param len int 长度 533 | * @return BOOLEAN 字符串合法 534 | */ 535 | BOOLEAN strIsNull(char* input /* in */, int len /* in */) { 536 | BOOLEAN res = BFALSE; 537 | do { 538 | if (len <= 3 || !input) { 539 | break; 540 | } 541 | char* buf = malloc(len + 1); 542 | if (!buf) { 543 | break; 544 | } 545 | memset(buf, 0, len + 1); 546 | memcpy(buf, input, len); 547 | if (strcmp(buf, "null") != 0) { 548 | free(buf); 549 | break; 550 | } 551 | free(buf); 552 | res = BTRUE; 553 | } while (0); 554 | return res; 555 | } 556 | 557 | /* 558 | * 字符串是否是 Boolean 559 | * @param input char* 输入的字符串 560 | * @param len int 长度 561 | * @return BOOLEAN 字符串合法 562 | */ 563 | BOOLEAN strIsBoolean(char* input /* in */, int len /* in */) { 564 | BOOLEAN res = BFALSE; 565 | do { 566 | if (len <= 3 || !input) { 567 | break; 568 | } 569 | char* buf = malloc(len + 1); 570 | if (!buf) { 571 | break; 572 | } 573 | memset(buf, 0, len + 1); 574 | memcpy(buf, input, len); 575 | if ((strcmp(buf, "true") != 0 && strcmp(buf, "false") != 0)) { 576 | free(buf); 577 | break; 578 | } 579 | free(buf); 580 | res = BTRUE; 581 | } while (0); 582 | return res; 583 | } 584 | 585 | /* 586 | * 字符串是否是数组 587 | * @param input char* 输入的字符串 588 | * @param len int 长度 589 | * @return BOOLEAN 字符串合法 590 | */ 591 | BOOLEAN strIsArray(char* input /* in */, int len /* in */) { 592 | BOOLEAN res = BFALSE; 593 | do { 594 | if (len <= 1 || !input) { 595 | break; 596 | } 597 | char* buf = malloc(len + 1); 598 | if (!buf) { 599 | break; 600 | } 601 | memset(buf, 0, len + 1); 602 | memcpy(buf, input, len); 603 | if ((buf[0] != '[') || (buf[len - 1] != ']')) { 604 | free(buf); 605 | break; 606 | } 607 | if (len == 2) { 608 | res = BTRUE; 609 | free(buf); 610 | break; 611 | } 612 | BOOLEAN badChar = BFALSE; 613 | for (int i = 1; i < len - 1; i++) { 614 | int idx = 0; 615 | if (buf[i] == '\"') { 616 | // 标志接下来的元素是字符串 617 | idx = i + 1; 618 | while (idx <= len - 2) { 619 | if (buf[idx] == '\"') { 620 | break; 621 | } 622 | idx++; 623 | } 624 | if (idx == len - 1) { 625 | idx--; 626 | } 627 | if (buf[idx] == '\"') { 628 | // 已经匹配到最近的引号 629 | if (idx == len - 2) { 630 | // 此时字符串是最后一个元素,判断字符串是否合法 631 | badChar = !strIsString(buf+i, idx + 1 - i); 632 | break; 633 | } 634 | else { 635 | badChar = !strIsString(buf + i, idx + 1 - i); 636 | if (badChar) { 637 | break; 638 | } 639 | // 距离结尾只有一个字符了,不足以存放数据 640 | if (idx == len - 3) { 641 | badChar = BTRUE; 642 | break; 643 | } 644 | // 下一个字符不是数组分割符 645 | if (buf[idx+1] != ',') { 646 | badChar = BTRUE; 647 | break; 648 | } 649 | i = idx + 1; 650 | } 651 | } 652 | else { 653 | // 没有匹配到 654 | badChar = BTRUE; 655 | break; 656 | } 657 | } 658 | else if (buf[i] == '[') { 659 | // 标志接下来的属性是数组 660 | // 需要索引到匹配的数组符号 661 | int markCount = 1; 662 | idx = i + 1; 663 | int quotCount = 0; 664 | while (idx <= len - 2) { 665 | if (buf[idx] == '\"') { 666 | quotCount++; 667 | } 668 | if (quotCount % 2 == 0) { 669 | if (buf[idx] == ']') { 670 | markCount--; 671 | } 672 | else if (buf[idx] == '[') { 673 | markCount++; 674 | } 675 | } 676 | if (markCount == 0) { 677 | break; 678 | } 679 | idx++; 680 | } 681 | if (idx == len - 1) { 682 | idx--; 683 | } 684 | if (markCount != 0) { 685 | // 数组不匹配 686 | badChar = BTRUE; 687 | break; 688 | } 689 | badChar = !strIsArray(buf + i, idx + 1 - i); 690 | } 691 | else if (buf[i] == '{') { 692 | // 标志接下来是对象 693 | // 标志接下来的属性是数组 694 | // 需要索引到匹配的数组符号 695 | int markCount = 1; 696 | idx = i + 1; 697 | int quotCount = 0; 698 | while (idx <= len - 2) { 699 | if (buf[idx] == '\"') { 700 | quotCount++; 701 | } 702 | if (quotCount % 2 == 0) { 703 | if (buf[idx] == '}') { 704 | markCount--; 705 | } 706 | else if (buf[idx] == '{') { 707 | markCount++; 708 | } 709 | } 710 | if (markCount == 0) { 711 | break; 712 | } 713 | idx++; 714 | } 715 | if (idx == len - 1) { 716 | idx--; 717 | } 718 | if (markCount != 0) { 719 | // 数组不匹配 720 | badChar = BTRUE; 721 | break; 722 | } 723 | badChar = !strIsObject(buf + i, idx + 1 - i); 724 | } 725 | else if (buf[i] == 'f' || buf[i] == 't') { 726 | // 标志接下来是 true 或 false 727 | idx = i + 1; 728 | while (idx <= len - 2) { 729 | if (buf[idx] == ',') { 730 | break; 731 | } 732 | idx++; 733 | } 734 | if (idx == len - 1 || buf[idx] == ',') { 735 | idx--; 736 | } 737 | badChar = !strIsBoolean(buf + i, idx + 1 - i); 738 | } 739 | else if (buf[i] == 'n') { 740 | // 标志接下来是 null 741 | // 标志接下来是 true 或 false 742 | idx = i + 1; 743 | while (idx <= len - 2) { 744 | if (buf[idx] == ',') { 745 | break; 746 | } 747 | idx++; 748 | } 749 | 750 | if (idx == len - 1 || buf[idx] == ',') { 751 | idx--; 752 | } 753 | badChar = !strIsNull(buf + i, idx + 1 - i); 754 | } 755 | else if (('0' <= buf[i] && buf[i] <= '9') || buf[i] == '-') { 756 | // 接下来是数字 757 | idx = i + 1; 758 | while (idx <= len - 2) { 759 | if (buf[idx] == ',') { 760 | break; 761 | } 762 | idx++; 763 | } 764 | 765 | if (idx == len - 1 || buf[idx] == ',') { 766 | idx--; 767 | } 768 | badChar = !strIsNumber(buf + i, idx + 1 - i); 769 | } 770 | else { 771 | badChar = BTRUE; 772 | break; 773 | } 774 | 775 | if (badChar) { 776 | break; 777 | } 778 | // 已经到结尾 779 | if (idx == len - 2) { 780 | break; 781 | } 782 | if (idx == len - 3) { 783 | badChar = BTRUE; 784 | break; 785 | } 786 | // 下一个字符不是数组分割符 787 | if (buf[idx + 1] != ',') { 788 | badChar = BTRUE; 789 | break; 790 | } 791 | i = idx + 1; 792 | } 793 | free(buf); 794 | if (badChar) { 795 | break; 796 | } 797 | res = BTRUE; 798 | } while (0); 799 | return res; 800 | } 801 | 802 | /* 803 | * 字符串是否是对象 804 | * @param input char* 输入的字符串 805 | * @param len int 长度 806 | * @return BOOLEAN 字符串合法 807 | */ 808 | BOOLEAN strIsObject(char* input /* in */, int len /* in */) { 809 | BOOLEAN res = BFALSE; 810 | do { 811 | if (len <= 1 || !input) { 812 | break; 813 | } 814 | char* buf = malloc(len + 1); 815 | if (!buf) { 816 | break; 817 | } 818 | memset(buf, 0, len + 1); 819 | memcpy(buf, input, len); 820 | if ((buf[0] != '{') || (buf[len - 1] != '}')) { 821 | free(buf); 822 | break; 823 | } 824 | if (len == 2) { 825 | free(buf); 826 | res = BTRUE; 827 | break; 828 | } 829 | BOOLEAN badChar = BFALSE; 830 | for (int i = 1; i < len - 1; i++) { 831 | if (buf[i] == '\"') { 832 | int idx = i + 1; 833 | while (idx <= len - 2) { 834 | if (buf[idx] == '\"') { 835 | break; 836 | } 837 | idx++; 838 | } 839 | 840 | // 遍历到结尾,未曾找到结尾 841 | if (idx == len - 1) { 842 | badChar = BTRUE; 843 | break; 844 | } 845 | else if (idx == len - 3) { 846 | badChar = BTRUE; 847 | break; 848 | } else { 849 | if (buf[idx+1] != ':') { 850 | badChar = BTRUE; 851 | break; 852 | } 853 | else { 854 | // 此处需要进行判断数据类型了 855 | int curIdx = idx + 2; 856 | idx = curIdx + 1; 857 | if (buf[curIdx] == '\"') { 858 | // 值为字符串 859 | while (idx <= len - 2) { 860 | if (buf[idx] == '\"') { 861 | break; 862 | } 863 | idx++; 864 | } 865 | // 已经到结尾了,但是没匹配到字符串 866 | badChar = !JSONStringStringIsFormat(buf + i, idx - i + 1, BTRUE); 867 | } 868 | else if (buf[curIdx] == '[') { 869 | // 值为数组 870 | int markCount = 1; 871 | int quotCount = 0; 872 | while (idx <= len - 2) { 873 | if (buf[idx] == '\"') { 874 | quotCount++; 875 | } 876 | if (quotCount % 2 == 0) { 877 | if (buf[idx] == ']') { 878 | markCount--; 879 | } 880 | else if (buf[idx] == '[') { 881 | markCount++; 882 | } 883 | } 884 | if (markCount == 0) { 885 | break; 886 | } 887 | idx++; 888 | } 889 | if (markCount != 0) { 890 | // 数组不匹配 891 | badChar = BTRUE; 892 | break; 893 | } 894 | badChar = !JSONStringArrayIsFormat(buf + i, idx - i + 1, BTRUE); 895 | } 896 | else if (buf[curIdx] == '{') { 897 | // 值为对象 898 | int markCount = 1; 899 | int quotCount = 0; 900 | while (idx <= len - 2) { 901 | if (buf[idx] == '\"') { 902 | quotCount++; 903 | } 904 | if (quotCount % 2 == 0) { 905 | if (buf[idx] == '}') { 906 | markCount--; 907 | } 908 | else if (buf[idx] == '{') { 909 | markCount++; 910 | } 911 | } 912 | if (markCount == 0) { 913 | break; 914 | } 915 | idx++; 916 | } 917 | if (markCount != 0) { 918 | // 数组不匹配 919 | badChar = BTRUE; 920 | break; 921 | } 922 | badChar = !JSONStringObjectIsFormat(buf + i, idx - i + 1, BTRUE); 923 | } 924 | else if (buf[curIdx] == 'n') { 925 | // 值为 null 926 | while (idx <= len - 2) { 927 | if (buf[idx] == ',') { 928 | break; 929 | } 930 | idx++; 931 | } 932 | 933 | if (idx == len - 1 || buf[idx] == ',') { 934 | idx--; 935 | } 936 | badChar = !JSONStringNullIsFormat(buf + i, idx - i + 1, BTRUE); 937 | } 938 | else if (buf[curIdx] == 't' || buf[curIdx] == 'f') { 939 | // 值为 Boolean 940 | while (idx <= len - 2) { 941 | if (buf[idx] == ',') { 942 | break; 943 | } 944 | idx++; 945 | } 946 | 947 | if (idx == len - 1 || buf[idx] == ',') { 948 | idx--; 949 | } 950 | badChar = !JSONStringBooleanIsFormat(buf + i, idx - i + 1, BTRUE); 951 | } 952 | else if ((buf[curIdx] >= '0' && buf[curIdx] <= '9') || buf[curIdx] == '-') { 953 | // 值为 Number 954 | while (idx <= len - 2) { 955 | if (buf[idx] == ',') { 956 | break; 957 | } 958 | idx++; 959 | } 960 | 961 | if (idx == len - 1 || buf[idx] == ',') { 962 | idx--; 963 | } 964 | badChar = !JSONStringNumberIsFormat(buf + i, idx - i + 1, BTRUE); 965 | } 966 | else { 967 | badChar = BTRUE; 968 | break; 969 | } 970 | 971 | 972 | if (idx == len - 1) { 973 | badChar = BTRUE; 974 | break; 975 | } 976 | else { 977 | // 字符串为最后一个元素 978 | if (idx == len - 2) { 979 | break; 980 | } 981 | else if (idx >= len - 6) { 982 | badChar = BTRUE; 983 | break; 984 | } 985 | else { 986 | if (buf[idx + 1] != ',') { 987 | badChar = BTRUE; 988 | break; 989 | } 990 | } 991 | } 992 | } 993 | } 994 | if (badChar) { 995 | break; 996 | } 997 | i = idx + 1; 998 | } 999 | else { 1000 | badChar = BTRUE; 1001 | break; 1002 | } 1003 | } 1004 | free(buf); 1005 | if (badChar) { 1006 | break; 1007 | } 1008 | res = BTRUE; 1009 | } while (0); 1010 | return res; 1011 | } 1012 | 1013 | /* 1014 | * 数字类型键值对字符串格式是否合法 1015 | * @param input char* 输入的字符串 1016 | * @param len int 长度 1017 | * @param isHasKey BOOLEAN 是否包含键 1018 | * @return BOOLEAN 字符串合法 1019 | */ 1020 | BOOLEAN JSONStringNumberIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */) { 1021 | BOOLEAN res = BFALSE; 1022 | do { 1023 | if (!input || len <= 0) { 1024 | break; 1025 | } 1026 | int quotationMarks = 0; // 引号计数 1027 | char ch = 0; 1028 | int quotPosLeft = 0; // 左引号位置 1029 | int quotPosRight = 0; // 右引号位置 1030 | BOOLEAN badKey = BFALSE; // 不合法键名称 1031 | BOOLEAN colonCount = 0;//计数 1032 | if (!isHasKey) { 1033 | badKey = !strIsNumber(input, len); 1034 | if (badKey) { 1035 | break; 1036 | } 1037 | res = BTRUE; 1038 | break; 1039 | } 1040 | for (int i = 0; i < len; i++) { 1041 | ch = input[i] & 0xFF; 1042 | if (ch == '"') { 1043 | quotationMarks++; 1044 | if (quotationMarks % 2) { 1045 | quotPosLeft = i; 1046 | } 1047 | else { 1048 | quotPosRight = i; 1049 | } 1050 | } 1051 | if (quotationMarks % 2 == 0) { 1052 | if (ch == ':') { 1053 | colonCount++; 1054 | if (input[i - 1] != '"') { 1055 | badKey = BTRUE; 1056 | break; 1057 | } 1058 | badKey = !JSONKeyIsOk(input + (quotPosLeft + 1), (quotPosRight - quotPosLeft - 1)); 1059 | if (badKey) { 1060 | break; 1061 | } 1062 | if (i + 1 == len) { 1063 | badKey = BTRUE; 1064 | break; 1065 | } 1066 | badKey = !strIsNumber(input + (i + 1), len - i - 1); 1067 | break; 1068 | } 1069 | } 1070 | } 1071 | if (badKey) { 1072 | break; 1073 | } 1074 | if (colonCount != 1) { 1075 | break; 1076 | } 1077 | if (quotationMarks != 2) { 1078 | break; 1079 | } 1080 | res = BTRUE; 1081 | } while (0); 1082 | return res; 1083 | } 1084 | 1085 | /* 1086 | * null 类型键值对字符串格式是否合法 1087 | * @param input char* 输入的字符串 1088 | * @param len int 长度 1089 | * @param isHasKey BOOLEAN 是否包含键 1090 | * @return BOOLEAN 字符串合法 1091 | */ 1092 | BOOLEAN JSONStringNullIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */) { 1093 | BOOLEAN res = BFALSE; 1094 | do { 1095 | if (!input || len <= 3) { 1096 | break; 1097 | } 1098 | int quotationMarks = 0; // 引号计数 1099 | char ch = 0; 1100 | int quotPosLeft = 0; // 左引号位置 1101 | int quotPosRight = 0; // 右引号位置 1102 | BOOLEAN badKey = BFALSE; // 不合法键名称 1103 | BOOLEAN colonCount = 0;//计数 1104 | 1105 | if (!isHasKey) { 1106 | badKey = !strIsNull(input, len); 1107 | if (badKey) { 1108 | break; 1109 | } 1110 | res = BTRUE; 1111 | break; 1112 | } 1113 | 1114 | for (int i = 0; i < len; i++) { 1115 | ch = input[i] & 0xFF; 1116 | if (ch == '"') { 1117 | quotationMarks++; 1118 | if (quotationMarks % 2) { 1119 | quotPosLeft = i; 1120 | } 1121 | else { 1122 | quotPosRight = i; 1123 | } 1124 | } 1125 | if (quotationMarks % 2 == 0) { 1126 | if (ch == ':') { 1127 | colonCount++; 1128 | if (input[i - 1] != '"') { 1129 | badKey = BTRUE; 1130 | break; 1131 | } 1132 | badKey = !JSONKeyIsOk(input + (quotPosLeft + 1), (quotPosRight - quotPosLeft - 1)); 1133 | if (badKey) { 1134 | break; 1135 | } 1136 | if (i + 1 == len) { 1137 | badKey = BTRUE; 1138 | break; 1139 | } 1140 | badKey = !strIsNull(input + (i + 1), len - i - 1); 1141 | break; 1142 | } 1143 | } 1144 | } 1145 | if (badKey) { 1146 | break; 1147 | } 1148 | if (colonCount != 1) { 1149 | break; 1150 | } 1151 | if (quotationMarks != 2) { 1152 | break; 1153 | } 1154 | res = BTRUE; 1155 | } while (0); 1156 | return res; 1157 | } 1158 | 1159 | /* 1160 | * Boolean 类型键值对字符串格式是否合法 1161 | * @param input char* 输入的字符串 1162 | * @param len int 长度 1163 | * @param isHasKey BOOLEAN 是否包含键 1164 | * @return BOOLEAN 字符串合法 1165 | */ 1166 | BOOLEAN JSONStringBooleanIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */) { 1167 | BOOLEAN res = BFALSE; 1168 | do { 1169 | if (!input || len <= 3) { 1170 | break; 1171 | } 1172 | int quotationMarks = 0; // 引号计数 1173 | char ch = 0; 1174 | int quotPosLeft = 0; // 左引号位置 1175 | int quotPosRight = 0; // 右引号位置 1176 | BOOLEAN badKey = BFALSE; // 不合法键名称 1177 | BOOLEAN colonCount = 0;//计数 1178 | 1179 | if (!isHasKey) { 1180 | badKey = !strIsBoolean(input, len); 1181 | if (badKey) { 1182 | break; 1183 | } 1184 | res = BTRUE; 1185 | break; 1186 | } 1187 | 1188 | for (int i = 0; i < len; i++) { 1189 | ch = input[i] & 0xFF; 1190 | if (ch == '"') { 1191 | quotationMarks++; 1192 | if (quotationMarks % 2) { 1193 | quotPosLeft = i; 1194 | } 1195 | else { 1196 | quotPosRight = i; 1197 | } 1198 | } 1199 | if (quotationMarks % 2 == 0) { 1200 | if (ch == ':') { 1201 | colonCount++; 1202 | if (input[i - 1] != '"') { 1203 | badKey = BTRUE; 1204 | break; 1205 | } 1206 | badKey = !JSONKeyIsOk(input + (quotPosLeft + 1), (quotPosRight - quotPosLeft - 1)); 1207 | if (badKey) { 1208 | break; 1209 | } 1210 | if (i + 1 == len) { 1211 | badKey = BTRUE; 1212 | break; 1213 | } 1214 | badKey = !strIsBoolean(input + (i + 1), len - i - 1); 1215 | break; 1216 | } 1217 | } 1218 | } 1219 | if (badKey) { 1220 | break; 1221 | } 1222 | if (colonCount != 1) { 1223 | break; 1224 | } 1225 | if (quotationMarks != 2) { 1226 | break; 1227 | } 1228 | res = BTRUE; 1229 | } while (0); 1230 | return res; 1231 | } 1232 | 1233 | /* 1234 | * String 类型键值对字符串格式是否合法 1235 | * @param input char* 输入的字符串 1236 | * @param len int 长度 1237 | * @param isHasKey BOOLEAN 是否包含键 1238 | * @return BOOLEAN 字符串合法 1239 | */ 1240 | BOOLEAN JSONStringStringIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */) { 1241 | BOOLEAN res = BFALSE; 1242 | do { 1243 | if (!input || len <= 1) { 1244 | break; 1245 | } 1246 | int quotationMarks = 0; // 引号计数 1247 | char ch = 0; 1248 | int quotPosLeft = 0; // 左引号位置 1249 | int quotPosRight = 0; // 右引号位置 1250 | BOOLEAN badKey = BFALSE; // 不合法键名称 1251 | BOOLEAN colonCount = 0;//计数 1252 | 1253 | if (!isHasKey) { 1254 | badKey = !strIsString(input, len); 1255 | if (badKey) { 1256 | break; 1257 | } 1258 | res = BTRUE; 1259 | break; 1260 | } 1261 | 1262 | for (int i = 0; i < len; i++) { 1263 | ch = input[i] & 0xFF; 1264 | if (ch == '"') { 1265 | quotationMarks++; 1266 | if (quotationMarks % 2) { 1267 | quotPosLeft = i; 1268 | } 1269 | else { 1270 | quotPosRight = i; 1271 | } 1272 | } 1273 | if (quotationMarks % 2 == 0) { 1274 | if (ch == ':') { 1275 | colonCount++; 1276 | if (input[i - 1] != '"') { 1277 | badKey = BTRUE; 1278 | break; 1279 | } 1280 | badKey = !JSONKeyIsOk(input + (quotPosLeft + 1), (quotPosRight - quotPosLeft - 1)); 1281 | if (badKey) { 1282 | break; 1283 | } 1284 | if (i + 1 == len) { 1285 | badKey = BTRUE; 1286 | break; 1287 | } 1288 | badKey = !strIsString(input + (i + 1), len - i - 1); 1289 | if (badKey) { 1290 | break; 1291 | } 1292 | } 1293 | } 1294 | } 1295 | if (badKey) { 1296 | break; 1297 | } 1298 | if (colonCount != 1) { 1299 | break; 1300 | } 1301 | if (quotationMarks != 4) { 1302 | break; 1303 | } 1304 | res = BTRUE; 1305 | } while (0); 1306 | return res; 1307 | } 1308 | 1309 | /* 1310 | * Array 类型键值对字符串格式是否合法 1311 | * @param input char* 输入的字符串 1312 | * @param len int 长度 1313 | * @param isHasKey BOOLEAN 是否包含键 1314 | * @return BOOLEAN 字符串合法 1315 | */ 1316 | BOOLEAN JSONStringArrayIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */) { 1317 | BOOLEAN res = BFALSE; 1318 | do { 1319 | if (!input || len <= 1) { 1320 | break; 1321 | } 1322 | int quotationMarks = 0; // 引号计数 1323 | char ch = 0; 1324 | int quotPosLeft = 0; // 左引号位置 1325 | int quotPosRight = 0; // 右引号位置 1326 | BOOLEAN badKey = BFALSE; // 不合法键名称 1327 | BOOLEAN colonCount = 0;//计数 1328 | if (!isHasKey) { 1329 | badKey = !strIsArray(input, len); 1330 | if (badKey) { 1331 | break; 1332 | } 1333 | res = BTRUE; 1334 | break; 1335 | } 1336 | 1337 | for (int i = 0; i < len; i++) { 1338 | ch = input[i] & 0xFF; 1339 | if (ch == '"') { 1340 | quotationMarks++; 1341 | if (quotationMarks % 2) { 1342 | quotPosLeft = i; 1343 | } 1344 | else { 1345 | quotPosRight = i; 1346 | } 1347 | } 1348 | if (quotationMarks % 2 == 0) { 1349 | if (ch == ':') { 1350 | colonCount++; 1351 | if (input[i - 1] != '"') { 1352 | badKey = BTRUE; 1353 | break; 1354 | } 1355 | badKey = !JSONKeyIsOk(input + (quotPosLeft + 1), (quotPosRight - quotPosLeft - 1)); 1356 | if (badKey) { 1357 | break; 1358 | } 1359 | if (i + 1 == len) { 1360 | badKey = BTRUE; 1361 | break; 1362 | } 1363 | 1364 | badKey = !strIsArray(input + (i + 1), len - i - 1); 1365 | break; 1366 | } 1367 | } 1368 | } 1369 | 1370 | if (badKey) { 1371 | break; 1372 | } 1373 | if (colonCount != 1) { 1374 | break; 1375 | } 1376 | if (quotationMarks != 2) { 1377 | break; 1378 | } 1379 | res = BTRUE; 1380 | } while (0); 1381 | return res; 1382 | } 1383 | 1384 | /* 1385 | * Object 类型键值对字符串格式是否合法 1386 | * @param input char* 输入的字符串 1387 | * @param len int 长度 1388 | * @param isHasKey BOOLEAN 是否包含键 1389 | * @return BOOLEAN 字符串合法 1390 | */ 1391 | BOOLEAN JSONStringObjectIsFormat(char* input /* in */, int len /* in */, BOOLEAN isHasKey /* in */) { 1392 | BOOLEAN res = BFALSE; 1393 | do { 1394 | if (!input || len <= 1) { 1395 | break; 1396 | } 1397 | int quotationMarks = 0; // 引号计数 1398 | char ch = 0; 1399 | int quotPosLeft = 0; // 左引号位置 1400 | int quotPosRight = 0; // 右引号位置 1401 | BOOLEAN badKey = BFALSE; // 不合法键名称 1402 | BOOLEAN colonCount = 0;//计数 1403 | if (!isHasKey) { 1404 | badKey = !strIsObject(input, len); 1405 | if (badKey) { 1406 | break; 1407 | } 1408 | res = BTRUE; 1409 | break; 1410 | } 1411 | 1412 | for (int i = 0; i < len; i++) { 1413 | ch = input[i] & 0xFF; 1414 | if (ch == '"') { 1415 | quotationMarks++; 1416 | if (quotationMarks % 2) { 1417 | quotPosLeft = i; 1418 | } 1419 | else { 1420 | quotPosRight = i; 1421 | } 1422 | } 1423 | if (quotationMarks % 2 == 0) { 1424 | if (ch == ':') { 1425 | colonCount++; 1426 | if (input[i - 1] != '"') { 1427 | badKey = BTRUE; 1428 | break; 1429 | } 1430 | badKey = !JSONKeyIsOk(input + (quotPosLeft + 1), (quotPosRight - quotPosLeft - 1)); 1431 | if (badKey) { 1432 | break; 1433 | } 1434 | if (i + 1 == len) { 1435 | badKey = BTRUE; 1436 | break; 1437 | } 1438 | 1439 | badKey = !strIsObject(input + (i + 1), len - i - 1); 1440 | break; 1441 | } 1442 | } 1443 | } 1444 | 1445 | if (badKey) { 1446 | break; 1447 | } 1448 | if (colonCount != 1) { 1449 | break; 1450 | } 1451 | if (quotationMarks != 2) { 1452 | break; 1453 | } 1454 | res = BTRUE; 1455 | } while (0); 1456 | return res; 1457 | } 1458 | 1459 | /* 1460 | * 去掉字符串中多余的逗号 1461 | * @param input char* 需要去逗号的字符串 1462 | * @return char* 去逗号后的字符串 1463 | */ 1464 | char* JSONStringRemoveComma(char* input /* in */) { 1465 | char* output = NULL; 1466 | do { 1467 | if (!input || !strlen(input)) { 1468 | break; 1469 | } 1470 | output = (char*)malloc(strlen(input) + 1); 1471 | if (!output) { 1472 | break; 1473 | } 1474 | memset(output, 0, strlen(input) + 1); 1475 | // 引号计数 1476 | int quotationMarks = 0; 1477 | char ch = 0; 1478 | int j = 0; 1479 | int len = strlen(input); 1480 | for (int i = 0; i < len; i++) { 1481 | ch = input[i] & 0xFF; 1482 | if (input[i] == '"') { 1483 | quotationMarks++; 1484 | } 1485 | if (quotationMarks % 2 == 0) { 1486 | if (ch == ',') { 1487 | if (i + 1 < len) { 1488 | if (((input[i+1] & 0xFF) == ']') || ((input[i+1] & 0xFF) == '}')) { 1489 | continue; 1490 | } 1491 | } 1492 | } 1493 | output[j] = input[i]; 1494 | j++; 1495 | } 1496 | else { 1497 | output[j] = input[i]; 1498 | j++; 1499 | } 1500 | } 1501 | } while (0); 1502 | return output; 1503 | } 1504 | 1505 | /* 1506 | * 判断 键是否有效 1507 | * @param input char* 输入的键 1508 | * @param len int 长度 1509 | */ 1510 | BOOLEAN JSONKeyIsOk(char* input /* in */, int len /* in */) { 1511 | BOOLEAN res = BFALSE; 1512 | do { 1513 | if (!input || len <= 0) { 1514 | break; 1515 | } 1516 | BOOLEAN isBad = BFALSE; 1517 | char ch = 0; 1518 | for (int i = 0; i < len; i++) { 1519 | ch = input[i] & 0xFF; 1520 | if (0 <= (ch & 0xFF) && (ch & 0xFF) < 0x20) { 1521 | isBad = BTRUE; 1522 | } 1523 | } 1524 | if (isBad) { 1525 | break; 1526 | } 1527 | res = BTRUE; 1528 | } while (0); 1529 | return res; 1530 | } 1531 | 1532 | /* 1533 | * json 字符串去空格等不可见字符 1534 | * @param input char* 需要去空格的字符串 1535 | * @return char* 去空格后的字符串 1536 | */ 1537 | char* JSONStringTrim(char* input /* in */) { 1538 | char* output = NULL; 1539 | do { 1540 | if (!input || !strlen(input)) { 1541 | break; 1542 | } 1543 | output = (char*)malloc(strlen(input) + 1); 1544 | if (!output) { 1545 | break; 1546 | } 1547 | memset(output, 0, strlen(input) + 1); 1548 | // 引号计数 1549 | int quotationMarks = 0; 1550 | char ch = 0; 1551 | int j = 0; 1552 | int len = strlen(input); 1553 | for (int i = 0; i < len; i++) { 1554 | ch = input[i] & 0xFF; 1555 | if (input[i] == '"') { 1556 | quotationMarks++; 1557 | } 1558 | if (quotationMarks % 2 == 0) { 1559 | if (((int)ch >= 0x21 && (int)ch <= 0x7D) || (int)ch < 0) { 1560 | output[j] = input[i]; 1561 | j++; 1562 | } 1563 | } 1564 | else { 1565 | output[j] = input[i]; 1566 | j++; 1567 | } 1568 | } 1569 | /*char* tmp = JSONStringRemoveComma(output); 1570 | free(output); 1571 | output = tmp;*/ 1572 | } while (0); 1573 | return output; 1574 | } 1575 | 1576 | /* 1577 | * 判断 json 字符串是否合法 1578 | * @param input char* 需要解析的字符串 1579 | * @return BOOLEAN 符合格式要求,返回 BTRUE 否则返回 BFALSE 1580 | */ 1581 | BOOLEAN JSONStringIsFormat(char* input /* in */) { 1582 | BOOLEAN res = BFALSE; 1583 | do { 1584 | if (!input) { 1585 | break; 1586 | } 1587 | res = JSONStringObjectIsFormat(input, strlen(input), BFALSE); 1588 | } while (0); 1589 | return res; 1590 | } 1591 | 1592 | ////////////////////////////////////////////////////////////////////////// 1593 | ////////////////////////////////////////////////////////////////////////// 1594 | ////////////////////////////////////////////////////////////////////////// 1595 | /** 1596 | * 计算 md5 加密后的字符串 1597 | * @param input 需要加密的字符串 1598 | * @return unsigned int 1599 | */ 1600 | unsigned long int encryptionHashIndex(char *input /* in */) { 1601 | unsigned long int hash = 0; 1602 | do { 1603 | if (!input) { 1604 | break; 1605 | } 1606 | MD5_CTX md5 = {0}; 1607 | unsigned char decrypt[16] = { 0 }; 1608 | unsigned long int seed = 7; 1609 | 1610 | MD5Init(&md5); 1611 | MD5Update(&md5, (unsigned char*)input, strlen((char *)input)); 1612 | MD5Final(&md5, decrypt); 1613 | for (int i = 0; i < 16; i++){ 1614 | hash += hash * seed + decrypt[i]; 1615 | } 1616 | } while (0); 1617 | return hash % HASH_MAX; 1618 | } 1619 | 1620 | /* 1621 | * 根据 json key 值,转换成 int 值 1622 | * @param key json 键 1623 | * @return int 1624 | */ 1625 | int JSONGetHashIndex(char *key /* in */) { 1626 | int idx = -1; 1627 | do { 1628 | if (!key) { 1629 | break; 1630 | } 1631 | idx = encryptionHashIndex(key); 1632 | } while (0); 1633 | //printf("属性: %s, 索引: %d\n", key, idx); 1634 | return idx; 1635 | } 1636 | 1637 | /* 1638 | * 深拷贝字符串 1639 | * @param input 输入的字符串 1640 | * @return char* 分配的字符串内存 1641 | */ 1642 | char* deepCloneString(char* input /* in */) { 1643 | char* pRes = NULL; 1644 | do { 1645 | if (!input) { 1646 | break; 1647 | } 1648 | pRes = (char*)malloc(strlen(input) + 1); 1649 | if (!pRes) { 1650 | break; 1651 | } 1652 | memset(pRes, 0, strlen(input) + 1); 1653 | strcat(pRes, input); 1654 | } while (0); 1655 | return pRes; 1656 | } 1657 | 1658 | /* 1659 | * 创建 Object 空对象 1660 | * @param key char* 键名称 1661 | * @return pJsonObjectNode 1662 | */ 1663 | pJsonObjectNode JSONCreateObjectNode(char* key /* in */) { 1664 | pJsonObjectNode pObject = NULL; 1665 | do { 1666 | pObject = (pJsonObjectNode)malloc(sizeof(JsonObjectNode)); 1667 | if (!pObject) { 1668 | // json对象内存分配失败 1669 | break; 1670 | } 1671 | memset(pObject, 0, sizeof(JsonObjectNode)); 1672 | if (key) { 1673 | pObject->node.keyName = deepCloneString(key); 1674 | if (!pObject->node.keyName) { 1675 | // 拷贝名称失败,则节点毫无意义 1676 | JSONDestroyObjectNode(pObject); 1677 | break; 1678 | } 1679 | } 1680 | pObject->node.type = JSONTYPEOBJECT; 1681 | } while (0); 1682 | return pObject; 1683 | } 1684 | 1685 | /* 1686 | * 创建 Number 节点 1687 | * @param key char* 键名称 1688 | * @param value double 键值 1689 | * @return pJsonNumberNode Number节点指针 1690 | */ 1691 | pJsonNumberNode JSONCreateNumberNode(char* key /* in */, double value /* in */) { 1692 | pJsonNumberNode pNode = NULL; 1693 | 1694 | do { 1695 | pNode = (pJsonNumberNode)malloc(sizeof(JsonNumberNode)); 1696 | if (!pNode) { 1697 | break; 1698 | } 1699 | memset(pNode, 0, sizeof(JsonNumberNode)); 1700 | if (key) { 1701 | pNode->node.keyName = deepCloneString(key); 1702 | if (!pNode->node.keyName) { 1703 | // 拷贝名称失败,则节点毫无意义 1704 | JSONDestroyNumberNode(pNode); 1705 | break; 1706 | } 1707 | } 1708 | pNode->node.type = JSONTYPENUMBER; 1709 | pNode->value = value; 1710 | } while (0); 1711 | 1712 | return pNode; 1713 | } 1714 | 1715 | /* 1716 | * 创建 Number 节点 1717 | * @param key char* 键名称 1718 | * @param value UINT64 键值 1719 | * @return pJsonNumberNode Number节点指针 1720 | */ 1721 | pJsonLongNumberNode JSONCreateLongNumberNode(char* key /* in */, UINT64 value /* in */) { 1722 | pJsonLongNumberNode pNode = NULL; 1723 | 1724 | do { 1725 | pNode = (pJsonLongNumberNode)malloc(sizeof(JsonLongNumberNode)); 1726 | if (!pNode) { 1727 | break; 1728 | } 1729 | memset(pNode, 0, sizeof(JsonLongNumberNode)); 1730 | if (key) { 1731 | pNode->node.keyName = deepCloneString(key); 1732 | if (!pNode->node.keyName) { 1733 | // 拷贝名称失败,则节点毫无意义 1734 | JSONDestroyLongNumberNode(pNode); 1735 | break; 1736 | } 1737 | } 1738 | pNode->node.type = JSONTYPELONGNUMBER; 1739 | pNode->value = value; 1740 | } while (0); 1741 | 1742 | return pNode; 1743 | } 1744 | 1745 | /* 1746 | * 创建 String 节点 1747 | * @param key char* 键名称 1748 | * @param value char* 键值 1749 | * @return pJsonStringNode String节点指针 1750 | */ 1751 | pJsonStringNode JSONCreateStringNode(char* key /* in */, char* value /* in */) { 1752 | pJsonStringNode pNode = NULL; 1753 | 1754 | do { 1755 | pNode = (pJsonStringNode)malloc(sizeof(JsonStringNode)); 1756 | if (!pNode) { 1757 | break; 1758 | } 1759 | memset(pNode, 0, sizeof(JsonStringNode)); 1760 | if (key) { 1761 | pNode->node.keyName = deepCloneString(key); 1762 | if (!pNode->node.keyName) { 1763 | // 拷贝名称失败,则节点毫无意义 1764 | JSONDestroyStringNode(pNode); 1765 | break; 1766 | } 1767 | } 1768 | pNode->node.type = JSONTYPESTRING; 1769 | pNode->value = deepCloneString(value); 1770 | } while (0); 1771 | 1772 | return pNode; 1773 | } 1774 | 1775 | /* 1776 | * 创建 Array 空数组对象 1777 | * @param key char* 键名称 1778 | * @return pJsonArrayNode Array节点指针 1779 | */ 1780 | pJsonArrayNode JSONCreateArrayNode(char* key /* in */) { 1781 | pJsonArrayNode pNode = NULL; 1782 | 1783 | do { 1784 | pNode = (pJsonArrayNode)malloc(sizeof(JsonArrayNode)); 1785 | if (!pNode) { 1786 | break; 1787 | } 1788 | memset(pNode, 0, sizeof(JsonArrayNode)); 1789 | if (key) { 1790 | pNode->node.keyName = deepCloneString(key); 1791 | if (!pNode->node.keyName) { 1792 | // 拷贝名称失败,则节点毫无意义 1793 | JSONDestroyArrayNode(pNode); 1794 | break; 1795 | } 1796 | } 1797 | pNode->node.type = JSONTYPEARRAY; 1798 | } while (0); 1799 | return pNode; 1800 | } 1801 | 1802 | /* 1803 | * 创建 null 节点 1804 | * @param key char* 键名称 1805 | * @return pJsonNullNode null节点指针 1806 | */ 1807 | pJsonNullNode JSONCreateNullNode(char* key /* in */) { 1808 | pJsonNullNode pNode = NULL; 1809 | 1810 | do { 1811 | pNode = (pJsonNullNode)malloc(sizeof(JsonNullNode)); 1812 | if (!pNode) { 1813 | break; 1814 | } 1815 | memset(pNode, 0, sizeof(JsonNullNode)); 1816 | if (key) { 1817 | pNode->node.keyName = deepCloneString(key); 1818 | if (!pNode->node.keyName) { 1819 | // 拷贝名称失败,则节点毫无意义 1820 | JSONDestroyNullNode(pNode); 1821 | break; 1822 | } 1823 | } 1824 | pNode->node.type = JSONTYPENULL; 1825 | pNode->value = (int)null; 1826 | } while (0); 1827 | 1828 | return pNode; 1829 | } 1830 | 1831 | /* 1832 | * 创建 Boolean 节点 1833 | * @param key char* 键名称 1834 | * @param value BOOLEAN 键值 1835 | * @return pJsonBooleanNode Boolean节点指针 1836 | */ 1837 | pJsonBooleanNode JSONCreateBooleanNode(char* key /* in */, BOOLEAN value /* in */) { 1838 | pJsonBooleanNode pNode = NULL; 1839 | 1840 | do { 1841 | pNode = (pJsonBooleanNode)malloc(sizeof(JsonBooleanNode)); 1842 | if (!pNode) { 1843 | break; 1844 | } 1845 | memset(pNode, 0, sizeof(JsonBooleanNode)); 1846 | if (key) { 1847 | pNode->node.keyName = deepCloneString(key); 1848 | if (!pNode->node.keyName) { 1849 | // 拷贝名称失败,则节点毫无意义 1850 | JSONDestroyBooleanNode(pNode); 1851 | break; 1852 | } 1853 | } 1854 | pNode->node.type = JSONTYPEBOOLEAN; 1855 | pNode->value = value && 0x01; 1856 | } while (0); 1857 | 1858 | return pNode; 1859 | } 1860 | 1861 | /* 1862 | * 创建 Object 空对象 1863 | * @return pJsonObjectNode 1864 | */ 1865 | pJsonObjectNode JSONCreate(void) { 1866 | return JSONCreateObjectNode(NULL); 1867 | } 1868 | 1869 | /* 1870 | * 创建 Array 空数组 1871 | * @return pJsonArrayNode 1872 | */ 1873 | pJsonArrayNode JSONCreateArray(void) { 1874 | return JSONCreateArrayNode(NULL); 1875 | } 1876 | 1877 | /* 1878 | * 格式化 json 数组 1879 | * @param input char* 需要格式化的字符串 1880 | * @return pJsonArrayNode 1881 | */ 1882 | pJsonArrayNode JSONParseArray(char* input /* in */) { 1883 | pJsonArrayNode pArray = NULL; 1884 | do { 1885 | if (!input || strlen(input) < 2) { 1886 | break; 1887 | } 1888 | int len = strlen(input); 1889 | // 字符串格式已经做了校验,所以无需考虑格式问题 1890 | pArray = JSONCreateArray(); 1891 | if (!pArray || len == 2) { 1892 | break; 1893 | } 1894 | int idx = 0; 1895 | char* element = NULL; 1896 | for (int i = 1; i < len - 1; i++) { 1897 | if (input[i] == '\"') { 1898 | // 元素是字符串 1899 | idx = i + 1; 1900 | while (idx <= len - 2) { 1901 | if (input[idx] == '\"') { 1902 | break; 1903 | } 1904 | idx++; 1905 | } 1906 | element = (char*)malloc(idx - i); 1907 | if (!element) { 1908 | break; 1909 | } 1910 | memset(element, 0, idx - i); 1911 | memcpy(element, input + i + 1, idx - i - 1); 1912 | JSONArrayPushString(pArray, element); 1913 | free(element); 1914 | } 1915 | else if (input[i] == 'n') { 1916 | JSONArrayPushNull(pArray); 1917 | idx = i + 3; 1918 | } 1919 | else if (input[i] == 't' || input[i] == 'f') { 1920 | if (input[i] == 't') { 1921 | JSONArrayPushBoolean(pArray, BTRUE); 1922 | idx = i + 3; 1923 | } 1924 | else { 1925 | JSONArrayPushBoolean(pArray, BFALSE); 1926 | idx = i + 4; 1927 | } 1928 | } 1929 | else if ((input[i] >= '0' && input[i] <= '9') || input[i] == '-') { 1930 | double num = 0; 1931 | idx = i + 1; 1932 | int not_long = 0; 1933 | while (idx <= len - 2) { 1934 | if(input[idx] == '.' || input[idx] == '-'){ 1935 | not_long = 1; 1936 | } 1937 | if (input[idx] == ',') { 1938 | break; 1939 | } 1940 | idx++; 1941 | } 1942 | idx--; 1943 | sscanf(input + idx, "%lf", &num); 1944 | if(num > 2100000000 && !not_long){ 1945 | UINT64 lNum = 0; 1946 | sscanf(input + idx, "%llu", &lNum); 1947 | JSONArrayPushLongNumber(pArray, lNum); 1948 | }else { 1949 | JSONArrayPushNumber(pArray, num); 1950 | } 1951 | } 1952 | else if (input[i] == '[') { 1953 | idx = i + 1; 1954 | int markCount = 1; 1955 | int quotCount = 0; 1956 | while (idx <= len - 2) { 1957 | if (input[idx] == '\"') { 1958 | quotCount++; 1959 | } 1960 | if (quotCount % 2 == 0) { 1961 | if (input[idx] == '[') { 1962 | markCount++; 1963 | } 1964 | else if (input[idx] == ']') { 1965 | markCount--; 1966 | } 1967 | } 1968 | if (markCount == 0) { 1969 | break; 1970 | } 1971 | idx++; 1972 | } 1973 | element = (char*)malloc(idx - i + 2); 1974 | if (!element) { 1975 | break; 1976 | } 1977 | memset(element, 0, idx - i + 2); 1978 | memcpy(element, input + i, idx - i + 1); 1979 | pJsonArrayNode pNode = JSONParseArray(element); 1980 | free(element); 1981 | if (pNode) { 1982 | JSONArrayPushArray(pArray, pNode); 1983 | } 1984 | } 1985 | else if (input[i] == '{') { 1986 | idx = i + 1; 1987 | int markCount = 1; 1988 | int quotCount = 0; 1989 | while (idx <= len - 2) { 1990 | if (input[idx] == '\"') { 1991 | quotCount++; 1992 | } 1993 | if (quotCount % 2 == 0) { 1994 | if (input[idx] == '{') { 1995 | markCount++; 1996 | } 1997 | else if (input[idx] == '}') { 1998 | markCount--; 1999 | } 2000 | } 2001 | if (markCount == 0) { 2002 | break; 2003 | } 2004 | idx++; 2005 | } 2006 | element = (char*)malloc(idx - i + 2); 2007 | if (!element) { 2008 | break; 2009 | } 2010 | memset(element, 0, idx - i + 2); 2011 | memcpy(element, input + i, idx - i + 1); 2012 | pJsonObjectNode pNode = JSONParseObject(element); 2013 | free(element); 2014 | if (pNode) { 2015 | JSONArrayPushObject(pArray, pNode); 2016 | } 2017 | } 2018 | else { 2019 | break; 2020 | } 2021 | i = idx + 1; 2022 | } 2023 | } while (0); 2024 | return pArray; 2025 | } 2026 | 2027 | /* 2028 | * 格式化 json 对象 2029 | * @param input char* 需要格式化的字符串 2030 | * @return pJsonObjectNode 2031 | */ 2032 | pJsonObjectNode JSONParseObject(char* input /* in */) { 2033 | pJsonObjectNode pObject = NULL; 2034 | do { 2035 | if (!input || strlen(input) < 2) { 2036 | break; 2037 | } 2038 | int len = strlen(input); 2039 | // 字符串格式已经做了校验,所以无需考虑格式问题 2040 | pObject = JSONCreate(); 2041 | if (!pObject || len == 2) { 2042 | break; 2043 | } 2044 | int idx = 0; 2045 | for (int i = 1; i < len - 1; i++) { 2046 | if (input[i] == '\"') { 2047 | idx = i + 1; 2048 | while (idx <= len - 2) { 2049 | if (input[idx] == '\"') { 2050 | break; 2051 | } 2052 | idx++; 2053 | } 2054 | char* key = (char*)malloc(idx - i); 2055 | if (!key) { 2056 | break; 2057 | } 2058 | memset(key, 0, idx - i); 2059 | memcpy(key, input + i + 1, idx - i - 1); 2060 | ///////////////////////// 记得销毁 key ////////////////////////// 2061 | idx += 2; 2062 | int valIdx = 0; 2063 | char* value = NULL; 2064 | if (input[idx] == '\"') { 2065 | // 值是字符串 2066 | valIdx = idx + 1; 2067 | while (valIdx <= len - 2) { 2068 | if (input[valIdx] == '\"') { 2069 | break; 2070 | } 2071 | valIdx++; 2072 | } 2073 | if (valIdx - idx == 1) { 2074 | // 空字符串 2075 | JSONSetStringAttr(pObject, key, ""); 2076 | } 2077 | else { 2078 | value = malloc(valIdx - idx); 2079 | if (!value) { 2080 | free(key); 2081 | break; 2082 | } 2083 | memset(value, 0, valIdx - idx); 2084 | memcpy(value, input + (idx + 1), valIdx - idx - 1); 2085 | JSONSetStringAttr(pObject, key, value); 2086 | free(value); 2087 | } 2088 | } 2089 | else if (input[idx] == 'n') { 2090 | // 值是 null 2091 | JSONSetNullAttr(pObject, key); 2092 | valIdx = idx + 3; 2093 | } 2094 | else if (input[idx] == 't' || input[idx] == 'f') { 2095 | // 值是 boolean 2096 | if (input[idx] == 't') { 2097 | JSONSetBooleanAttr(pObject, key, BTRUE); 2098 | valIdx = idx + 3; 2099 | } 2100 | else { 2101 | JSONSetBooleanAttr(pObject, key, BFALSE); 2102 | valIdx = idx + 4; 2103 | } 2104 | } 2105 | else if ((input[idx] >= '0' && input[idx] <= '9') || input[idx] == '-') { 2106 | // 值是 number 或 long long 2107 | valIdx = idx + 1; 2108 | int not_long = 0; 2109 | while (valIdx <= len - 2) { 2110 | if(input[valIdx] == '.' || input[valIdx] == '-'){ 2111 | not_long = 1; 2112 | } 2113 | if (input[valIdx] == ',') { 2114 | break; 2115 | } 2116 | valIdx++; 2117 | } 2118 | valIdx--; 2119 | double num = 0; 2120 | sscanf(input + idx, "%lf", &num); 2121 | if(num > 2100000000 && !not_long){ 2122 | UINT64 lNum = 0; 2123 | sscanf(input + idx, "%llu", &lNum); 2124 | JSONSetLongNumberAttr(pObject, key, lNum); 2125 | }else { 2126 | JSONSetNumberAttr(pObject, key, num); 2127 | } 2128 | } 2129 | else if (input[idx] == '[') { 2130 | // 值是 array 2131 | valIdx = idx + 1; 2132 | int markCount = 1; 2133 | int quotCount = 0; 2134 | while (valIdx <= len - 2) { 2135 | if (input[valIdx] == '\"') { 2136 | quotCount++; 2137 | } 2138 | if (quotCount % 2 == 0) { 2139 | if (input[valIdx] == '[') { 2140 | markCount++; 2141 | } 2142 | else if(input[valIdx] == ']'){ 2143 | markCount--; 2144 | } 2145 | } 2146 | if (markCount == 0) { 2147 | break; 2148 | } 2149 | valIdx++; 2150 | } 2151 | value = (char*)malloc(valIdx - idx + 2); 2152 | if (!value) { 2153 | free(key); 2154 | break; 2155 | } 2156 | memset(value, 0, valIdx - idx + 2); 2157 | memcpy(value, input + idx, valIdx - idx + 1); 2158 | pJsonArrayNode pNode = JSONParseArray(value); 2159 | free(value); 2160 | if (pNode) { 2161 | JSONSetArrayAttr(pObject, key, pNode, BFALSE); 2162 | } 2163 | } 2164 | else if (input[idx] == '{') { 2165 | // 值是 object 2166 | valIdx = idx + 1; 2167 | int markCount = 1; 2168 | int quotCount = 0; 2169 | while (valIdx <= len - 2) { 2170 | if (input[valIdx] == '\"') { 2171 | quotCount++; 2172 | } 2173 | if (quotCount % 2 == 0) { 2174 | if (input[valIdx] == '{') { 2175 | markCount++; 2176 | } 2177 | else if (input[valIdx] == '}') { 2178 | markCount--; 2179 | } 2180 | } 2181 | if (markCount == 0) { 2182 | break; 2183 | } 2184 | valIdx++; 2185 | } 2186 | value = (char*)malloc(valIdx - idx + 2); 2187 | if (!value) { 2188 | free(key); 2189 | break; 2190 | } 2191 | memset(value, 0, valIdx - idx + 2); 2192 | memcpy(value, input + idx, valIdx - idx + 1); 2193 | pJsonObjectNode pNode = JSONParseObject(value); 2194 | free(value); 2195 | if (pNode) { 2196 | JSONSetObjectAttr(pObject, key, pNode, BFALSE); 2197 | } 2198 | } 2199 | else { 2200 | // 其他情况,释放键 2201 | free(key); 2202 | break; 2203 | } 2204 | free(key); 2205 | i = valIdx + 1; 2206 | } 2207 | else { 2208 | break; 2209 | } 2210 | } 2211 | } while (0); 2212 | return pObject; 2213 | } 2214 | 2215 | /* 2216 | * json 格式化函数,将 json 字符串转换成 json 对象 2217 | * @param input json 字符串 2218 | * @return pJsonObjectNode 2219 | */ 2220 | pJsonObjectNode JSONParse(char* input /* in */) { 2221 | pJsonObjectNode pObject = NULL; 2222 | do { 2223 | if (!input) { 2224 | break; 2225 | } 2226 | char* str = JSONStringTrim(input); 2227 | if (!str) { 2228 | break; 2229 | } 2230 | if (!JSONStringIsFormat(str)) { 2231 | free(str); 2232 | break; 2233 | } 2234 | 2235 | pObject = JSONParseObject(str); 2236 | free(str); 2237 | } while (0); 2238 | return pObject; 2239 | } 2240 | 2241 | /* 2242 | * json 序列化,将 json 对象转换成 json 字符串 2243 | * @param pNode pJsonNode json 节点 2244 | * @param output char** 接收字符串的指针 2245 | * @return void 2246 | */ 2247 | void JSONStringify(pJsonNode pNode /* in */, char** output /* out */) { 2248 | do { 2249 | if (!pNode) { 2250 | break; 2251 | } 2252 | if (pNode->type == JSONTYPENUMBER) { 2253 | *output = JSONStringifyNumberNode((pJsonNumberNode)pNode, BFALSE); 2254 | } 2255 | else if (pNode->type == JSONTYPELONGNUMBER) { 2256 | *output = JSONStringifyLongNumberNode((pJsonLongNumberNode)pNode, BFALSE); 2257 | } 2258 | else if (pNode->type == JSONTYPESTRING) { 2259 | *output = JSONStringifyStringNode((pJsonStringNode)pNode, BFALSE); 2260 | } 2261 | else if (pNode->type == JSONTYPEARRAY) { 2262 | *output = JSONStringifyArrayNode((pJsonArrayNode)pNode, BFALSE); 2263 | } 2264 | else if (pNode->type == JSONTYPEOBJECT) { 2265 | *output = JSONStringifyObjectNode((pJsonObjectNode)pNode, BFALSE); 2266 | } 2267 | else if (pNode->type == JSONTYPENULL) { 2268 | *output = JSONStringifyNullNode((pJsonNullNode)pNode, BFALSE); 2269 | } 2270 | else if (pNode->type == JSONTYPEBOOLEAN) { 2271 | *output = JSONStringifyBooleanNode((pJsonBooleanNode)pNode, BFALSE); 2272 | } 2273 | } while (0); 2274 | } 2275 | 2276 | /* 2277 | * 数字节点序列化,做数组元素,不需要键名称 2278 | * @param pNode pJsonNumberNode 数字节点 2279 | * @param isContainsKey BOOLEAN 是否包含键 2280 | * @return char* 序列化字符串 2281 | */ 2282 | char* JSONStringifyNumberNode(pJsonNumberNode pNode, BOOLEAN isContainsKey) { 2283 | char* pStr = NULL; 2284 | char buf[64] = { 0 }; 2285 | int num = 0; 2286 | do { 2287 | if (!pNode) { 2288 | break; 2289 | } 2290 | if (pNode->node.type != JSONTYPENUMBER) { 2291 | break; 2292 | } 2293 | num = (int)pNode->value; 2294 | if (num == pNode->value) { 2295 | sprintf(buf, "%d", num); 2296 | } 2297 | else { 2298 | sprintf(buf, "%0.3f", pNode->value); 2299 | for (int i = strlen(buf); i > 0; i--) { 2300 | if (buf[i] == 0 || (buf[i] & 0xFF) == '0') { 2301 | buf[i] = 0; 2302 | } 2303 | else { 2304 | break; 2305 | } 2306 | } 2307 | if (buf[strlen(buf) - 1] == '.') { 2308 | buf[strlen(buf) - 1] = 0; 2309 | } 2310 | } 2311 | if (isContainsKey) { 2312 | if (pNode->node.keyName && strlen(pNode->node.keyName)) { 2313 | pStr = (char*)malloc(strlen(pNode->node.keyName) + strlen(buf) + 4); 2314 | if (!pStr) { 2315 | break; 2316 | } 2317 | memset(pStr, 0, strlen(pNode->node.keyName) + strlen(buf) + 4); 2318 | sprintf(pStr, "\"%s\":", pNode->node.keyName); 2319 | } 2320 | else { 2321 | pStr = (char*)malloc(strlen(buf) + 1); 2322 | if (!pStr) { 2323 | break; 2324 | } 2325 | memset(pStr, 0, strlen(buf) + 1); 2326 | } 2327 | } 2328 | else { 2329 | pStr = (char*)malloc(strlen(buf) + 1); 2330 | if (!pStr) { 2331 | break; 2332 | } 2333 | memset(pStr, 0, strlen(buf) + 1); 2334 | } 2335 | strcat(pStr, buf); 2336 | } while (0); 2337 | return pStr; 2338 | } 2339 | 2340 | /* 2341 | * 数字节点序列化,做数组元素,不需要键名称 2342 | * @param pNode pJsonLongNumberNode 数字节点 2343 | * @param isContainsKey BOOLEAN 是否包含键 2344 | * @return char* 序列化字符串 2345 | */ 2346 | char* JSONStringifyLongNumberNode(pJsonLongNumberNode pNode, BOOLEAN isContainsKey) { 2347 | char* pStr = NULL; 2348 | char buf[64] = { 0 }; 2349 | long long int num = 0; 2350 | do { 2351 | if (!pNode) { 2352 | break; 2353 | } 2354 | if (pNode->node.type != JSONTYPELONGNUMBER) { 2355 | break; 2356 | } 2357 | num = (long long int)pNode->value; 2358 | if (num == pNode->value) { 2359 | sprintf(buf, "%lld", num); 2360 | } 2361 | 2362 | if (isContainsKey) { 2363 | if (pNode->node.keyName && strlen(pNode->node.keyName)) { 2364 | pStr = (char*)malloc(strlen(pNode->node.keyName) + strlen(buf) + 4); 2365 | if (!pStr) { 2366 | break; 2367 | } 2368 | memset(pStr, 0, strlen(pNode->node.keyName) + strlen(buf) + 4); 2369 | sprintf(pStr, "\"%s\":", pNode->node.keyName); 2370 | } 2371 | else { 2372 | pStr = (char*)malloc(strlen(buf) + 1); 2373 | if (!pStr) { 2374 | break; 2375 | } 2376 | memset(pStr, 0, strlen(buf) + 1); 2377 | } 2378 | } 2379 | else { 2380 | pStr = (char*)malloc(strlen(buf) + 1); 2381 | if (!pStr) { 2382 | break; 2383 | } 2384 | memset(pStr, 0, strlen(buf) + 1); 2385 | } 2386 | strcat(pStr, buf); 2387 | } while (0); 2388 | return pStr; 2389 | } 2390 | 2391 | /* 2392 | * 字符串节点序列化,做数组元素,不需要键名称 2393 | * @param pNode pJsonStringNode 字符串节点 2394 | * @param isContainsKey BOOLEAN 是否包含键 2395 | * @return char* 序列化字符串 2396 | */ 2397 | char* JSONStringifyStringNode(pJsonStringNode pNode, BOOLEAN isContainsKey) { 2398 | char* pStr = NULL; 2399 | do { 2400 | if (!pNode) { 2401 | break; 2402 | } 2403 | if (pNode->node.type != JSONTYPESTRING) { 2404 | break; 2405 | } 2406 | if (isContainsKey) { 2407 | if (pNode->node.keyName && strlen(pNode->node.keyName)) { 2408 | if (pNode->value) { 2409 | int len = strlen(pNode->node.keyName) + strlen(pNode->value) + 6; 2410 | pStr = (char*)malloc(len); 2411 | if (!pStr) { 2412 | break; 2413 | } 2414 | memset(pStr, 0, len); 2415 | sprintf(pStr, "\"%s\":\"%s\"", pNode->node.keyName, pNode->value); 2416 | } 2417 | else { 2418 | int len = strlen(pNode->node.keyName) + 8; 2419 | pStr = (char*)malloc(len); 2420 | if (!pStr) { 2421 | break; 2422 | } 2423 | memset(pStr, 0, len); 2424 | sprintf(pStr, "\"%s\":null", pNode->node.keyName); 2425 | } 2426 | } 2427 | else { 2428 | if (pNode->value) { 2429 | int len = strlen(pNode->value) + 3; 2430 | pStr = (char*)malloc(len); 2431 | if (!pStr) { 2432 | break; 2433 | } 2434 | memset(pStr, 0, len); 2435 | sprintf(pStr, "\"%s\"", pNode->value); 2436 | } 2437 | else { 2438 | int len = 5; 2439 | pStr = (char*)malloc(len); 2440 | if (!pStr) { 2441 | break; 2442 | } 2443 | memset(pStr, 0, len); 2444 | sprintf(pStr, "null"); 2445 | } 2446 | } 2447 | } 2448 | else { 2449 | if (pNode->value) { 2450 | int len = strlen(pNode->value) + 3; 2451 | pStr = (char*)malloc(len); 2452 | if (!pStr) { 2453 | break; 2454 | } 2455 | memset(pStr, 0, len); 2456 | sprintf(pStr, "\"%s\"", pNode->value); 2457 | } 2458 | else { 2459 | int len = 5; 2460 | pStr = (char*)malloc(len); 2461 | if (!pStr) { 2462 | break; 2463 | } 2464 | memset(pStr, 0, len); 2465 | sprintf(pStr, "null"); 2466 | } 2467 | } 2468 | } while (0); 2469 | return pStr; 2470 | } 2471 | 2472 | /* 2473 | * 数组节点序列化,做数组元素,不需要键名称 2474 | * @param pNode pJsonArrayNode 数组节点 2475 | * @param isContainsKey BOOLEAN 是否包含键 2476 | * @return char* 序列化字符串 2477 | */ 2478 | char* JSONStringifyArrayNode(pJsonArrayNode pNode, BOOLEAN isContainsKey) { 2479 | char* pStr = NULL; 2480 | char tmpBuf[TMP_BUF_SIZE] = { 0 }; 2481 | do { 2482 | if (!pNode) { 2483 | break; 2484 | } 2485 | if (pNode->node.type != JSONTYPEARRAY) { 2486 | break; 2487 | } 2488 | if (isContainsKey) { 2489 | if (pNode->node.keyName && strlen(pNode->node.keyName)) { 2490 | int len = strlen(pNode->node.keyName) + 5; // 需要存放引号冒号等 2491 | if (len > TMP_BUF_SIZE) { 2492 | // 名称长度过大,需要开辟内存来存放 2493 | pStr = (char*)malloc(len); 2494 | if (!pStr) { 2495 | break; 2496 | } 2497 | memset(pStr, 0, len); 2498 | sprintf(pStr, "\"%s\":[", pNode->node.keyName); 2499 | } 2500 | else { 2501 | sprintf(tmpBuf, "\"%s\":[", pNode->node.keyName); 2502 | } 2503 | } 2504 | else { 2505 | sprintf(tmpBuf, "["); 2506 | } 2507 | } 2508 | else { 2509 | sprintf(tmpBuf, "["); 2510 | } 2511 | // 进行序列化数组元素 2512 | if (pNode->size && pNode->array) { 2513 | for (int i = 0; i < pNode->size; i++) { 2514 | pJsonNode pCur = pNode->array[i]; 2515 | if (pCur) { 2516 | char* pTmpStringify = NULL; 2517 | int tmpLen = 0; 2518 | if (pCur->type == JSONTYPENUMBER) { 2519 | pTmpStringify = JSONStringifyNumberNode((pJsonNumberNode)pCur, BFALSE); 2520 | } 2521 | else if (pCur->type == JSONTYPELONGNUMBER) { 2522 | pTmpStringify = JSONStringifyLongNumberNode((pJsonLongNumberNode)pCur, BFALSE); 2523 | } 2524 | else if (pCur->type == JSONTYPESTRING) { 2525 | pTmpStringify = JSONStringifyStringNode((pJsonStringNode)pCur, BFALSE); 2526 | } 2527 | else if (pCur->type == JSONTYPEARRAY) { 2528 | pTmpStringify = JSONStringifyArrayNode((pJsonArrayNode)pCur, BFALSE); 2529 | } 2530 | else if (pCur->type == JSONTYPEOBJECT) { 2531 | pTmpStringify = JSONStringifyObjectNode((pJsonObjectNode)pCur, BFALSE); 2532 | } 2533 | else if (pCur->type == JSONTYPENULL) { 2534 | pTmpStringify = JSONStringifyNullNode((pJsonNullNode)pCur, BFALSE); 2535 | } 2536 | else if (pCur->type == JSONTYPEBOOLEAN) { 2537 | pTmpStringify = JSONStringifyBooleanNode((pJsonBooleanNode)pCur, BFALSE); 2538 | } 2539 | if (pTmpStringify) { 2540 | tmpLen = strlen(pTmpStringify); 2541 | tmpLen += strlen(tmpBuf) + 1; 2542 | if (tmpLen >= TMP_BUF_SIZE) { 2543 | // 将序列数据放入结果区,并把临时缓冲区清空 2544 | // 重新分配内存存放数据 2545 | char* _pTmp = pStr; 2546 | int _tmpLen = 0; 2547 | if (_pTmp) { 2548 | _tmpLen = strlen(_pTmp); 2549 | } 2550 | _tmpLen += strlen(tmpBuf) + strlen(pTmpStringify) + 2; 2551 | pStr = (char*)malloc(_tmpLen); 2552 | if (!pStr) { 2553 | free(_pTmp); 2554 | break; 2555 | } 2556 | memset(pStr, 0, _tmpLen); 2557 | if (_pTmp) { 2558 | strcat(pStr, _pTmp); 2559 | free(_pTmp); 2560 | _pTmp = NULL; 2561 | } 2562 | strcat(pStr, tmpBuf); 2563 | strcat(pStr, pTmpStringify); 2564 | strcat(pStr, ","); 2565 | memset(tmpBuf, 0, TMP_BUF_SIZE); 2566 | } 2567 | else { 2568 | // 将数据存入缓冲区 2569 | strcat(tmpBuf, pTmpStringify); 2570 | strcat(tmpBuf, ","); 2571 | } 2572 | free(pTmpStringify); 2573 | pTmpStringify = NULL; 2574 | } 2575 | } 2576 | } 2577 | } 2578 | // 追加结尾 2579 | if (strlen(tmpBuf)) { 2580 | char* p = pStr; 2581 | int tLen = 0; 2582 | if (p) { 2583 | tLen = strlen(p); 2584 | } 2585 | tLen += strlen(tmpBuf) + 2; 2586 | pStr = malloc(tLen); 2587 | if (!pStr) { 2588 | free(p); 2589 | break; 2590 | } 2591 | memset(pStr, 0, tLen); 2592 | if (p) { 2593 | strcat(pStr, p); 2594 | free(p); 2595 | } 2596 | strcat(pStr, tmpBuf); 2597 | if (pStr[strlen(pStr) - 1] == ',') { 2598 | pStr[strlen(pStr) - 1] = ']'; 2599 | } 2600 | else { 2601 | pStr[strlen(pStr)] = ']'; 2602 | } 2603 | 2604 | memset(tmpBuf, 0, TMP_BUF_SIZE); 2605 | } 2606 | else { 2607 | if (strlen(pStr)) { 2608 | 2609 | if (pStr[strlen(pStr) - 1] == ',') { 2610 | pStr[strlen(pStr) - 1] = ']'; 2611 | } 2612 | else { 2613 | char* p = pStr; 2614 | int tLen = 0; 2615 | tLen += strlen(p) + 2; 2616 | pStr = malloc(tLen); 2617 | if (!pStr) { 2618 | free(p); 2619 | break; 2620 | } 2621 | memset(pStr, 0, tLen); 2622 | strcat(pStr, p); 2623 | free(p); 2624 | pStr[strlen(pStr)] = ']'; 2625 | memset(tmpBuf, 0, TMP_BUF_SIZE); 2626 | } 2627 | } 2628 | } 2629 | 2630 | } while (0); 2631 | return pStr; 2632 | } 2633 | 2634 | /* 2635 | * 对象节点序列化,做数组元素,不需要键名称 2636 | * @param pNode pJsonObjectNode 对象节点 2637 | * @param isContainsKey BOOLEAN 是否包含键 2638 | * @return char* 序列化字符串 2639 | */ 2640 | char* JSONStringifyObjectNode(pJsonObjectNode pNode, BOOLEAN isContainsKey) { 2641 | char* pStr = NULL; 2642 | char tmpBuf[TMP_BUF_SIZE] = { 0 }; 2643 | do { 2644 | if (!pNode) { 2645 | break; 2646 | } 2647 | if (pNode->node.type != JSONTYPEOBJECT) { 2648 | break; 2649 | } 2650 | if (isContainsKey) { 2651 | if (pNode->node.keyName && strlen(pNode->node.keyName)) { 2652 | int len = strlen(pNode->node.keyName) + 5; // 需要存放引号冒号等 2653 | if (len > TMP_BUF_SIZE) { 2654 | // 名称长度过大,需要开辟内存来存放 2655 | pStr = (char*)malloc(len); 2656 | if (!pStr) { 2657 | break; 2658 | } 2659 | memset(pStr, 0, len); 2660 | sprintf(pStr, "\"%s\":{", pNode->node.keyName); 2661 | } 2662 | else { 2663 | sprintf(tmpBuf, "\"%s\":{", pNode->node.keyName); 2664 | } 2665 | } 2666 | else { 2667 | sprintf(tmpBuf, "{"); 2668 | } 2669 | } 2670 | else { 2671 | sprintf(tmpBuf, "{"); 2672 | } 2673 | // 进行序列化属性节点 2674 | for (int i = 0; i < HASH_MAX; i++) { 2675 | if (pNode->table[i]) { 2676 | pJsonNode pCur = pNode->table[i]; 2677 | while (pCur) { 2678 | char* pTmpStringify = NULL; 2679 | int tmpLen = 0; 2680 | if (pCur->type == JSONTYPENUMBER) { 2681 | pTmpStringify = JSONStringifyNumberNode((pJsonNumberNode)pCur, BTRUE); 2682 | } 2683 | else if (pCur->type == JSONTYPELONGNUMBER) { 2684 | pTmpStringify = JSONStringifyLongNumberNode((pJsonLongNumberNode)pCur, BTRUE); 2685 | } 2686 | else if (pCur->type == JSONTYPESTRING) { 2687 | pTmpStringify = JSONStringifyStringNode((pJsonStringNode)pCur, BTRUE); 2688 | } 2689 | else if (pCur->type == JSONTYPEARRAY) { 2690 | pTmpStringify = JSONStringifyArrayNode((pJsonArrayNode)pCur, BTRUE); 2691 | } 2692 | else if (pCur->type == JSONTYPEOBJECT) { 2693 | pTmpStringify = JSONStringifyObjectNode((pJsonObjectNode)pCur, BTRUE); 2694 | } 2695 | else if (pCur->type == JSONTYPENULL) { 2696 | pTmpStringify = JSONStringifyNullNode((pJsonNullNode)pCur, BTRUE); 2697 | } 2698 | else if (pCur->type == JSONTYPEBOOLEAN) { 2699 | pTmpStringify = JSONStringifyBooleanNode((pJsonBooleanNode)pCur, BTRUE); 2700 | } 2701 | if (pTmpStringify) { 2702 | tmpLen = strlen(pTmpStringify); 2703 | tmpLen += strlen(tmpBuf) + 1; 2704 | if (tmpLen >= TMP_BUF_SIZE) { 2705 | // 将序列数据放入结果区,并把临时缓冲区清空 2706 | // 重新分配内存存放数据 2707 | char* _pTmp = pStr; 2708 | int _tmpLen = 0; 2709 | if (_pTmp) { 2710 | _tmpLen = strlen(_pTmp); 2711 | } 2712 | _tmpLen += strlen(tmpBuf) + strlen(pTmpStringify) + 2; 2713 | pStr = (char*)malloc(_tmpLen); 2714 | if (!pStr) { 2715 | free(_pTmp); 2716 | break; 2717 | } 2718 | memset(pStr, 0, _tmpLen); 2719 | if (_pTmp) { 2720 | strcat(pStr, _pTmp); 2721 | free(_pTmp); 2722 | _pTmp = NULL; 2723 | } 2724 | strcat(pStr, tmpBuf); 2725 | strcat(pStr, pTmpStringify); 2726 | strcat(pStr, ","); 2727 | memset(tmpBuf, 0, TMP_BUF_SIZE); 2728 | } 2729 | else { 2730 | // 将数据存入缓冲区 2731 | strcat(tmpBuf, pTmpStringify); 2732 | strcat(tmpBuf, ","); 2733 | } 2734 | free(pTmpStringify); 2735 | pTmpStringify = NULL; 2736 | } 2737 | pCur = pCur->next; 2738 | } 2739 | } 2740 | } 2741 | // 追加结尾 2742 | if (strlen(tmpBuf)) { 2743 | char* p = pStr; 2744 | int tLen = 0; 2745 | if (p) { 2746 | tLen = strlen(p); 2747 | } 2748 | tLen += strlen(tmpBuf) + 2; 2749 | pStr = malloc(tLen); 2750 | if (!pStr) { 2751 | free(p); 2752 | break; 2753 | } 2754 | memset(pStr, 0, tLen); 2755 | if (p) { 2756 | strcat(pStr, p); 2757 | free(p); 2758 | } 2759 | strcat(pStr, tmpBuf); 2760 | if (pStr[strlen(pStr) - 1] == ',') { 2761 | pStr[strlen(pStr) - 1] = '}'; 2762 | } 2763 | else { 2764 | pStr[strlen(pStr)] = '}'; 2765 | } 2766 | 2767 | memset(tmpBuf, 0, TMP_BUF_SIZE); 2768 | } 2769 | else { 2770 | if (strlen(pStr)) { 2771 | 2772 | if (pStr[strlen(pStr) - 1] == ',') { 2773 | pStr[strlen(pStr) - 1] = '}'; 2774 | } 2775 | else { 2776 | char* p = pStr; 2777 | int tLen = 0; 2778 | tLen += strlen(p) + 2; 2779 | pStr = malloc(tLen); 2780 | if (!pStr) { 2781 | free(p); 2782 | break; 2783 | } 2784 | memset(pStr, 0, tLen); 2785 | strcat(pStr, p); 2786 | free(p); 2787 | pStr[strlen(pStr)] = '}'; 2788 | memset(tmpBuf, 0, TMP_BUF_SIZE); 2789 | } 2790 | } 2791 | } 2792 | 2793 | } while (0); 2794 | return pStr; 2795 | } 2796 | 2797 | /* 2798 | * null 节点序列化,做数组元素,不需要键名称 2799 | * @param pNode pJsonNullNode null节点 2800 | * @param isContainsKey BOOLEAN 是否包含键 2801 | * @return char* 序列化字符串 2802 | */ 2803 | char* JSONStringifyNullNode(pJsonNullNode pNode, BOOLEAN isContainsKey) { 2804 | char* pStr = NULL; 2805 | do { 2806 | if (!pNode) { 2807 | break; 2808 | } 2809 | if (pNode->node.type != JSONTYPENULL) { 2810 | break; 2811 | } 2812 | if (isContainsKey) { 2813 | if (pNode->node.keyName && strlen(pNode->node.keyName)) { 2814 | int len = strlen(pNode->node.keyName) + 8; 2815 | pStr = (char*)malloc(len); 2816 | if (!pStr) { 2817 | break; 2818 | } 2819 | memset(pStr, 0, len); 2820 | sprintf(pStr, "\"%s\":", pNode->node.keyName); 2821 | } 2822 | else { 2823 | pStr = (char*)malloc(5); 2824 | if (!pStr) { 2825 | break; 2826 | } 2827 | memset(pStr, 0, 5); 2828 | } 2829 | } 2830 | else { 2831 | pStr = (char*)malloc(5); 2832 | if (!pStr) { 2833 | break; 2834 | } 2835 | memset(pStr, 0, 5); 2836 | } 2837 | strcat(pStr, "null"); 2838 | } while (0); 2839 | return pStr; 2840 | } 2841 | 2842 | /* 2843 | * Boolean 节点序列化,做数组元素,不需要键名称 2844 | * @param pNode pJsonBooleanNode Boolean节点 2845 | * @param isContainsKey BOOLEAN 是否包含键 2846 | * @return char* 序列化字符串 2847 | */ 2848 | char* JSONStringifyBooleanNode(pJsonBooleanNode pNode, BOOLEAN isContainsKey) { 2849 | char* pStr = NULL; 2850 | do { 2851 | if (!pNode) { 2852 | break; 2853 | } 2854 | if (pNode->node.type != JSONTYPEBOOLEAN) { 2855 | break; 2856 | } 2857 | if (isContainsKey) { 2858 | if (pNode->node.keyName && strlen(pNode->node.keyName)) { 2859 | int len = strlen(pNode->node.keyName) + 9; 2860 | pStr = (char*)malloc(len); 2861 | if (!pStr) { 2862 | break; 2863 | } 2864 | memset(pStr, 0, len); 2865 | sprintf(pStr, "\"%s\":", pNode->node.keyName); 2866 | } 2867 | else { 2868 | pStr = (char*)malloc(6); 2869 | if (!pStr) { 2870 | break; 2871 | } 2872 | memset(pStr, 0, 6); 2873 | } 2874 | } 2875 | else { 2876 | pStr = (char*)malloc(6); 2877 | if (!pStr) { 2878 | break; 2879 | } 2880 | memset(pStr, 0, 6); 2881 | } 2882 | 2883 | if (pNode->value) { 2884 | strcat(pStr, "true"); 2885 | } 2886 | else { 2887 | strcat(pStr, "false"); 2888 | } 2889 | } while (0); 2890 | return pStr; 2891 | } 2892 | 2893 | /* 2894 | * 判断 json 是否包含属性 2895 | * @param pObject json 对象 2896 | * @param key 属性名称 2897 | * @return BOOLEAN 是否包含属性 2898 | */ 2899 | BOOLEAN JSONIsContainsAttr(pJsonObjectNode pObject /* in */, char* key /* in */) { 2900 | BOOLEAN res = BFALSE; 2901 | do { 2902 | if (!pObject) { 2903 | break; 2904 | } 2905 | if (pObject->node.type != JSONTYPEOBJECT) { 2906 | break; 2907 | } 2908 | if (!key || !strlen(key)) { 2909 | break; 2910 | } 2911 | int idx = JSONGetHashIndex(key); 2912 | if (idx < 0 || idx >= HASH_MAX) { 2913 | break; 2914 | } 2915 | if (pObject->table[idx]) { 2916 | // 进行链式遍历 2917 | pJsonNode pNode = pObject->table[idx]; 2918 | do { 2919 | if (strcmp(key, pNode->keyName) == 0) { 2920 | res = BTRUE; 2921 | break; 2922 | } 2923 | pNode = pNode->next; 2924 | } while (pNode); 2925 | } 2926 | } while (0); 2927 | return res; 2928 | } 2929 | 2930 | /* 2931 | * 向 json 对象中添加 Number类型 键值对象 2932 | * @param pObject json 对象 2933 | * @param key 属性名称 2934 | * @param value 属性值 2935 | */ 2936 | void JSONSetNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, double value /* in */) { 2937 | int idx = -1; 2938 | do { 2939 | if (!pObject) { 2940 | break; 2941 | } 2942 | if (pObject->node.type != JSONTYPEOBJECT) { 2943 | break; 2944 | } 2945 | if (!key) { 2946 | break; 2947 | } 2948 | idx = JSONGetHashIndex(key); 2949 | if (idx == -1) { 2950 | break; 2951 | } 2952 | if (!pObject->table[idx]) { 2953 | pJsonNumberNode pNode = JSONCreateNumberNode(key, value); 2954 | if (!pNode) { 2955 | // Number节点内存分配失败 2956 | break; 2957 | } 2958 | pObject->table[idx] = (pJsonNode)pNode; 2959 | } 2960 | else { 2961 | pJsonNode pPre = NULL; 2962 | pJsonNode pCur = pObject->table[idx]; 2963 | pJsonNode pNext = pObject->table[idx]->next; 2964 | do { 2965 | if (strcmp(key, pCur->keyName) == 0) { 2966 | if (pCur->type == JSONTYPENUMBER) { 2967 | ((pJsonNumberNode)pCur)->value = value; 2968 | break; 2969 | } 2970 | else if (pCur->type == JSONTYPELONGNUMBER) { 2971 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pCur); 2972 | } 2973 | else if (pCur->type == JSONTYPESTRING) { 2974 | JSONDestroyStringNode((pJsonStringNode)pCur); 2975 | } 2976 | else if (pCur->type == JSONTYPEARRAY) { 2977 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 2978 | } 2979 | else if (pCur->type == JSONTYPEOBJECT) { 2980 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 2981 | } 2982 | else if (pCur->type == JSONTYPENULL) { 2983 | JSONDestroyNullNode((pJsonNullNode)pCur); 2984 | } 2985 | else if (pCur->type == JSONTYPEBOOLEAN) { 2986 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 2987 | } 2988 | else { 2989 | break; 2990 | } 2991 | pJsonNumberNode pNode = JSONCreateNumberNode(key, value); 2992 | if (!pNode) { 2993 | // Number节点内存分配失败 2994 | break; 2995 | } 2996 | pNode->node.next = pNext; 2997 | if (!pPre) { 2998 | pObject->table[idx] = (pJsonNode)pNode; 2999 | } 3000 | else { 3001 | pPre->next = (pJsonNode)pNode; 3002 | } 3003 | break; 3004 | } 3005 | if (pCur && !pCur->next) { 3006 | pJsonNumberNode pNode = JSONCreateNumberNode(key, value); 3007 | if (!pNode) { 3008 | // Number节点内存分配失败 3009 | break; 3010 | } 3011 | pNode->node.next = pNext; 3012 | pCur->next = (pJsonNode)pNode; 3013 | break; 3014 | } 3015 | pPre = pCur; 3016 | pCur = pNext; 3017 | pNext = pNext->next; 3018 | } while (pCur); 3019 | } 3020 | } while (0); 3021 | } 3022 | 3023 | /* 3024 | * 向 json 对象中添加 Long Number类型 键值对象 3025 | * @param pObject json 对象 3026 | * @param key 属性名称 3027 | * @param value 属性值 3028 | * @return void 3029 | */ 3030 | void JSONSetLongNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, UINT64 value /* in */){ 3031 | int idx = -1; 3032 | do { 3033 | if (!pObject) { 3034 | break; 3035 | } 3036 | if (pObject->node.type != JSONTYPEOBJECT) { 3037 | break; 3038 | } 3039 | if (!key) { 3040 | break; 3041 | } 3042 | idx = JSONGetHashIndex(key); 3043 | if (idx == -1) { 3044 | break; 3045 | } 3046 | if (!pObject->table[idx]) { 3047 | pJsonLongNumberNode pNode = JSONCreateLongNumberNode(key, value); 3048 | if (!pNode) { 3049 | // Number节点内存分配失败 3050 | break; 3051 | } 3052 | pObject->table[idx] = (pJsonNode)pNode; 3053 | } 3054 | else { 3055 | pJsonNode pPre = NULL; 3056 | pJsonNode pCur = pObject->table[idx]; 3057 | pJsonNode pNext = pObject->table[idx]->next; 3058 | do { 3059 | if (strcmp(key, pCur->keyName) == 0) { 3060 | if (pCur->type == JSONTYPELONGNUMBER) { 3061 | ((pJsonLongNumberNode)pCur)->value = value; 3062 | break; 3063 | } 3064 | else if (pCur->type == JSONTYPENUMBER) { 3065 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3066 | } 3067 | else if (pCur->type == JSONTYPESTRING) { 3068 | JSONDestroyStringNode((pJsonStringNode)pCur); 3069 | } 3070 | else if (pCur->type == JSONTYPEARRAY) { 3071 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3072 | } 3073 | else if (pCur->type == JSONTYPEOBJECT) { 3074 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3075 | } 3076 | else if (pCur->type == JSONTYPENULL) { 3077 | JSONDestroyNullNode((pJsonNullNode)pCur); 3078 | } 3079 | else if (pCur->type == JSONTYPEBOOLEAN) { 3080 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 3081 | } 3082 | else { 3083 | break; 3084 | } 3085 | pJsonLongNumberNode pNode = JSONCreateLongNumberNode(key, value); 3086 | if (!pNode) { 3087 | // Number节点内存分配失败 3088 | break; 3089 | } 3090 | pNode->node.next = pNext; 3091 | if (!pPre) { 3092 | pObject->table[idx] = (pJsonNode)pNode; 3093 | } 3094 | else { 3095 | pPre->next = (pJsonNode)pNode; 3096 | } 3097 | break; 3098 | } 3099 | if (pCur && !pCur->next) { 3100 | pJsonLongNumberNode pNode = JSONCreateLongNumberNode(key, value); 3101 | if (!pNode) { 3102 | // Number节点内存分配失败 3103 | break; 3104 | } 3105 | pNode->node.next = pNext; 3106 | pCur->next = (pJsonNode)pNode; 3107 | break; 3108 | } 3109 | pPre = pCur; 3110 | pCur = pNext; 3111 | pNext = pNext->next; 3112 | } while (pCur); 3113 | } 3114 | } while (0); 3115 | } 3116 | 3117 | /* 3118 | * 向 json 对象中添加 String类型 键值对象 3119 | * @param pObject json 对象 3120 | * @param key 属性名称 3121 | * @param value 属性值 3122 | */ 3123 | void JSONSetStringAttr(pJsonObjectNode pObject /* in */, char* key /* in */, char* value /* in */) { 3124 | int idx = -1; 3125 | do { 3126 | if (!pObject) { 3127 | break; 3128 | } 3129 | if (pObject->node.type != JSONTYPEOBJECT) { 3130 | break; 3131 | } 3132 | if (!key) { 3133 | break; 3134 | } 3135 | if (!value) { 3136 | break; 3137 | } 3138 | idx = JSONGetHashIndex(key); 3139 | if (idx == -1) { 3140 | break; 3141 | } 3142 | if (!pObject->table[idx]) { 3143 | pJsonStringNode pNode = JSONCreateStringNode(key, value); 3144 | if (!pNode) { 3145 | // String节点内存分配失败 3146 | break; 3147 | } 3148 | pObject->table[idx] = (pJsonNode)pNode; 3149 | } 3150 | else { 3151 | pJsonNode pPre = NULL; 3152 | pJsonNode pCur = pObject->table[idx]; 3153 | pJsonNode pNext = pObject->table[idx]->next; 3154 | do { 3155 | if (strcmp(key, pCur->keyName) == 0) { 3156 | if (pCur->type == JSONTYPENUMBER) { 3157 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3158 | } 3159 | else if (pCur->type == JSONTYPELONGNUMBER) { 3160 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pCur); 3161 | } 3162 | else if (pCur->type == JSONTYPESTRING) { 3163 | if (((pJsonStringNode)pCur)->value) { 3164 | free(((pJsonStringNode)pCur)->value); 3165 | } 3166 | ((pJsonStringNode)pCur)->value = deepCloneString(value); 3167 | if (!((pJsonStringNode)pCur)->value) { 3168 | // String节点值内存分配失败 3169 | break; 3170 | } 3171 | break; 3172 | } 3173 | else if (pCur->type == JSONTYPEARRAY) { 3174 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3175 | } 3176 | else if (pCur->type == JSONTYPEOBJECT) { 3177 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3178 | } 3179 | else if (pCur->type == JSONTYPENULL) { 3180 | JSONDestroyNullNode((pJsonNullNode)pCur); 3181 | } 3182 | else if (pCur->type == JSONTYPEBOOLEAN) { 3183 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 3184 | } 3185 | else { 3186 | break; 3187 | } 3188 | pJsonStringNode pNode = JSONCreateStringNode(key, value); 3189 | if (!pNode) { 3190 | // String节点内存分配失败 3191 | break; 3192 | } 3193 | pNode->node.next = pNext; 3194 | if (!pPre) { 3195 | pObject->table[idx] = (pJsonNode)pNode; 3196 | } 3197 | else { 3198 | pPre->next = (pJsonNode)pNode; 3199 | } 3200 | break; 3201 | } 3202 | if (pCur && !pCur->next) { 3203 | pJsonStringNode pNode = JSONCreateStringNode(key, value); 3204 | if (!pNode) { 3205 | // String节点内存分配失败 3206 | break; 3207 | } 3208 | pNode->node.next = pNext; 3209 | pCur->next = (pJsonNode)pNode; 3210 | break; 3211 | } 3212 | pPre = pCur; 3213 | pCur = pNext; 3214 | pNext = pNext->next; 3215 | } while (pCur); 3216 | } 3217 | } while (0); 3218 | } 3219 | 3220 | /* 3221 | * 向 json 对象中添加 Array类型 键值对象,添加的数组为空数组 3222 | * @param pObject json 对象 3223 | * @param key 属性名称 3224 | */ 3225 | void JSONSetEmptyArrayAttr(pJsonObjectNode pObject /* in */, char* key /* in */) { 3226 | int idx = -1; 3227 | do { 3228 | if (!pObject) { 3229 | break; 3230 | } 3231 | if (pObject->node.type != JSONTYPEOBJECT) { 3232 | break; 3233 | } 3234 | if (!key) { 3235 | break; 3236 | } 3237 | idx = JSONGetHashIndex(key); 3238 | if (idx == -1) { 3239 | break; 3240 | } 3241 | if (!pObject->table[idx]) { 3242 | pJsonArrayNode pNode = JSONCreateArrayNode(key); 3243 | if (!pNode) { 3244 | // Array节点内存分配失败 3245 | break; 3246 | } 3247 | pObject->table[idx] = (pJsonNode)pNode; 3248 | } 3249 | else { 3250 | pJsonNode pPre = NULL; 3251 | pJsonNode pCur = pObject->table[idx]; 3252 | pJsonNode pNext = pObject->table[idx]->next; 3253 | do { 3254 | if (strcmp(key, pCur->keyName) == 0) { 3255 | if (pCur->type == JSONTYPENUMBER) { 3256 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3257 | } 3258 | else if (pCur->type == JSONTYPELONGNUMBER) { 3259 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pCur); 3260 | } 3261 | else if (pCur->type == JSONTYPESTRING) { 3262 | JSONDestroyStringNode((pJsonStringNode)pCur); 3263 | } 3264 | else if (pCur->type == JSONTYPEARRAY) { 3265 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3266 | } 3267 | else if (pCur->type == JSONTYPEOBJECT) { 3268 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3269 | } 3270 | else if (pCur->type == JSONTYPENULL) { 3271 | JSONDestroyNullNode((pJsonNullNode)pCur); 3272 | } 3273 | else if (pCur->type == JSONTYPEBOOLEAN) { 3274 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 3275 | } 3276 | else { 3277 | break; 3278 | } 3279 | pJsonArrayNode pNode = JSONCreateArrayNode(key); 3280 | if (!pNode) { 3281 | // Array节点内存分配失败 3282 | break; 3283 | } 3284 | pNode->node.next = pNext; 3285 | if (!pPre) { 3286 | pObject->table[idx] = (pJsonNode)pNode; 3287 | } 3288 | else { 3289 | pPre->next = (pJsonNode)pNode; 3290 | } 3291 | break; 3292 | } 3293 | if (pCur && !pCur->next) { 3294 | pJsonArrayNode pNode = JSONCreateArrayNode(key); 3295 | if (!pNode) { 3296 | // Array节点内存分配失败 3297 | break; 3298 | } 3299 | pNode->node.next = pNext; 3300 | pCur->next = (pJsonNode)pNode; 3301 | break; 3302 | } 3303 | pPre = pCur; 3304 | pCur = pNext; 3305 | pNext = pNext->next; 3306 | } while (pCur); 3307 | } 3308 | } while (0); 3309 | } 3310 | 3311 | /* 3312 | * 向 json 对象中添加 Array类型 键值对象 3313 | * @param pObject json 对象 3314 | * @param key 属性名称 3315 | * @param value pJsonArrayNode 数组节点 3316 | * @param isDeepClone BOOLEAN 是否进行深度拷贝 3317 | * @return void 3318 | */ 3319 | void JSONSetArrayAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonArrayNode pArray, BOOLEAN isDeepClone) { 3320 | int idx = -1; 3321 | do { 3322 | if (!pObject) { 3323 | break; 3324 | } 3325 | if (pObject->node.type != JSONTYPEOBJECT) { 3326 | break; 3327 | } 3328 | if (!key) { 3329 | break; 3330 | } 3331 | if (!pArray) { 3332 | break; 3333 | } 3334 | if (pArray->node.type != JSONTYPEARRAY) { 3335 | break; 3336 | } 3337 | idx = JSONGetHashIndex(key); 3338 | if (idx == -1) { 3339 | break; 3340 | } 3341 | if (!pObject->table[idx]) { 3342 | pJsonArrayNode pNode = NULL; 3343 | if (isDeepClone) { 3344 | pNode = JSONArrayDeepClone(pArray); 3345 | } 3346 | else { 3347 | pNode = pArray; 3348 | } 3349 | if (!pNode) { 3350 | // Array节点内存分配失败 3351 | break; 3352 | } 3353 | // 重新对数组进行名称赋值 3354 | if (pNode->node.keyName) { 3355 | free(pNode->node.keyName); 3356 | } 3357 | pNode->node.keyName = deepCloneString(key); 3358 | if (!pNode->node.keyName) { 3359 | JSONDestroyArrayNode(pNode); 3360 | break; 3361 | } 3362 | pObject->table[idx] = (pJsonNode)pNode; 3363 | } 3364 | else { 3365 | pJsonNode pPre = NULL; 3366 | pJsonNode pCur = pObject->table[idx]; 3367 | pJsonNode pNext = pObject->table[idx]->next; 3368 | do { 3369 | if (strcmp(key, pCur->keyName) == 0) { 3370 | if (pCur->type == JSONTYPENUMBER) { 3371 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3372 | } 3373 | else if (pCur->type == JSONTYPELONGNUMBER) { 3374 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pCur); 3375 | } 3376 | else if (pCur->type == JSONTYPESTRING) { 3377 | JSONDestroyStringNode((pJsonStringNode)pCur); 3378 | } 3379 | else if (pCur->type == JSONTYPEARRAY) { 3380 | if (pCur == (pJsonNode)pArray) { 3381 | break; 3382 | } 3383 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3384 | } 3385 | else if (pCur->type == JSONTYPEOBJECT) { 3386 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3387 | } 3388 | else if (pCur->type == JSONTYPENULL) { 3389 | JSONDestroyNullNode((pJsonNullNode)pCur); 3390 | } 3391 | else if (pCur->type == JSONTYPEBOOLEAN) { 3392 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 3393 | } 3394 | else { 3395 | break; 3396 | } 3397 | pJsonArrayNode pNode = NULL; 3398 | if (isDeepClone) { 3399 | pNode = JSONArrayDeepClone(pArray); 3400 | } 3401 | else { 3402 | pNode = pArray; 3403 | } 3404 | if (!pNode) { 3405 | // Array节点内存分配失败 3406 | break; 3407 | } 3408 | // 重新对数组进行名称赋值 3409 | if (pNode->node.keyName) { 3410 | free(pNode->node.keyName); 3411 | } 3412 | pNode->node.keyName = deepCloneString(key); 3413 | if (!pNode->node.keyName) { 3414 | JSONDestroyArrayNode(pNode); 3415 | break; 3416 | } 3417 | pNode->node.next = pNext; 3418 | if (!pPre) { 3419 | pObject->table[idx] = (pJsonNode)pNode; 3420 | } 3421 | else { 3422 | pPre->next = (pJsonNode)pNode; 3423 | } 3424 | break; 3425 | } 3426 | if (pCur && !pCur->next) { 3427 | pJsonArrayNode pNode = NULL; 3428 | if (isDeepClone) { 3429 | pNode = JSONArrayDeepClone(pArray); 3430 | } 3431 | else { 3432 | pNode = pArray; 3433 | } 3434 | if (!pNode) { 3435 | // Array节点内存分配失败 3436 | break; 3437 | } 3438 | // 重新对数组进行名称赋值 3439 | if (pNode->node.keyName) { 3440 | free(pNode->node.keyName); 3441 | } 3442 | pNode->node.keyName = deepCloneString(key); 3443 | if (!pNode->node.keyName) { 3444 | JSONDestroyArrayNode(pNode); 3445 | break; 3446 | } 3447 | pNode->node.next = pNext; 3448 | pCur->next = (pJsonNode)pNode; 3449 | break; 3450 | } 3451 | pPre = pCur; 3452 | pCur = pNext; 3453 | pNext = pNext->next; 3454 | } while (pCur); 3455 | } 3456 | } while (0); 3457 | } 3458 | 3459 | /* 3460 | * 向 json 对象中添加 Object类型 键值对象 3461 | * @param pObject json 对象 3462 | * @param key 属性名称 3463 | * @param value 属性值 3464 | * @param isDeepClone BOOLEAN 是否进行深度拷贝 3465 | * @return void 3466 | */ 3467 | void JSONSetObjectAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonObjectNode value /* in */, BOOLEAN isDeepClone) { 3468 | int idx = -1; 3469 | do { 3470 | if (!pObject) { 3471 | break; 3472 | } 3473 | if (pObject->node.type != JSONTYPEOBJECT) { 3474 | break; 3475 | } 3476 | if (!key) { 3477 | break; 3478 | } 3479 | // 自己不能作为自己的子节点 3480 | if (pObject == value) { 3481 | break; 3482 | } 3483 | if (value->node.type != JSONTYPEOBJECT) { 3484 | break; 3485 | } 3486 | idx = JSONGetHashIndex(key); 3487 | if (idx == -1) { 3488 | break; 3489 | } 3490 | if (!pObject->table[idx]) { 3491 | pJsonObjectNode pNode = NULL; 3492 | if (isDeepClone) { 3493 | pNode = JSONObjectDeepClone(value); 3494 | } 3495 | else { 3496 | pNode = value; 3497 | } 3498 | if (!pNode) { 3499 | break; 3500 | } 3501 | pNode->node.keyName = deepCloneString(key); 3502 | if (!pNode->node.keyName) { 3503 | JSONDestroyObjectNode(pNode); 3504 | break; 3505 | } 3506 | pObject->table[idx] = (pJsonNode)pNode; 3507 | } 3508 | else { 3509 | pJsonNode pPre = NULL; 3510 | pJsonNode pCur = pObject->table[idx]; 3511 | pJsonNode pNext = pObject->table[idx]->next; 3512 | do { 3513 | if (strcmp(key, pCur->keyName) == 0) { 3514 | if (pCur->type == JSONTYPENUMBER) { 3515 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3516 | } 3517 | else if (pCur->type == JSONTYPELONGNUMBER) { 3518 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pCur); 3519 | } 3520 | else if (pCur->type == JSONTYPESTRING) { 3521 | JSONDestroyStringNode((pJsonStringNode)pCur); 3522 | } 3523 | else if (pCur->type == JSONTYPEARRAY) { 3524 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3525 | } 3526 | else if (pCur->type == JSONTYPEOBJECT) { 3527 | // 节点已存在,不需要重复添加 3528 | if (pCur == (pJsonNode)value) { 3529 | break; 3530 | } 3531 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3532 | } 3533 | else if (pCur->type == JSONTYPENULL) { 3534 | JSONDestroyNullNode((pJsonNullNode)pCur); 3535 | } 3536 | else if (pCur->type == JSONTYPEBOOLEAN) { 3537 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 3538 | } 3539 | else { 3540 | break; 3541 | } 3542 | pJsonObjectNode pNode = NULL; 3543 | if (isDeepClone) { 3544 | pNode = JSONObjectDeepClone((pJsonObjectNode)value); 3545 | } 3546 | else { 3547 | pNode = value; 3548 | } 3549 | if (!pNode) { 3550 | // Object节点内存分配失败 3551 | break; 3552 | } 3553 | // 重新对Object进行名称赋值 3554 | if (pNode->node.keyName) { 3555 | free(pNode->node.keyName); 3556 | } 3557 | pNode->node.keyName = deepCloneString(key); 3558 | if (!pNode->node.keyName) { 3559 | JSONDestroyObjectNode(pNode); 3560 | break; 3561 | } 3562 | pNode->node.next = pNext; 3563 | if (!pPre) { 3564 | pObject->table[idx] = (pJsonNode)pNode; 3565 | } 3566 | else { 3567 | pPre->next = (pJsonNode)pNode; 3568 | } 3569 | break; 3570 | } 3571 | if (pCur && !pCur->next) { 3572 | pJsonObjectNode pNode = NULL; 3573 | if (isDeepClone) { 3574 | pNode = JSONObjectDeepClone(value); 3575 | } 3576 | else { 3577 | pNode = value; 3578 | } 3579 | if (!pNode) { 3580 | // Object节点内存分配失败 3581 | break; 3582 | } 3583 | // 重新对数组进行名称赋值 3584 | if (pNode->node.keyName) { 3585 | free(pNode->node.keyName); 3586 | } 3587 | pNode->node.keyName = deepCloneString(key); 3588 | if (!pNode->node.keyName) { 3589 | JSONDestroyObjectNode(pNode); 3590 | break; 3591 | } 3592 | pNode->node.next = pNext; 3593 | pCur->next = (pJsonNode)pNode; 3594 | break; 3595 | } 3596 | pPre = pCur; 3597 | pCur = pNext; 3598 | pNext = pNext->next; 3599 | } while (pCur); 3600 | } 3601 | } while (0); 3602 | } 3603 | 3604 | /* 3605 | * 向 json 对象中添加 null类型 键值对象 3606 | * @param pObject json 对象 3607 | * @param key 属性名称 3608 | * @return void 3609 | */ 3610 | void JSONSetNullAttr(pJsonObjectNode pObject /* in */, char* key /* in */) { 3611 | int idx = -1; 3612 | do { 3613 | if (!pObject) { 3614 | break; 3615 | } 3616 | if (pObject->node.type != JSONTYPEOBJECT) { 3617 | break; 3618 | } 3619 | if (!key) { 3620 | break; 3621 | } 3622 | idx = JSONGetHashIndex(key); 3623 | if (idx == -1) { 3624 | break; 3625 | } 3626 | if (!pObject->table[idx]) { 3627 | pJsonNullNode pNode = JSONCreateNullNode(key); 3628 | if (!pNode) { 3629 | // Null节点内存分配失败 3630 | break; 3631 | } 3632 | pObject->table[idx] = (pJsonNode)pNode; 3633 | } 3634 | else { 3635 | pJsonNode pPre = NULL; 3636 | pJsonNode pCur = pObject->table[idx]; 3637 | pJsonNode pNext = pObject->table[idx]->next; 3638 | do { 3639 | if (strcmp(key, pCur->keyName) == 0) { 3640 | if (pCur->type == JSONTYPENUMBER) { 3641 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3642 | } 3643 | else if (pCur->type == JSONTYPELONGNUMBER) { 3644 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pCur); 3645 | } 3646 | else if (pCur->type == JSONTYPESTRING) { 3647 | JSONDestroyStringNode((pJsonStringNode)pCur); 3648 | } 3649 | else if (pCur->type == JSONTYPEARRAY) { 3650 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3651 | } 3652 | else if (pCur->type == JSONTYPEOBJECT) { 3653 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3654 | } 3655 | else if (pCur->type == JSONTYPENULL) { 3656 | break; 3657 | } 3658 | else if (pCur->type == JSONTYPEBOOLEAN) { 3659 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 3660 | } 3661 | else { 3662 | break; 3663 | } 3664 | pJsonNullNode pNode = JSONCreateNullNode(key); 3665 | if (!pNode) { 3666 | // Null节点内存分配失败 3667 | break; 3668 | } 3669 | pNode->node.next = pNext; 3670 | if (!pPre) { 3671 | pObject->table[idx] = (pJsonNode)pNode; 3672 | } 3673 | else { 3674 | pPre->next = (pJsonNode)pNode; 3675 | } 3676 | break; 3677 | } 3678 | if (pCur && !pCur->next) { 3679 | pJsonNullNode pNode = JSONCreateNullNode(key); 3680 | if (!pNode) { 3681 | // null节点内存分配失败 3682 | break; 3683 | } 3684 | pNode->node.next = pNext; 3685 | pCur->next = (pJsonNode)pNode; 3686 | break; 3687 | } 3688 | pPre = pCur; 3689 | pCur = pNext; 3690 | pNext = pNext->next; 3691 | } while (pCur); 3692 | } 3693 | } while (0); 3694 | } 3695 | 3696 | /* 3697 | * 向 json 对象中添加 Boolean类型 键值对象 3698 | * @param pObject json 对象 3699 | * @param key char* 属性名称 3700 | * @param value Boolean 属性值 3701 | * @return void 3702 | */ 3703 | void JSONSetBooleanAttr(pJsonObjectNode pObject /* in */, char* key /* in */, BOOLEAN value /* in */) { 3704 | int idx = -1; 3705 | do { 3706 | if (!pObject) { 3707 | break; 3708 | } 3709 | if (pObject->node.type != JSONTYPEOBJECT) { 3710 | break; 3711 | } 3712 | if (!key) { 3713 | break; 3714 | } 3715 | idx = JSONGetHashIndex(key); 3716 | if (idx == -1) { 3717 | break; 3718 | } 3719 | if (!pObject->table[idx]) { 3720 | pJsonBooleanNode pNode = JSONCreateBooleanNode(key, value); 3721 | if (!pNode) { 3722 | // Boolean节点内存分配失败 3723 | break; 3724 | } 3725 | pObject->table[idx] = (pJsonNode)pNode; 3726 | } 3727 | else { 3728 | pJsonNode pPre = NULL; 3729 | pJsonNode pCur = pObject->table[idx]; 3730 | pJsonNode pNext = pObject->table[idx]->next; 3731 | do { 3732 | if (strcmp(key, pCur->keyName) == 0) { 3733 | if (pCur->type == JSONTYPENUMBER) { 3734 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3735 | } 3736 | else if (pCur->type == JSONTYPELONGNUMBER) { 3737 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pCur); 3738 | } 3739 | else if (pCur->type == JSONTYPESTRING) { 3740 | JSONDestroyStringNode((pJsonStringNode)pCur); 3741 | } 3742 | else if (pCur->type == JSONTYPEARRAY) { 3743 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3744 | } 3745 | else if (pCur->type == JSONTYPEOBJECT) { 3746 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3747 | } 3748 | else if (pCur->type == JSONTYPENULL) { 3749 | JSONDestroyNullNode((pJsonNullNode)pCur); 3750 | } 3751 | else if (pCur->type == JSONTYPEBOOLEAN) { 3752 | ((pJsonBooleanNode)pCur)->value = value && 0x01; 3753 | break; 3754 | } 3755 | else { 3756 | break; 3757 | } 3758 | pJsonBooleanNode pNode = JSONCreateBooleanNode(key, value); 3759 | if (!pNode) { 3760 | // Number节点内存分配失败 3761 | break; 3762 | } 3763 | pNode->node.next = pNext; 3764 | if (!pPre) { 3765 | pObject->table[idx] = (pJsonNode)pNode; 3766 | } 3767 | else { 3768 | pPre->next = (pJsonNode)pNode; 3769 | } 3770 | break; 3771 | } 3772 | if (pCur && !pCur->next) { 3773 | pJsonBooleanNode pNode = JSONCreateBooleanNode(key, value); 3774 | if (!pNode) { 3775 | // Number节点内存分配失败 3776 | break; 3777 | } 3778 | pNode->node.next = pNext; 3779 | pCur->next = (pJsonNode)pNode; 3780 | break; 3781 | } 3782 | pPre = pCur; 3783 | pCur = pNext; 3784 | pNext = pNext->next; 3785 | } while (pCur); 3786 | } 3787 | } while (0); 3788 | } 3789 | 3790 | /* 3791 | * 移除 json 对象中的属性 3792 | * @param pObject json 对象 3793 | * @param key 属性名称 3794 | * @return void 3795 | */ 3796 | void JSONRemoveAttr(pJsonObjectNode pObject /* in */, char* key /* in */) { 3797 | int idx = -1; 3798 | do { 3799 | if (!pObject) { 3800 | break; 3801 | } 3802 | if (pObject->node.type != JSONTYPEOBJECT) { 3803 | break; 3804 | } 3805 | if (!key) { 3806 | break; 3807 | } 3808 | idx = JSONGetHashIndex(key); 3809 | if (idx == -1) { 3810 | break; 3811 | } 3812 | if (!pObject->table[idx]) { 3813 | break; 3814 | } 3815 | 3816 | pJsonNode pPre = NULL; 3817 | pJsonNode pCur = pObject->table[idx]; 3818 | pJsonNode pNext = pObject->table[idx]->next; 3819 | do { 3820 | if (strcmp(key, pCur->keyName) == 0) { 3821 | if (pCur->type == JSONTYPENUMBER) { 3822 | JSONDestroyNumberNode((pJsonNumberNode)pCur); 3823 | } 3824 | else if (pCur->type == JSONTYPESTRING) { 3825 | JSONDestroyStringNode((pJsonStringNode)pCur); 3826 | } 3827 | else if (pCur->type == JSONTYPEARRAY) { 3828 | JSONDestroyArrayNode((pJsonArrayNode)pCur); 3829 | } 3830 | else if (pCur->type == JSONTYPEOBJECT) { 3831 | JSONDestroyObjectNode((pJsonObjectNode)pCur); 3832 | } 3833 | else if (pCur->type == JSONTYPENULL) { 3834 | JSONDestroyNullNode((pJsonNullNode)pCur); 3835 | } 3836 | else if (pCur->type == JSONTYPEBOOLEAN) { 3837 | JSONDestroyBooleanNode((pJsonBooleanNode)pCur); 3838 | } 3839 | if (!pPre) { 3840 | pObject->table[idx] = pNext; 3841 | } 3842 | else { 3843 | pPre->next = pNext; 3844 | } 3845 | break; 3846 | } 3847 | } while (pNext); 3848 | } while (0); 3849 | } 3850 | 3851 | /* 3852 | * 销毁 json 节点 3853 | * @param pObject json 节点 3854 | * @return void 3855 | */ 3856 | void JSONDestroy(pJsonNode pNode /* in */) { 3857 | do { 3858 | if (!pNode) { 3859 | break; 3860 | } 3861 | if (pNode->type == JSONTYPENUMBER) { 3862 | JSONDestroyNumberNode((pJsonNumberNode)pNode); 3863 | } 3864 | else if (pNode->type == JSONTYPELONGNUMBER) { 3865 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pNode); 3866 | } 3867 | else if (pNode->type == JSONTYPESTRING) { 3868 | JSONDestroyStringNode((pJsonStringNode)pNode); 3869 | } 3870 | else if (pNode->type == JSONTYPEARRAY) { 3871 | JSONDestroyArrayNode((pJsonArrayNode)pNode); 3872 | } 3873 | else if (pNode->type == JSONTYPEOBJECT) { 3874 | JSONDestroyObjectNode((pJsonObjectNode)pNode); 3875 | } 3876 | else if (pNode->type == JSONTYPENULL) { 3877 | JSONDestroyNullNode((pJsonNullNode)pNode); 3878 | } 3879 | else if (pNode->type == JSONTYPEBOOLEAN) { 3880 | JSONDestroyBooleanNode((pJsonBooleanNode)pNode); 3881 | } 3882 | } while (0); 3883 | } 3884 | 3885 | /* 3886 | * json 对象深拷贝 3887 | * @param pObject json对象 3888 | * @return pJsonObjectNode 新创建的json对象 3889 | */ 3890 | pJsonObjectNode JSONObjectDeepClone(pJsonObjectNode pObject /* in */) { 3891 | pJsonObjectNode pJson = NULL; 3892 | do { 3893 | if (!pObject) { 3894 | break; 3895 | } 3896 | if (pObject->node.type != JSONTYPEOBJECT) { 3897 | break; 3898 | } 3899 | pJson = JSONCreateObjectNode(pObject->node.keyName); 3900 | if (!pJson) { 3901 | // 深拷贝时,json对象节点内存分配失败 3902 | break; 3903 | } 3904 | 3905 | // 拷贝hash表 3906 | for (int i = 0; i < HASH_MAX; i++) { 3907 | if (pObject->table[i]) { 3908 | // 需要进行链式拷贝 3909 | pJsonNode pPre = NULL; 3910 | pJsonNode pCur = pObject->table[i]; 3911 | pJsonNode pNext = pObject->table[i]->next; 3912 | int num = 0; 3913 | while (pCur) { 3914 | // 拷贝时如果拷贝到对象,或者自身怎么办 3915 | if (pCur == (pJsonNode)pObject || (num != 0 && pCur == pObject->table[i])) { 3916 | break; 3917 | } 3918 | num++; 3919 | pJsonNode pTmpNode = NULL; 3920 | if (pCur->type == JSONTYPENUMBER) { 3921 | pTmpNode = (pJsonNode)JSONCreateNumberNode(pCur->keyName, ((pJsonNumberNode)pCur)->value); 3922 | if (!pTmpNode) { 3923 | // 深拷贝时,json对象Number属性节点内存分配失败 3924 | break; 3925 | } 3926 | } 3927 | else if (pCur->type == JSONTYPESTRING) { 3928 | pTmpNode = (pJsonNode)JSONCreateStringNode(pCur->keyName, ((pJsonStringNode)pCur)->value); 3929 | if (!pTmpNode) { 3930 | // 深拷贝时,json对象String属性节点内存分配失败; 3931 | break; 3932 | } 3933 | } 3934 | else if (pCur->type == JSONTYPEARRAY) { 3935 | // 数组节点 3936 | pTmpNode = (pJsonNode)JSONArrayDeepClone((pJsonArrayNode)pCur); 3937 | } 3938 | else if (pCur->type == JSONTYPEOBJECT) { 3939 | // Object几点直接递归 3940 | pTmpNode = (pJsonNode)JSONObjectDeepClone((pJsonObjectNode)pCur); 3941 | } 3942 | else if (pCur->type == JSONTYPENULL) { 3943 | // null 节点 3944 | pTmpNode = (pJsonNode)JSONCreateNullNode(pCur->keyName); 3945 | } 3946 | else if (pCur->type == JSONTYPEBOOLEAN) { 3947 | // Boolean 3948 | pTmpNode = (pJsonNode)JSONCreateBooleanNode(pCur->keyName, ((pJsonBooleanNode)pCur)->value); 3949 | } 3950 | if (pPre == NULL) { 3951 | pJson->table[i] = pTmpNode; 3952 | } 3953 | else { 3954 | if (pPre) { 3955 | pPre->next = pTmpNode; 3956 | } 3957 | else { 3958 | // 链表断裂,无法继续链接下去,需要中断当前循环,并进行释放当前创建的节点 3959 | if (pTmpNode) { 3960 | if (pTmpNode->type == JSONTYPENUMBER) { 3961 | JSONDestroyNumberNode((pJsonNumberNode)pTmpNode); 3962 | } 3963 | else if (pTmpNode->type == JSONTYPESTRING) { 3964 | JSONDestroyStringNode((pJsonStringNode)pTmpNode); 3965 | } 3966 | else if (pTmpNode->type == JSONTYPEARRAY) { 3967 | JSONDestroyArrayNode((pJsonArrayNode)pTmpNode); 3968 | } 3969 | else if (pTmpNode->type == JSONTYPEOBJECT) { 3970 | JSONDestroyObjectNode((pJsonObjectNode)pTmpNode); 3971 | } 3972 | else if (pTmpNode->type == JSONTYPENULL) { 3973 | JSONDestroyNullNode((pJsonNullNode)pTmpNode); 3974 | } 3975 | else if (pTmpNode->type == JSONTYPEBOOLEAN) { 3976 | JSONDestroyBooleanNode((pJsonBooleanNode)pTmpNode); 3977 | } 3978 | } 3979 | } 3980 | } 3981 | pPre = pTmpNode; 3982 | pCur = pNext; 3983 | if (pNext) { 3984 | pNext = pNext->next; 3985 | } 3986 | 3987 | } 3988 | } 3989 | } 3990 | 3991 | } while (0); 3992 | return pJson; 3993 | } 3994 | 3995 | /* 3996 | * json 数组深拷贝 3997 | * @param pArray json数组 3998 | * @return pJsonArrayNode 新创建的json Array 3999 | */ 4000 | pJsonArrayNode JSONArrayDeepClone(pJsonArrayNode pArray /* in */) { 4001 | pJsonArrayNode pArrayNode = NULL; 4002 | do { 4003 | if (!pArray) { 4004 | break; 4005 | } 4006 | if (pArray->node.type != JSONTYPEARRAY) { 4007 | break; 4008 | } 4009 | pArrayNode = JSONCreateArrayNode(pArray->node.keyName); 4010 | if (!pArrayNode) { 4011 | break; 4012 | } 4013 | pArrayNode->size = pArray->size; 4014 | // 存放元素的类型 4015 | pArrayNode->type = pArray->type; 4016 | // 拷贝数组元素 4017 | if (pArray->size && pArray->array) { 4018 | // 计算数组分配内存空间,数组节点的元素数组空间默认不分配,只有添加元素才进行分配 4019 | // 介于内存分配考虑,每次分配 ARRAY_MIN_SIZE 大小,当缓冲区越界的时候,重新分配,以节约内存 4020 | int size = (pArray->size / ARRAY_MIN_SIZE + (pArray->size % ARRAY_MIN_SIZE == 0 ? 0 : 1)) * ARRAY_MIN_SIZE; 4021 | pArrayNode->array = (pJsonNode*)malloc(size * sizeof(pJsonNode)); 4022 | if (!pArrayNode->array) { 4023 | // 数组空间分配失败 4024 | pArrayNode->size = 0; 4025 | pArrayNode->type = JSONTYPEUNDEFINED; 4026 | break; 4027 | } 4028 | memset(pArrayNode->array, 0, size * sizeof(pJsonNode)); 4029 | // 进行数组元素深拷贝 4030 | for (int i = 0; i < pArray->size; i++) { 4031 | if (pArray->array[i]) { 4032 | if (pArray->array[i]->type != pArray->type) { 4033 | continue; 4034 | } 4035 | if (pArray->array[i]->type == JSONTYPENUMBER) { 4036 | pArrayNode->array[i] = (pJsonNode)JSONCreateNumberNode(pArray->array[i]->keyName, ((pJsonNumberNode)pArray->array[i])->value); 4037 | } 4038 | else if (pArray->array[i]->type == JSONTYPESTRING) { 4039 | pArrayNode->array[i] = (pJsonNode)JSONCreateStringNode(pArray->array[i]->keyName, ((pJsonStringNode)pArray->array[i])->value); 4040 | } 4041 | else if (pArray->array[i]->type == JSONTYPEARRAY) { 4042 | pArrayNode->array[i] = (pJsonNode)JSONArrayDeepClone((pJsonArrayNode)pArray->array[i]); 4043 | } 4044 | else if (pArray->array[i]->type == JSONTYPEOBJECT) { 4045 | pArrayNode->array[i] = (pJsonNode)JSONObjectDeepClone((pJsonObjectNode)pArray->array[i]); 4046 | } 4047 | else if (pArray->array[i]->type == JSONTYPENULL) { 4048 | pArrayNode->array[i] = (pJsonNode)JSONCreateNullNode(pArray->array[i]->keyName); 4049 | } 4050 | else if (pArray->array[i]->type == JSONTYPEBOOLEAN) { 4051 | pArrayNode->array[i] = (pJsonNode)JSONCreateBooleanNode(pArray->array[i]->keyName, ((pJsonBooleanNode)pArray->array[i])->value); 4052 | } 4053 | } 4054 | } 4055 | } 4056 | } while (0); 4057 | return pArrayNode; 4058 | } 4059 | 4060 | /* 4061 | * json 数字对象深拷贝 4062 | * @param pNumber jsonNumber 4063 | * @param isCopyKey char* 是否进行键名称拷贝 4064 | * @return pJsonNumberNode 新创建的json Number 4065 | */ 4066 | pJsonNumberNode JSONNumberDeepClone(pJsonNumberNode pNumber /* in */, BOOLEAN isCopyKey /* in */) { 4067 | pJsonNumberNode pNumberNode = NULL; 4068 | do { 4069 | if (!pNumber) { 4070 | break; 4071 | } 4072 | if (pNumber->node.type != JSONTYPENUMBER) { 4073 | break; 4074 | } 4075 | pNumberNode = (pJsonNumberNode)malloc(sizeof(JsonNumberNode)); 4076 | if (!pNumberNode) { 4077 | break; 4078 | } 4079 | memset(pNumberNode, 0, sizeof(JsonNumberNode)); 4080 | // 属性拷贝的时候,需要对键名称进行拷贝,做为数组元素的时候则不需要键名称 4081 | if (isCopyKey) { 4082 | if (pNumber->node.keyName) { 4083 | pNumberNode->node.keyName = deepCloneString(pNumber->node.keyName); 4084 | if (!pNumberNode->node.keyName) { 4085 | // 拷贝键名称的时候,内存无法分配,则节点没有存在的意义 4086 | JSONDestroyNumberNode(pNumberNode); 4087 | break; 4088 | } 4089 | } 4090 | } 4091 | pNumberNode->node.type = JSONTYPENUMBER; 4092 | pNumberNode->value = pNumber->value; 4093 | } while (0); 4094 | return pNumberNode; 4095 | } 4096 | 4097 | /* 4098 | * json 字符串对象深拷贝 4099 | * @param pString jsonString 4100 | * @param isCopyKey char* 是否进行键名称拷贝 4101 | * @return pJsonNumberNode 新创建的json Number 4102 | */ 4103 | pJsonStringNode JSONStringDeepClone(pJsonStringNode pString /* in */, BOOLEAN isCopyKey /* in */) { 4104 | pJsonStringNode pStringNode = NULL; 4105 | do { 4106 | if (!pString) { 4107 | break; 4108 | } 4109 | if (pString->node.type != JSONTYPESTRING) { 4110 | break; 4111 | } 4112 | pStringNode = (pJsonStringNode)malloc(sizeof(JsonStringNode)); 4113 | if (!pStringNode) { 4114 | break; 4115 | } 4116 | memset(pStringNode, 0, sizeof(JsonStringNode)); 4117 | // 属性拷贝的时候,需要对键名称进行拷贝,做为数组元素的时候则不需要键名称 4118 | if (isCopyKey) { 4119 | if (pString->node.keyName) { 4120 | pStringNode->node.keyName = deepCloneString(pString->node.keyName); 4121 | if (!pStringNode->node.keyName) { 4122 | // 拷贝键名称的时候,内存无法分配,则节点没有存在的意义 4123 | JSONDestroyStringNode(pStringNode); 4124 | break; 4125 | } 4126 | } 4127 | } 4128 | pStringNode->node.type = JSONTYPESTRING; 4129 | pStringNode->value = deepCloneString(pString->value); 4130 | } while (0); 4131 | return pStringNode; 4132 | } 4133 | 4134 | /* 4135 | * json null 对象深拷贝 4136 | * @param pNull pJsonNullNode 4137 | * @param isCopyKey char* 是否进行键名称拷贝 4138 | * @return pJsonNullNode 新创建的json null 4139 | */ 4140 | pJsonNullNode JSONNullDeepClone(pJsonNullNode pNull /* in */, BOOLEAN isCopyKey /* in */) { 4141 | pJsonNullNode pNode = NULL; 4142 | do { 4143 | if (!pNull) { 4144 | break; 4145 | } 4146 | if (pNull->node.type != JSONTYPENULL) { 4147 | break; 4148 | } 4149 | pNode = (pJsonNullNode)malloc(sizeof(JsonNullNode)); 4150 | if (!pNull) { 4151 | break; 4152 | } 4153 | memset(pNode, 0, sizeof(JsonNullNode)); 4154 | // 属性拷贝的时候,需要对键名称进行拷贝,做为数组元素的时候则不需要键名称 4155 | if (isCopyKey) { 4156 | if (pNull->node.keyName) { 4157 | pNode->node.keyName = deepCloneString(pNull->node.keyName); 4158 | if (!pNode->node.keyName) { 4159 | // 拷贝键名称的时候,内存无法分配,则节点没有存在的意义 4160 | JSONDestroyNullNode(pNode); 4161 | break; 4162 | } 4163 | } 4164 | } 4165 | pNode->node.type = JSONTYPENULL; 4166 | pNode->value = (int)null; 4167 | } while (0); 4168 | return pNode; 4169 | } 4170 | 4171 | /* 4172 | * json Boolean 对象深拷贝 4173 | * @param pNull pJsonBooleanNode 4174 | * @param isCopyKey char* 是否进行键名称拷贝 4175 | * @return pJsonBooleanNode 新创建的json Boolean 4176 | */ 4177 | pJsonBooleanNode JSONBooleanDeepClone(pJsonBooleanNode pBoolean /* in */, BOOLEAN isCopyKey /* in */) { 4178 | pJsonBooleanNode pNode = NULL; 4179 | do { 4180 | if (!pBoolean) { 4181 | break; 4182 | } 4183 | if (pBoolean->node.type != JSONTYPEBOOLEAN) { 4184 | break; 4185 | } 4186 | pNode = (pJsonBooleanNode)malloc(sizeof(JsonBooleanNode)); 4187 | if (!pBoolean) { 4188 | break; 4189 | } 4190 | memset(pNode, 0, sizeof(JsonBooleanNode)); 4191 | // 属性拷贝的时候,需要对键名称进行拷贝,做为数组元素的时候则不需要键名称 4192 | if (isCopyKey) { 4193 | if (pBoolean->node.keyName) { 4194 | pNode->node.keyName = deepCloneString(pBoolean->node.keyName); 4195 | if (!pNode->node.keyName) { 4196 | // 拷贝键名称的时候,内存无法分配,则节点没有存在的意义 4197 | JSONDestroyBooleanNode(pNode); 4198 | break; 4199 | } 4200 | } 4201 | } 4202 | pNode->node.type = JSONTYPEBOOLEAN; 4203 | pNode->value = pBoolean->value && 0x01; 4204 | } while (0); 4205 | return pNode; 4206 | } 4207 | 4208 | /* 4209 | * 销毁 Number 类型类型节点 4210 | * @param pNode Number类型节点 4211 | * @return void 4212 | */ 4213 | void JSONDestroyNumberNode(pJsonNumberNode pNode /* in */) { 4214 | do { 4215 | if (!pNode) { 4216 | break; 4217 | } 4218 | if (pNode->node.type != JSONTYPENUMBER) { 4219 | break; 4220 | } 4221 | pNode->node.type = (JSON_DATA_TYPE)0; 4222 | if (pNode->node.keyName) { 4223 | free(pNode->node.keyName); 4224 | pNode->node.keyName = NULL; 4225 | } 4226 | free(pNode); 4227 | pNode = NULL; 4228 | } while (0); 4229 | } 4230 | 4231 | /* 4232 | * 销毁 Long Number 类型类型节点 4233 | * @param pNode Number类型节点 4234 | * @return void 4235 | */ 4236 | void JSONDestroyLongNumberNode(pJsonLongNumberNode pNode /* in */) { 4237 | do { 4238 | if (!pNode) { 4239 | break; 4240 | } 4241 | if (pNode->node.type != JSONTYPELONGNUMBER) { 4242 | break; 4243 | } 4244 | pNode->node.type = (JSON_DATA_TYPE)0; 4245 | if (pNode->node.keyName) { 4246 | free(pNode->node.keyName); 4247 | pNode->node.keyName = NULL; 4248 | } 4249 | free(pNode); 4250 | pNode = NULL; 4251 | } while (0); 4252 | } 4253 | 4254 | /* 4255 | * 销毁 String 类型类型节点 4256 | * @param pNode String类型节点 4257 | * @return void 4258 | */ 4259 | void JSONDestroyStringNode(pJsonStringNode pNode /* in */) { 4260 | do { 4261 | if (!pNode) { 4262 | break; 4263 | } 4264 | if (pNode->node.type != JSONTYPESTRING) { 4265 | break; 4266 | } 4267 | pNode->node.type = (JSON_DATA_TYPE)0; 4268 | if (pNode->value) { 4269 | free(pNode->value); 4270 | } 4271 | if (pNode->node.keyName) { 4272 | free(pNode->node.keyName); 4273 | pNode->node.keyName = NULL; 4274 | } 4275 | free(pNode); 4276 | pNode = NULL; 4277 | } while (0); 4278 | } 4279 | 4280 | /* 4281 | * 销毁 Array 类型类型节点 4282 | * @param pNode Array类型节点 4283 | * @return void 4284 | */ 4285 | void JSONDestroyArrayNode(pJsonArrayNode pNode /* in */) { 4286 | int i = 0; 4287 | do { 4288 | if (!pNode) { 4289 | break; 4290 | } 4291 | if (pNode->node.type != JSONTYPEARRAY) { 4292 | break; 4293 | } 4294 | pNode->node.type = (JSON_DATA_TYPE)0; 4295 | if (pNode->size && pNode->array) { 4296 | for (i = 0; i < pNode->size; i++) { 4297 | if (pNode->array[i]->type == JSONTYPENUMBER) { 4298 | JSONDestroyNumberNode((pJsonNumberNode)pNode->array[i]); 4299 | } 4300 | else if (pNode->array[i]->type == JSONTYPELONGNUMBER) { 4301 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pNode->array[i]); 4302 | } 4303 | else if (pNode->array[i]->type == JSONTYPESTRING) { 4304 | JSONDestroyStringNode((pJsonStringNode)pNode->array[i]); 4305 | } 4306 | else if (pNode->array[i]->type == JSONTYPEARRAY) { 4307 | JSONDestroyArrayNode((pJsonArrayNode)pNode->array[i]); 4308 | } 4309 | else if (pNode->array[i]->type == JSONTYPEOBJECT) { 4310 | JSONDestroyObjectNode((pJsonObjectNode)pNode->array[i]); 4311 | } 4312 | else if (pNode->array[i]->type == JSONTYPENULL) { 4313 | JSONDestroyNullNode((pJsonNullNode)pNode->array[i]); 4314 | } 4315 | else if (pNode->array[i]->type == JSONTYPEBOOLEAN) { 4316 | JSONDestroyBooleanNode((pJsonBooleanNode)pNode->array[i]); 4317 | } 4318 | pNode->array[i] = NULL; 4319 | } 4320 | } 4321 | if (pNode->array) { 4322 | free(pNode->array); 4323 | pNode->array = NULL; 4324 | } 4325 | //printf("销毁 Array 节点,属性名称: %s\n", pNode->node.keyName); 4326 | if (pNode->node.keyName) { 4327 | free(pNode->node.keyName); 4328 | pNode->node.keyName = NULL; 4329 | } 4330 | free(pNode); 4331 | pNode = NULL; 4332 | } while (0); 4333 | } 4334 | 4335 | /* 4336 | * 销毁 Object 类型类型节点 4337 | * @param pNode Object类型节点 4338 | * @return void 4339 | */ 4340 | void JSONDestroyObjectNode(pJsonObjectNode pNode /* in */) { 4341 | int i = 0; 4342 | do { 4343 | if (!pNode) { 4344 | break; 4345 | } 4346 | if (pNode->node.type != JSONTYPEOBJECT) { 4347 | break; 4348 | } 4349 | pNode->node.type = (JSON_DATA_TYPE)0; 4350 | for (i = 0; i < HASH_MAX; i++) { 4351 | // 判断当前属性是否存在,存在则销毁 4352 | if (pNode->table[i]) { 4353 | // 对当前属性,进行链表式销毁 4354 | pJsonNode pNext = NULL; 4355 | do { 4356 | if (!pNode->table[i]) { 4357 | break; 4358 | } 4359 | pNext = pNode->table[i]->next; 4360 | if (pNode->table[i]->type == JSONTYPENUMBER) { 4361 | // 数字类型,直接销毁节点 4362 | JSONDestroyNumberNode((pJsonNumberNode)pNode->table[i]); 4363 | } 4364 | else if (pNode->table[i]->type == JSONTYPELONGNUMBER) { 4365 | // 长整型类型,先进行销毁长整型节点 4366 | JSONDestroyLongNumberNode((pJsonLongNumberNode)pNode->table[i]); 4367 | } 4368 | else if (pNode->table[i]->type == JSONTYPESTRING) { 4369 | // 字符串类型,先进行销毁字符串空间 4370 | JSONDestroyStringNode((pJsonStringNode)pNode->table[i]); 4371 | } 4372 | else if (pNode->table[i]->type == JSONTYPEARRAY) { 4373 | JSONDestroyArrayNode((pJsonArrayNode)pNode->table[i]); 4374 | } 4375 | else if (pNode->table[i]->type == JSONTYPEOBJECT) { 4376 | JSONDestroyObjectNode((pJsonObjectNode)pNode->table[i]); 4377 | } 4378 | else if (pNode->table[i]->type == JSONTYPENULL) { 4379 | JSONDestroyNullNode((pJsonNullNode)pNode->table[i]); 4380 | } 4381 | else if (pNode->table[i]->type == JSONTYPEBOOLEAN) { 4382 | JSONDestroyBooleanNode((pJsonBooleanNode)pNode->table[i]); 4383 | } 4384 | pNode->table[i] = pNext; 4385 | } while (pNext); 4386 | } 4387 | pNode->table[i] = NULL; 4388 | } 4389 | if (pNode->node.keyName) { 4390 | free(pNode->node.keyName); 4391 | pNode->node.keyName = NULL; 4392 | } 4393 | 4394 | free(pNode); 4395 | pNode = NULL; 4396 | } while (0); 4397 | } 4398 | 4399 | /* 4400 | * 销毁 null 类型类型节点 4401 | * @param pNode null类型节点 4402 | * @return void 4403 | */ 4404 | void JSONDestroyNullNode(pJsonNullNode pNode /* in */) { 4405 | do { 4406 | if (!pNode) { 4407 | break; 4408 | } 4409 | if (pNode->node.type != JSONTYPENULL) { 4410 | break; 4411 | } 4412 | if (pNode->node.keyName) { 4413 | free(pNode->node.keyName); 4414 | } 4415 | free(pNode); 4416 | } while (0); 4417 | } 4418 | 4419 | /* 4420 | * 销毁 Boolean 类型类型节点 4421 | * @param pNode Boolean类型节点 4422 | * @return void 4423 | */ 4424 | void JSONDestroyBooleanNode(pJsonBooleanNode pNode /* in */) { 4425 | do { 4426 | if (!pNode) { 4427 | break; 4428 | } 4429 | if (pNode->node.type != JSONTYPEBOOLEAN) { 4430 | break; 4431 | } 4432 | if (pNode->node.keyName) { 4433 | free(pNode->node.keyName); 4434 | } 4435 | free(pNode); 4436 | } while (0); 4437 | } 4438 | 4439 | /* 4440 | * 向 json 数组节点中数组尾部追加元素 4441 | * @param pArrayNode pJsonArrayNode 数组节点 4442 | * @param pNode pJsonNode 需要添加的节点 4443 | * @return void 4444 | */ 4445 | void JSONArrayPush(pJsonArrayNode pArrayNode /* in */, pJsonNode pNode /* in */) { 4446 | do { 4447 | if (!pArrayNode) { 4448 | break; 4449 | } 4450 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4451 | break; 4452 | } 4453 | if (!pNode) { 4454 | break; 4455 | } 4456 | //// 数组类型,和需要插入的数据类型是否匹配 4457 | //if (pArrayNode->type != JSONTYPEUNDEFINED && (pArrayNode->type != pNode->type)) { 4458 | // break; 4459 | //} 4460 | if (pNode->type == JSONTYPEUNDEFINED) { 4461 | break; 4462 | } 4463 | // 判断数组是否需要扩容 4464 | int minSize = (pArrayNode->size / ARRAY_MIN_SIZE) * ARRAY_MIN_SIZE; 4465 | if (minSize >= pArrayNode->size) { 4466 | // 需扩容 4467 | pJsonNode* tmpArr = (pJsonNode*)malloc((minSize + ARRAY_MIN_SIZE) * sizeof(pJsonNode)); 4468 | if (!tmpArr) { 4469 | JSONDestroy(pNode); 4470 | break; 4471 | } 4472 | memset(tmpArr, 0, (minSize + ARRAY_MIN_SIZE) * sizeof(pJsonNode)); 4473 | memcpy(tmpArr, pArrayNode->array, pArrayNode->size * sizeof(pJsonNode)); 4474 | free(pArrayNode->array); 4475 | pArrayNode->array = tmpArr; 4476 | } 4477 | // 第一次插入元素时 4478 | if (!pArrayNode->array) { 4479 | pArrayNode->array = (pJsonNode*)malloc(ARRAY_MIN_SIZE); 4480 | if (!pArrayNode->array) { 4481 | memset(pArrayNode->array, 0, ARRAY_MIN_SIZE); 4482 | } 4483 | } 4484 | pArrayNode->array[pArrayNode->size] = pNode; 4485 | pArrayNode->size++; 4486 | pArrayNode->type = pNode->type; 4487 | } while (0); 4488 | } 4489 | 4490 | /* 4491 | * 向 json 数组节点中数组头部追加元素 4492 | * @param pArrayNode pJsonArrayNode 数组节点 4493 | * @param pNode pJsonNode 需要添加的节点 4494 | * @return void 4495 | */ 4496 | void JSONArrayUnshift(pJsonArrayNode pArrayNode /* in */, pJsonNode pNode /* in */) { 4497 | do { 4498 | if (!pArrayNode) { 4499 | break; 4500 | } 4501 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4502 | break; 4503 | } 4504 | if (!pNode) { 4505 | break; 4506 | } 4507 | // 数组类型,和需要插入的数据类型是否匹配 4508 | if (pArrayNode->type != JSONTYPEUNDEFINED && (pArrayNode->type != pNode->type)) { 4509 | break; 4510 | } 4511 | if (pNode->type == JSONTYPEUNDEFINED) { 4512 | break; 4513 | } 4514 | // 判断数组是否需要扩容 4515 | int minSize = (pArrayNode->size / ARRAY_MIN_SIZE) * ARRAY_MIN_SIZE; 4516 | if (minSize >= pArrayNode->size) { 4517 | // 需扩容 4518 | pJsonNode* tmpArr = (pJsonNode*)malloc((minSize + ARRAY_MIN_SIZE) * sizeof(pJsonNode)); 4519 | if (!tmpArr) { 4520 | break; 4521 | } 4522 | memset(tmpArr, 0, (minSize + ARRAY_MIN_SIZE) * sizeof(pJsonNode)); 4523 | memcpy(tmpArr + 1, pArrayNode->array, pArrayNode->size * sizeof(pJsonNode)); 4524 | pArrayNode->array = tmpArr; 4525 | } 4526 | else { 4527 | for (int i = pArrayNode->size; i > 0; i--) { 4528 | pArrayNode->array[i] = pArrayNode->array[i - 1]; 4529 | } 4530 | pArrayNode->array[0] = NULL; 4531 | } 4532 | // 第一次插入元素时 4533 | if (!pArrayNode->array) { 4534 | pArrayNode->array = (pJsonNode*)malloc(ARRAY_MIN_SIZE); 4535 | if (!pArrayNode->array) { 4536 | memset(pArrayNode->array, 0, ARRAY_MIN_SIZE); 4537 | } 4538 | } 4539 | pArrayNode->array[0] = pNode; 4540 | pArrayNode->size++; 4541 | pArrayNode->type = pNode->type; 4542 | } while (0); 4543 | } 4544 | 4545 | /* 4546 | * 向 json 数组节点中数组尾部追加数字 4547 | * @param pArrayNode pJsonArrayNode 数组节点 4548 | * @param number double 需要添加的数字 4549 | * @return void 4550 | */ 4551 | void JSONArrayPushNumber(pJsonArrayNode pArrayNode /* in */, double number /* in */) { 4552 | do { 4553 | if (!pArrayNode) { 4554 | break; 4555 | } 4556 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4557 | break; 4558 | } 4559 | pJsonNumberNode pNode = JSONCreateNumberNode(NULL, number); 4560 | JSONArrayPush(pArrayNode, (pJsonNode)pNode); 4561 | } while (0); 4562 | } 4563 | 4564 | /* 4565 | * 向 json 数组节点中数组尾部追加长数字 4566 | * @param pArrayNode pJsonArrayNode 数组节点 4567 | * @param number UINT64 需要添加的数字 4568 | * @return void 4569 | */ 4570 | void JSONArrayPushLongNumber(pJsonArrayNode pArrayNode /* in */, UINT64 number /* in */){ 4571 | do { 4572 | if (!pArrayNode) { 4573 | break; 4574 | } 4575 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4576 | break; 4577 | } 4578 | pJsonLongNumberNode pNode = JSONCreateLongNumberNode(NULL, number); 4579 | JSONArrayPush(pArrayNode, (pJsonNode)pNode); 4580 | } while (0); 4581 | } 4582 | 4583 | /* 4584 | * 向 json 数组节点中数组尾部追加字符串 4585 | * @param pArrayNode pJsonArrayNode 数组节点 4586 | * @param pString char* 需要添加的字符串 4587 | * @return void 4588 | */ 4589 | void JSONArrayPushString(pJsonArrayNode pArrayNode /* in */, char* pString /* in */) { 4590 | do { 4591 | if (!pArrayNode) { 4592 | break; 4593 | } 4594 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4595 | break; 4596 | } 4597 | pJsonStringNode pNode = JSONCreateStringNode(NULL, pString); 4598 | JSONArrayPush(pArrayNode, (pJsonNode)pNode); 4599 | } while (0); 4600 | } 4601 | 4602 | /* 4603 | * 向 json 数组节点中数组尾部追加数组 4604 | * @param pArrayNode pJsonArrayNode 数组节点 4605 | * @param pArr pJsonArrayNode 需要添加的数组节点 4606 | * @return void 4607 | */ 4608 | void JSONArrayPushArray(pJsonArrayNode pArrayNode /* in */, pJsonArrayNode pArr /* in */) { 4609 | JSONArrayPush(pArrayNode, (pJsonNode)pArr); 4610 | } 4611 | 4612 | /* 4613 | * 向 json 数组节点中数组尾部追加对象 4614 | * @param pArrayNode pJsonArrayNode 数组节点 4615 | * @param pObject pJsonObjectNode 需要添加的对象节点 4616 | * @return void 4617 | */ 4618 | void JSONArrayPushObject(pJsonArrayNode pArrayNode /* in */, pJsonObjectNode pObject /* in */) { 4619 | JSONArrayPush(pArrayNode, (pJsonNode)pObject); 4620 | } 4621 | 4622 | /* 4623 | * 向 json 数组节点中数组尾部追加 null 4624 | * @param pArrayNode pJsonArrayNode 数组节点 4625 | * @return void 4626 | */ 4627 | void JSONArrayPushNull(pJsonArrayNode pArrayNode /* in */) { 4628 | do { 4629 | if (!pArrayNode) { 4630 | break; 4631 | } 4632 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4633 | break; 4634 | } 4635 | pJsonNullNode pNode = JSONCreateNullNode(NULL); 4636 | JSONArrayPush(pArrayNode, (pJsonNode)pNode); 4637 | } while (0); 4638 | } 4639 | 4640 | /* 4641 | * 向 json 数组节点中数组尾部追加 Boolean 4642 | * @param pArrayNode pJsonArrayNode 数组节点 4643 | * @param value BOOLEAN 需要添加的 Boolean 4644 | * @return void 4645 | */ 4646 | void JSONArrayPushBoolean(pJsonArrayNode pArrayNode /* in */, BOOLEAN value /* in */) { 4647 | do { 4648 | if (!pArrayNode) { 4649 | break; 4650 | } 4651 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4652 | break; 4653 | } 4654 | pJsonBooleanNode pNode = JSONCreateBooleanNode(NULL, value); 4655 | JSONArrayPush(pArrayNode, (pJsonNode)pNode); 4656 | } while (0); 4657 | } 4658 | 4659 | /* 4660 | * 向 json 数组节点中数组头部追加数字 4661 | * @param pArrayNode pJsonArrayNode 数组节点 4662 | * @param number double 需要添加的数字 4663 | * @return void 4664 | */ 4665 | void JSONArrayUnshiftNumber(pJsonArrayNode pArrayNode /* in */, double number /* in */) { 4666 | do { 4667 | if (!pArrayNode) { 4668 | break; 4669 | } 4670 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4671 | break; 4672 | } 4673 | pJsonNumberNode pNode = JSONCreateNumberNode(NULL, number); 4674 | JSONArrayUnshift(pArrayNode, (pJsonNode)pNode); 4675 | } while (0); 4676 | } 4677 | 4678 | /* 4679 | * 向 json 数组节点中数组头部追加长数字 4680 | * @param pArrayNode pJsonArrayNode 数组节点 4681 | * @param number UINT64 需要添加的数字 4682 | * @return void 4683 | */ 4684 | void JSONArrayUnshiftLongNumber(pJsonArrayNode pArrayNode /* in */, UINT64 number /* in */){ 4685 | do { 4686 | if (!pArrayNode) { 4687 | break; 4688 | } 4689 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4690 | break; 4691 | } 4692 | pJsonLongNumberNode pNode = JSONCreateLongNumberNode(NULL, number); 4693 | JSONArrayUnshift(pArrayNode, (pJsonNode)pNode); 4694 | } while (0); 4695 | } 4696 | 4697 | /* 4698 | * 向 json 数组节点中数组头部追加字符串 4699 | * @param pArrayNode pJsonArrayNode 数组节点 4700 | * @param pString char* 需要添加的字符串 4701 | * @return void 4702 | */ 4703 | void JSONArrayUnshiftString(pJsonArrayNode pArrayNode /* in */, char* pString /* in */) { 4704 | do { 4705 | if (!pArrayNode) { 4706 | break; 4707 | } 4708 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4709 | break; 4710 | } 4711 | pJsonStringNode pNode = JSONCreateStringNode(NULL, pString); 4712 | JSONArrayUnshift(pArrayNode, (pJsonNode)pNode); 4713 | } while (0); 4714 | } 4715 | 4716 | /* 4717 | * 向 json 数组节点中数组头部追加数组 4718 | * @param pArrayNode pJsonArrayNode 数组节点 4719 | * @param pArr pJsonArrayNode 需要添加的数组 4720 | * @return void 4721 | */ 4722 | void JSONArrayUnshiftArray(pJsonArrayNode pArrayNode /* in */, pJsonArrayNode pArr /* in */) { 4723 | JSONArrayUnshift(pArrayNode, (pJsonNode)pArr); 4724 | } 4725 | 4726 | /* 4727 | * 向 json 数组节点中数组头部追加对象 4728 | * @param pArrayNode pJsonArrayNode 数组节点 4729 | * @param pObject pJsonObjectNode 需要添加的对象 4730 | * @return void 4731 | */ 4732 | void JSONArrayUnshiftObject(pJsonArrayNode pArrayNode /* in */, pJsonObjectNode pObject /* in */) { 4733 | JSONArrayUnshift(pArrayNode, (pJsonNode)pObject); 4734 | } 4735 | 4736 | /* 4737 | * 向 json 数组节点中数组头部追加 null 4738 | * @param pArrayNode pJsonArrayNode 数组节点 4739 | * @return void 4740 | */ 4741 | void JSONArrayUnshiftNull(pJsonArrayNode pArrayNode /* in */) { 4742 | do { 4743 | if (!pArrayNode) { 4744 | break; 4745 | } 4746 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4747 | break; 4748 | } 4749 | pJsonNullNode pNode = JSONCreateNullNode(NULL); 4750 | JSONArrayUnshift(pArrayNode, (pJsonNode)pNode); 4751 | } while (0); 4752 | } 4753 | 4754 | /* 4755 | * 向 json 数组节点中数组头部追加 Boolean 4756 | * @param pArrayNode pJsonArrayNode 数组节点 4757 | * @param value BOOLEAN 需要添加的 Boolean 4758 | * @return void 4759 | */ 4760 | void JSONArrayUnshiftBoolean(pJsonArrayNode pArrayNode /* in */, BOOLEAN value /* in */) { 4761 | do { 4762 | if (!pArrayNode) { 4763 | break; 4764 | } 4765 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4766 | break; 4767 | } 4768 | pJsonBooleanNode pNode = JSONCreateBooleanNode(NULL, value); 4769 | JSONArrayUnshift(pArrayNode, (pJsonNode)pNode); 4770 | } while (0); 4771 | } 4772 | 4773 | /* 4774 | * 向 json 数组节点中数组尾部删除元素,并销毁该元素 4775 | * @param pArrayNode pJsonArrayNode 数组节点 4776 | * @return void 4777 | */ 4778 | void JSONArrayPop(pJsonArrayNode pArrayNode /* in */) { 4779 | do { 4780 | if (!pArrayNode) { 4781 | break; 4782 | } 4783 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4784 | break; 4785 | } 4786 | if (pArrayNode->size <= 0) { 4787 | break; 4788 | } 4789 | pJsonNode pTmp = pArrayNode->array[pArrayNode->size - 1]; 4790 | pArrayNode->array[pArrayNode->size - 1] = NULL; 4791 | if (pTmp) { 4792 | if (pTmp->type == JSONTYPENUMBER) { 4793 | JSONDestroyNumberNode((pJsonNumberNode)pTmp); 4794 | } 4795 | else if (pTmp->type == JSONTYPESTRING) { 4796 | JSONDestroyStringNode((pJsonStringNode)pTmp); 4797 | } 4798 | else if (pTmp->type == JSONTYPEARRAY) { 4799 | JSONDestroyArrayNode((pJsonArrayNode)pTmp); 4800 | } 4801 | else if (pTmp->type == JSONTYPEOBJECT) { 4802 | JSONDestroyObjectNode((pJsonObjectNode)pTmp); 4803 | } 4804 | else if (pTmp->type == JSONTYPENULL) { 4805 | JSONDestroyNullNode((pJsonNullNode)pTmp); 4806 | } 4807 | else if (pTmp->type == JSONTYPEBOOLEAN) { 4808 | JSONDestroyBooleanNode((pJsonBooleanNode)pTmp); 4809 | } 4810 | } 4811 | pArrayNode->size--; 4812 | if (pArrayNode->size == 0) { 4813 | // 释放数组空间 4814 | free(pArrayNode->array); 4815 | pArrayNode->array = NULL; 4816 | } 4817 | } while (0); 4818 | } 4819 | 4820 | /* 4821 | * 向 json 数组节点中数组头部删除元素,并销毁该元素 4822 | * @param pArrayNode pJsonArrayNode 数组节点 4823 | * @return void 4824 | */ 4825 | void JSONArrayShift(pJsonArrayNode pArrayNode /* in */) { 4826 | do { 4827 | if (!pArrayNode) { 4828 | break; 4829 | } 4830 | if (pArrayNode->node.type != JSONTYPEARRAY) { 4831 | break; 4832 | } 4833 | if (pArrayNode->size <= 0) { 4834 | break; 4835 | } 4836 | pJsonNode pTmp = pArrayNode->array[pArrayNode->size - 1]; 4837 | pArrayNode->array[pArrayNode->size - 1] = NULL; 4838 | if (pTmp) { 4839 | if (pTmp->type == JSONTYPENUMBER) { 4840 | JSONDestroyNumberNode((pJsonNumberNode)pTmp); 4841 | } 4842 | else if (pTmp->type == JSONTYPESTRING) { 4843 | JSONDestroyStringNode((pJsonStringNode)pTmp); 4844 | } 4845 | else if (pTmp->type == JSONTYPEARRAY) { 4846 | JSONDestroyArrayNode((pJsonArrayNode)pTmp); 4847 | } 4848 | else if (pTmp->type == JSONTYPEOBJECT) { 4849 | JSONDestroyObjectNode((pJsonObjectNode)pTmp); 4850 | } 4851 | else if (pTmp->type == JSONTYPENULL) { 4852 | JSONDestroyNullNode((pJsonNullNode)pTmp); 4853 | } 4854 | else if (pTmp->type == JSONTYPEBOOLEAN) { 4855 | JSONDestroyBooleanNode((pJsonBooleanNode)pTmp); 4856 | } 4857 | } 4858 | pArrayNode->size--; 4859 | if (pArrayNode->size == 0) { 4860 | // 释放数组空间 4861 | free(pArrayNode->array); 4862 | pArrayNode->array = NULL; 4863 | break; 4864 | } 4865 | for (int i = 0; i < pArrayNode->size; i++) { 4866 | pArrayNode->array[i] = pArrayNode->array[i + 1]; 4867 | } 4868 | pArrayNode->array[pArrayNode->size] = NULL; 4869 | } while (0); 4870 | } 4871 | 4872 | /* 4873 | * 获取数组类型的数据长度 4874 | * @param pArray pJsonArrayNode 数组节点 4875 | * @return unsigned int 数组长度 4876 | */ 4877 | unsigned int JSONArrayGetLength(pJsonArrayNode pArray /* in */) { 4878 | unsigned int len = 0; 4879 | 4880 | do { 4881 | if (!pArray) { 4882 | break; 4883 | } 4884 | if (pArray->node.type != JSONTYPEARRAY) { 4885 | break; 4886 | } 4887 | len = pArray->size; 4888 | } while (0); 4889 | 4890 | return len; 4891 | } 4892 | 4893 | /* 4894 | * 获取数组指定位置元素 4895 | * @param pArray pJsonArrayNode 数组节点 4896 | * @param pos int 4897 | * @return pJsonNode 数组元素 4898 | */ 4899 | pJsonNode JSONArrayGetNode(pJsonArrayNode pArray /* in */, int pos /* in */) { 4900 | pJsonNode pNode = NULL; 4901 | 4902 | do { 4903 | if (!pArray) { 4904 | break; 4905 | } 4906 | if (pArray->node.type != JSONTYPEARRAY) { 4907 | break; 4908 | } 4909 | if (pos < 0 || pos >= pArray->size) { 4910 | break; 4911 | } 4912 | if (pArray->array) { 4913 | pNode = pArray->array[pos]; 4914 | } 4915 | 4916 | } while (0); 4917 | 4918 | return pNode; 4919 | } 4920 | 4921 | /* 4922 | * 获取 节点 数据类型 4923 | * @param pNode pJsonNode 节点 4924 | * @return JSON_DATA_TYPE json 节点的数据类型 4925 | */ 4926 | JSON_DATA_TYPE JSONNodeGetType(pJsonNode pNode /* in */) { 4927 | JSON_DATA_TYPE type = JSONTYPEUNDEFINED; 4928 | do { 4929 | if (!pNode) { 4930 | break; 4931 | } 4932 | type = pNode->type; 4933 | } while (0); 4934 | return type; 4935 | } 4936 | 4937 | /** 4938 | * 获取 Object 属性数据类型 4939 | * @param pObject pJsonObjectNode json 对象 4940 | * @param key char* 键值 4941 | * @return JSON_DATA_TYPE json 节点的数据类型 4942 | */ 4943 | JSON_DATA_TYPE JSONObjectGetAttrType(pJsonObjectNode pObject /* in */, char* key /* in */) { 4944 | JSON_DATA_TYPE type = JSONTYPEUNDEFINED; 4945 | do { 4946 | if (!pObject) { 4947 | break; 4948 | } 4949 | if (!key || !strlen(key)) { 4950 | break; 4951 | } 4952 | int idx = JSONGetHashIndex(key); 4953 | if (idx < 0 || idx >= HASH_MAX) { 4954 | break; 4955 | } 4956 | if (pObject->table[idx]) { 4957 | // 进行链式遍历 4958 | pJsonNode pNode = pObject->table[idx]; 4959 | do { 4960 | if (strcmp(key, pNode->keyName) == 0) { 4961 | type = pNode->type; 4962 | break; 4963 | } 4964 | pNode = pNode->next; 4965 | } while (pNode); 4966 | } 4967 | } while (0); 4968 | return type; 4969 | } 4970 | 4971 | /* 4972 | * 获取数字节点的数值 4973 | * @param pNode pJsonNumberNode 数字节点 4974 | * @param pValue double* 4975 | * @return void 4976 | */ 4977 | void JSONNodeGetNumberValue(pJsonNumberNode pNode /* in */, double* pValue /* out */) { 4978 | do { 4979 | if (!pNode) { 4980 | break; 4981 | } 4982 | if (pNode->node.type != JSONTYPENUMBER) { 4983 | break; 4984 | } 4985 | *pValue = pNode->value; 4986 | } while (0); 4987 | } 4988 | 4989 | /* 4990 | * 获取长数字节点的数值 4991 | * @param pNode pJsonNumberNode 数字节点 4992 | * @param pValue UINT64* 4993 | * @return void 4994 | */ 4995 | void JSONNodeGetLongNumberValue(pJsonNumberNode pNode /* in */, UINT64* pValue /* out */){ 4996 | do { 4997 | if (!pNode) { 4998 | break; 4999 | } 5000 | if (pNode->node.type != JSONTYPELONGNUMBER) { 5001 | break; 5002 | } 5003 | *pValue = pNode->value; 5004 | } while (0); 5005 | } 5006 | 5007 | /* 5008 | * 获取字符串节点的的字符串 5009 | * @param pNode pJsonStringNode 字符串节点 5010 | * @param ppValue char** 5011 | * @return void 5012 | */ 5013 | void JSONNodeGetStringValue(pJsonStringNode pNode /* in */, char** ppValue /* out */) { 5014 | do { 5015 | if (!pNode) { 5016 | break; 5017 | } 5018 | if (pNode->node.type != JSONTYPESTRING) { 5019 | break; 5020 | } 5021 | if (!pNode->value) { 5022 | break; 5023 | } 5024 | *ppValue = (char*)malloc(strlen(pNode->value) +1); 5025 | if (!*ppValue) { 5026 | break; 5027 | } 5028 | memset(*ppValue, 0, strlen(pNode->value) + 1); 5029 | strcat(*ppValue, pNode->value); 5030 | } while (0); 5031 | } 5032 | 5033 | /* 5034 | * 获取 Boolean 节点的值 5035 | * @param pNode pJsonBooleanNode 数字节点 5036 | * @param pValue double* 5037 | * @return void 5038 | */ 5039 | void JSONNodeGetBooleanValue(pJsonBooleanNode pNode /* in */, BOOLEAN* pValue /* out */) { 5040 | do { 5041 | if (!pNode) { 5042 | break; 5043 | } 5044 | if (pNode->node.type != JSONTYPEBOOLEAN) { 5045 | break; 5046 | } 5047 | *pValue = pNode->value && 0x01; 5048 | } while (0); 5049 | } 5050 | 5051 | /* 5052 | * 获取 json 数字属性节点的数值 5053 | * @param pObject pJsonObjectNode json 对象 5054 | * @param key char* 键 5055 | * @param pValue double* 值 5056 | * @return void 5057 | */ 5058 | void JSONObjectGetNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, double* pValue /* out */) { 5059 | do { 5060 | if (!pObject) { 5061 | break; 5062 | } 5063 | if (pObject->node.type != JSONTYPEOBJECT) { 5064 | break; 5065 | } 5066 | int idx = JSONGetHashIndex(key); 5067 | if (idx < 0 || idx >= HASH_MAX) { 5068 | break; 5069 | } 5070 | if (pObject->table[idx]) { 5071 | // 进行链式遍历 5072 | pJsonNode pNode = pObject->table[idx]; 5073 | do { 5074 | if (strcmp(key, pNode->keyName) == 0) { 5075 | if (pNode->type != JSONTYPENUMBER) { 5076 | break; 5077 | } 5078 | *pValue = ((pJsonNumberNode)pNode)->value; 5079 | break; 5080 | } 5081 | pNode = pNode->next; 5082 | } while (pNode); 5083 | } 5084 | } while (0); 5085 | } 5086 | 5087 | /* 5088 | * 获取 json 长数字属性节点的数值 5089 | * @param pObject pJsonObjectNode json 对象 5090 | * @param key char* 键 5091 | * @param pValue UINT64* 值 5092 | * @return void 5093 | */ 5094 | void JSONObjectGetLongNumberAttr(pJsonObjectNode pObject /* in */, char* key /* in */, UINT64* pValue /* out */){ 5095 | do { 5096 | if (!pObject) { 5097 | break; 5098 | } 5099 | if (pObject->node.type != JSONTYPEOBJECT) { 5100 | break; 5101 | } 5102 | int idx = JSONGetHashIndex(key); 5103 | if (idx < 0 || idx >= HASH_MAX) { 5104 | break; 5105 | } 5106 | if (pObject->table[idx]) { 5107 | // 进行链式遍历 5108 | pJsonNode pNode = pObject->table[idx]; 5109 | do { 5110 | if (strcmp(key, pNode->keyName) == 0) { 5111 | if (pNode->type != JSONTYPELONGNUMBER) { 5112 | break; 5113 | } 5114 | *pValue = ((pJsonLongNumberNode)pNode)->value; 5115 | break; 5116 | } 5117 | pNode = pNode->next; 5118 | } while (pNode); 5119 | } 5120 | } while (0); 5121 | } 5122 | 5123 | /** 5124 | * 获取 json 字符串属性节点的字符串 5125 | * @param pObject pJsonObjectNode json 对象 5126 | * @param key char* 键 5127 | * @param ppValue char** 值 5128 | * @return void 5129 | */ 5130 | void JSONObjectGetStringAttr(pJsonObjectNode pObject /* in */, char* key /* in */, char** ppValue /* out */) { 5131 | do{ 5132 | if(!pObject || !key || !ppValue) { 5133 | break; 5134 | } 5135 | if(pObject->node.type != JSONTYPEOBJECT) { 5136 | break; 5137 | } 5138 | int idx = JSONGetHashIndex(key); 5139 | if(idx < 0 || idx >= HASH_MAX) { 5140 | break; 5141 | } 5142 | if(!pObject->table[idx]) { 5143 | break; 5144 | } 5145 | // 进行链式遍历 5146 | pJsonNode pNode = pObject->table[idx]; 5147 | do{ 5148 | if(strcmp(key, pNode->keyName) == 0) { 5149 | if (pNode->type != JSONTYPESTRING) { 5150 | break; 5151 | } 5152 | int len = strlen(((pJsonStringNode)pNode)->value) + 1; 5153 | *ppValue = (char*)malloc(len); 5154 | if (!*ppValue) { 5155 | break; 5156 | } 5157 | memset(*ppValue, 0, len); 5158 | memcpy(*ppValue, ((pJsonStringNode)pNode)->value, len - 1); 5159 | break; 5160 | } 5161 | pNode = pNode->next; 5162 | } while (pNode); 5163 | } while (0); 5164 | } 5165 | 5166 | /* 5167 | * 获取 json 数组属性节点的值 5168 | * @param pObject pJsonObjectNode json 对象 5169 | * @param key char* 键 5170 | * @param ppValue pJsonArrayNode* 值 5171 | * @return void 5172 | */ 5173 | void JSONObjectGetArrayAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonArrayNode* ppValue /* out */) { 5174 | do { 5175 | if (!pObject) { 5176 | break; 5177 | } 5178 | if (pObject->node.type != JSONTYPEOBJECT) { 5179 | break; 5180 | } 5181 | int idx = JSONGetHashIndex(key); 5182 | if (idx < 0 || idx >= HASH_MAX) { 5183 | break; 5184 | } 5185 | if (pObject->table[idx]) { 5186 | // 进行链式遍历 5187 | pJsonNode pNode = pObject->table[idx]; 5188 | do { 5189 | if (strcmp(key, pNode->keyName) == 0) { 5190 | if (pNode->type != JSONTYPEARRAY) { 5191 | break; 5192 | } 5193 | *ppValue = (pJsonArrayNode)pNode; 5194 | break; 5195 | } 5196 | pNode = pNode->next; 5197 | } while (pNode); 5198 | } 5199 | } while (0); 5200 | } 5201 | 5202 | /* 5203 | * 获取 json 对象属性节点的值 5204 | * @param pObject pJsonObjectNode json 对象 5205 | * @param key char* 键 5206 | * @param ppValue pJsonObjectNode* 值 5207 | * @return void 5208 | */ 5209 | void JSONObjectGetObjectAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonObjectNode* ppValue /* out */) { 5210 | do { 5211 | if (!pObject) { 5212 | break; 5213 | } 5214 | if (pObject->node.type != JSONTYPEOBJECT) { 5215 | break; 5216 | } 5217 | int idx = JSONGetHashIndex(key); 5218 | if (idx < 0 || idx >= HASH_MAX) { 5219 | break; 5220 | } 5221 | if (pObject->table[idx]) { 5222 | // 进行链式遍历 5223 | pJsonNode pNode = pObject->table[idx]; 5224 | do { 5225 | if (strcmp(key, pNode->keyName) == 0) { 5226 | if (pNode->type != JSONTYPEOBJECT) { 5227 | break; 5228 | } 5229 | *ppValue = (pJsonObjectNode)pNode; 5230 | break; 5231 | } 5232 | pNode = pNode->next; 5233 | } while (pNode); 5234 | } 5235 | } while (0); 5236 | } 5237 | 5238 | /* 5239 | * 获取 json null 属性节点的值 5240 | * @param pObject pJsonObjectNode json 对象 5241 | * @param key char* 键 5242 | * @param pValue pJsonNullNode* 5243 | * @return void 5244 | */ 5245 | void JSONObjectGetNullAttr(pJsonObjectNode pObject /* in */, char* key /* in */, pJsonNullNode* ppValue /* out */) { 5246 | do { 5247 | if (!pObject) { 5248 | break; 5249 | } 5250 | if (pObject->node.type != JSONTYPEOBJECT) { 5251 | break; 5252 | } 5253 | int idx = JSONGetHashIndex(key); 5254 | if (idx < 0 || idx >= HASH_MAX) { 5255 | break; 5256 | } 5257 | if (pObject->table[idx]) { 5258 | // 进行链式遍历 5259 | pJsonNode pNode = pObject->table[idx]; 5260 | do { 5261 | if (strcmp(key, pNode->keyName) == 0) { 5262 | if (pNode->type != JSONTYPENULL) { 5263 | break; 5264 | } 5265 | *ppValue = (pJsonNullNode)pNode; 5266 | break; 5267 | } 5268 | pNode = pNode->next; 5269 | } while (pNode); 5270 | } 5271 | } while (0); 5272 | } 5273 | 5274 | /* 5275 | * 获取 json Boolean 属性节点的值 5276 | * @param pObject pJsonObjectNode json 对象 5277 | * @param key char* 键 5278 | * @param pValue BOOLEAN* 5279 | * @return void 5280 | */ 5281 | void JSONObjectGetBooleanAttr(pJsonObjectNode pObject /* in */, char* key /* in */, BOOLEAN* pValue /* out */) { 5282 | do { 5283 | if (!pObject) { 5284 | break; 5285 | } 5286 | if (pObject->node.type != JSONTYPEOBJECT) { 5287 | break; 5288 | } 5289 | int idx = JSONGetHashIndex(key); 5290 | if (idx < 0 || idx >= HASH_MAX) { 5291 | break; 5292 | } 5293 | if (pObject->table[idx]) { 5294 | // 进行链式遍历 5295 | pJsonNode pNode = pObject->table[idx]; 5296 | do { 5297 | if (strcmp(key, pNode->keyName) == 0) { 5298 | if (pNode->type != JSONTYPEBOOLEAN) { 5299 | break; 5300 | } 5301 | *pValue = ((pJsonBooleanNode)pNode)->value && 0x01; 5302 | break; 5303 | } 5304 | pNode = pNode->next; 5305 | } while (pNode); 5306 | } 5307 | } while (0); 5308 | } 5309 | 5310 | int tabCount = 0; 5311 | 5312 | /* 5313 | * 打印 Number 5314 | * @param pNumber pJsonNumberNode 5315 | * @return void 5316 | */ 5317 | void JSONPrintNumberNode(pJsonNumberNode pNode /* in */) { 5318 | do { 5319 | if (!pNode) { 5320 | break; 5321 | } 5322 | if (pNode->node.type != JSONTYPENUMBER) { 5323 | break; 5324 | } 5325 | if (pNode->node.keyName) { 5326 | printf("\"%s\": %f,\n", pNode->node.keyName, pNode->value); 5327 | } 5328 | else { 5329 | printf("%f,", pNode->value); 5330 | } 5331 | } while (0); 5332 | } 5333 | 5334 | /* 5335 | * 打印 Number 5336 | * @param pNumber pJsonNumberNode 5337 | * @return void 5338 | */ 5339 | void JSONPrintLongNumberNode(pJsonLongNumberNode pNode /* in */) { 5340 | do { 5341 | if (!pNode) { 5342 | break; 5343 | } 5344 | if (pNode->node.type != JSONTYPELONGNUMBER) { 5345 | break; 5346 | } 5347 | if (pNode->node.keyName) { 5348 | printf("\"%s\": %llu,\n", pNode->node.keyName, pNode->value); 5349 | } 5350 | else { 5351 | printf("%llu,", pNode->value); 5352 | } 5353 | } while (0); 5354 | } 5355 | 5356 | /* 5357 | * 打印 String 5358 | * @param pNode pJsonStringNode 5359 | * @return void 5360 | */ 5361 | void JSONPrintStringNode(pJsonStringNode pNode /* in */) { 5362 | do { 5363 | if (!pNode) { 5364 | break; 5365 | } 5366 | if (pNode->node.type != JSONTYPESTRING) { 5367 | break; 5368 | } 5369 | if (pNode->node.keyName) { 5370 | printf("\"%s\": \"%s\",\n", pNode->node.keyName, pNode->value); 5371 | } 5372 | else { 5373 | printf("\"%s\",", pNode->value); 5374 | } 5375 | } while (0); 5376 | } 5377 | 5378 | /* 5379 | * 打印 Array 5380 | * @param pNode pJsonArrayNode 5381 | * @return void 5382 | */ 5383 | void JSONPrintArrayNode(pJsonArrayNode pNode /* in */) { 5384 | do { 5385 | if (!pNode) { 5386 | break; 5387 | } 5388 | if (pNode->node.type != JSONTYPEARRAY) { 5389 | break; 5390 | } 5391 | if (pNode->node.keyName) { 5392 | printf("\"%s\": [\n", pNode->node.keyName); 5393 | tabCount++; 5394 | } 5395 | else { 5396 | printf("Array: [\n"); 5397 | tabCount = 1; 5398 | } 5399 | for (int i = 0; i < tabCount; i++) { 5400 | printf("\t"); 5401 | } 5402 | for (int i = 0; i < pNode->size; i++) { 5403 | if (pNode->array[i]->type == JSONTYPENUMBER) { 5404 | JSONPrintNumberNode((pJsonNumberNode)pNode->array[i]); 5405 | } 5406 | else if (pNode->array[i]->type == JSONTYPELONGNUMBER) { 5407 | JSONPrintLongNumberNode((pJsonLongNumberNode)pNode->array[i]); 5408 | } 5409 | else if (pNode->array[i]->type == JSONTYPESTRING) { 5410 | JSONPrintStringNode((pJsonStringNode)pNode->array[i]); 5411 | } 5412 | else if (pNode->array[i]->type == JSONTYPEARRAY) { 5413 | JSONPrintArrayNode((pJsonArrayNode)pNode->array[i]); 5414 | } 5415 | else if (pNode->array[i]->type == JSONTYPEOBJECT) { 5416 | JSONPrintObjectNode((pJsonObjectNode)pNode->array[i]); 5417 | } 5418 | else if (pNode->array[i]->type == JSONTYPENULL) { 5419 | JSONPrintNullNode((pJsonNullNode)pNode->array[i]); 5420 | } 5421 | else if (pNode->array[i]->type == JSONTYPEBOOLEAN) { 5422 | JSONPrintBooleanNode((pJsonBooleanNode)pNode->array[i]); 5423 | } 5424 | } 5425 | printf("\n"); 5426 | for (int i = 0; i < tabCount - 1; i++) { 5427 | printf("\t"); 5428 | } 5429 | if (pNode->node.keyName) { 5430 | printf("],\n,"); 5431 | } 5432 | else { 5433 | printf("]\n"); 5434 | } 5435 | if (tabCount > 0) { 5436 | tabCount--; 5437 | } 5438 | } while (0); 5439 | } 5440 | 5441 | /* 5442 | * 打印 json 对象 5443 | * @param pObject json 对象 5444 | * @return void 5445 | */ 5446 | void JSONPrintObjectNode(pJsonObjectNode pObject /* in */) { 5447 | int i = 0; 5448 | do { 5449 | if (!pObject) { 5450 | break; 5451 | } 5452 | if (pObject->node.type != JSONTYPEOBJECT) { 5453 | break; 5454 | } 5455 | if (pObject->node.keyName) { 5456 | printf("\"%s\": {\n", pObject->node.keyName); 5457 | tabCount++; 5458 | } 5459 | else { 5460 | printf("Object: {\n"); 5461 | tabCount = 1; 5462 | } 5463 | 5464 | for (i = 0; i < HASH_MAX; i++) { 5465 | if (pObject->table[i]) { 5466 | pJsonNode pNode = pObject->table[i]; 5467 | do { 5468 | if (!pNode) { 5469 | break; 5470 | } 5471 | for (int i = 0; i < tabCount; i++) { 5472 | printf("\t"); 5473 | } 5474 | if (pNode->type == JSONTYPENUMBER) { 5475 | JSONPrintNumberNode((pJsonNumberNode)pNode); 5476 | } 5477 | else if (pNode->type == JSONTYPELONGNUMBER) { 5478 | JSONPrintLongNumberNode((pJsonLongNumberNode)pNode); 5479 | } 5480 | else if (pNode->type == JSONTYPESTRING) { 5481 | JSONPrintStringNode((pJsonStringNode)pNode); 5482 | } 5483 | else if (pNode->type == JSONTYPEARRAY) { 5484 | JSONPrintArrayNode((pJsonArrayNode)pNode); 5485 | } 5486 | else if (pNode->type == JSONTYPEOBJECT) { 5487 | JSONPrintObjectNode((pJsonObjectNode)pNode); 5488 | } 5489 | else if (pNode->type == JSONTYPENULL) { 5490 | JSONPrintNullNode((pJsonNullNode)pNode); 5491 | } 5492 | else if (pNode->type == JSONTYPEBOOLEAN) { 5493 | JSONPrintBooleanNode((pJsonBooleanNode)pNode); 5494 | } 5495 | for (int i = 0; i < tabCount; i++) { 5496 | printf("\b"); 5497 | } 5498 | pNode = pNode->next; 5499 | } while (pNode); 5500 | } 5501 | } 5502 | for (int i = 0; i < tabCount - 1; i++) { 5503 | printf("\t"); 5504 | } 5505 | if (pObject->node.keyName) { 5506 | printf("},\n"); 5507 | } 5508 | else { 5509 | printf("}\n"); 5510 | } 5511 | if (tabCount > 0) { 5512 | tabCount--; 5513 | } 5514 | } while (0); 5515 | } 5516 | 5517 | 5518 | /* 5519 | * 打印 null 5520 | * @param pNode pJsonNullNode 5521 | * @return void 5522 | */ 5523 | void JSONPrintNullNode(pJsonNullNode pNode /* in */) { 5524 | do { 5525 | if (!pNode) { 5526 | break; 5527 | } 5528 | if (pNode->node.type != JSONTYPENULL) { 5529 | break; 5530 | } 5531 | if (pNode->node.keyName) { 5532 | printf("\"%s\": null,\n", pNode->node.keyName); 5533 | } 5534 | else { 5535 | printf("null,"); 5536 | } 5537 | } while (0); 5538 | } 5539 | 5540 | /* 5541 | * 打印 Boolean 5542 | * @param pNode pJsonBooleanNode 5543 | * @return void 5544 | */ 5545 | void JSONPrintBooleanNode(pJsonBooleanNode pNode /* in */) { 5546 | do { 5547 | if (!pNode) { 5548 | break; 5549 | } 5550 | if (pNode->node.type != JSONTYPEBOOLEAN) { 5551 | break; 5552 | } 5553 | if (pNode->node.keyName) { 5554 | printf("\"%s\": ", pNode->node.keyName); 5555 | if (pNode->value) { 5556 | printf("true,\n"); 5557 | } 5558 | else { 5559 | printf("false,\n"); 5560 | } 5561 | } 5562 | else { 5563 | 5564 | if (pNode->value) { 5565 | printf("true,"); 5566 | } 5567 | else { 5568 | printf("false,"); 5569 | } 5570 | } 5571 | } while (0); 5572 | } 5573 | 5574 | /* 5575 | * 打印 json 节点 5576 | * @param pNode pJsonNode json 节点 5577 | * @return void 5578 | */ 5579 | void JSONPrint(pJsonNode pNode /* in */) { 5580 | do { 5581 | if (!pNode) { 5582 | break; 5583 | } 5584 | if (!pNode) { 5585 | break; 5586 | } 5587 | if (pNode->type == JSONTYPENUMBER) { 5588 | JSONPrintNumberNode((pJsonNumberNode)pNode); 5589 | } 5590 | else if (pNode->type == JSONTYPELONGNUMBER) { 5591 | JSONPrintLongNumberNode((pJsonLongNumberNode)pNode); 5592 | } 5593 | else if (pNode->type == JSONTYPESTRING) { 5594 | JSONPrintStringNode((pJsonStringNode)pNode); 5595 | } 5596 | else if (pNode->type == JSONTYPEARRAY) { 5597 | JSONPrintArrayNode((pJsonArrayNode)pNode); 5598 | } 5599 | else if (pNode->type == JSONTYPEOBJECT) { 5600 | JSONPrintObjectNode((pJsonObjectNode)pNode); 5601 | } 5602 | else if (pNode->type == JSONTYPENULL) { 5603 | JSONPrintNullNode((pJsonNullNode)pNode); 5604 | } 5605 | else if (pNode->type == JSONTYPEBOOLEAN) { 5606 | JSONPrintBooleanNode((pJsonBooleanNode)pNode); 5607 | } 5608 | } while (0); 5609 | } 5610 | --------------------------------------------------------------------------------