-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
248 lines (199 loc) · 5.74 KB
/
parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"parse.h"
const char * readString(JsonItem *item,const char * json);
const char * readArray(JsonItem *item,const char * json);
const char * readObject(JsonItem *item,const char * json);
const char * readNum(JsonItem *item,const char * json);
const char * parseValue(JsonItem *item, const char * json);
const char * trim(const char * str);
int parseJson(JsonItem *item, const char *json);
JsonItem * newItem();
static void printCharArray(char *array);
// #define TESTMODE
int parseJson(JsonItem *item, const char *json){
const char * ptr = parseValue(item,json);
if (!ptr) {
fprintf(stderr, "wrong format of json\n");
return -1;
}
return 0;
}
const char * parseValue(JsonItem *item, const char * json){
// 如果传入的json指针为空,报错
if(!json){ return NULL;}
json = trim(json);
// 特定类型判断
if(!strncmp(json,"null",4) ||!strncmp(json,"NULL",4)){
item->itemType = TYPE_null;
#ifdef TESTMODE
printf("%s\n","null");
#endif
// return json + 4;
return trim(json + 4);
}
if(!strncmp(json,"false",5) ||!strncmp(json,"FALSE",5)){
item->itemType = TYPE_false;
#ifdef TESTMODE
printf("%s\n","false");
#endif
// return json + 5;
return trim(json + 5);
}
if(!strncmp(json,"true",4) ||!strncmp(json,"TRUE",4)){
item->itemType = TYPE_true;
#ifdef TESTMODE
printf("%s\n","true");
#endif
// return json + 4;
return trim(json + 4);
}
// 读取string
if(*json == '\"'){
return readString(item,json);
}
// 读取数字
if(*json =='-' || (*json > '0' && *json <= '9')){
return readNum(item,json);
}
// 读取 object
if(*json == '{'){
return readObject(item,json);
}
// 读取 array
if(*json == '['){
return readArray(item,json);
}
// fail
return NULL;
}
// 指针从引号到后引号的后一位非空格
const char * readString(JsonItem *item,const char * json){
const char* ptr = json + 1;
char * out, *res;
int len = 0;
// 计算出 string 的长度,申请对应内存
while(*ptr != '\"' && *ptr && ++len){ptr++;};
res = (char*)malloc(len+1);
out = res;
if (!out){return NULL;}
// 复制string
ptr = json+1;
while(len && (*out++ = *ptr++)){len--;}
*out++ = '\0';
#ifdef TESTMODE
printCharArray(res);
#endif
item->stringValue = res;
item->itemType = TYPE_string;
// return ++ptr;
return trim(++ptr);
}
// 指针从符号位到逗号
const char * readNum(JsonItem *item,const char * json){
int num = 0, n = 0;
const char* ptr = json;
// 获取符号
signed sign = 1; // 默认为 正
if(*ptr == '-'){
sign = -1; ptr++;
}
while(*ptr != ',' && *ptr >= '0' && *ptr <= '9'){
num = 10 * n + (*ptr -'0');
n = num;
ptr++;
}
#ifdef TESTMODE
printf("%d\n",num);
#endif
item->intValue = sign * num;
item->itemType = TYPE_num;
// return ptr; // 指针指向了结束的 ,
return trim(ptr);
}
const char * readArray(JsonItem *item,const char * json){
JsonItem *child;
item->itemType = TYPE_arrry;
const char * ptr = json+1;
ptr = trim(ptr);
// 空的 array
if(*ptr == ']'){return ptr+1;}
// 构建 array
child = newItem();
if(!child){return NULL;}
item->child = child;
// 开始读取 array
ptr = parseValue(item->child,ptr);
if(!ptr) {return NULL;}
// 读取同级元素
while(*ptr == ','){
JsonItem *newitem = newItem();
if(!newItem){return NULL;}
item->child->next = newitem;
newitem->pre = item->child;
item->child = newitem;
ptr = parseValue(item->child,++ptr);
if(!ptr){return 0;}
}
if(*ptr == ']') return trim(ptr+1);
return NULL;
}
// 指针从 { 到 } 后一位
const char * readObject(JsonItem *item,const char * json){
JsonItem *child;
item->itemType = TYPE_object;
const char *ptr = json+1;
ptr = trim(ptr);
// 空的 object
if(*ptr == '}'){return ptr+1;} // return 到了 } 的下一位
// 构建 object
child = newItem();
if(!child){return NULL;}
item->child = child;
// 开始读取 object
// 先读取 string
ptr = readString(item->child,ptr);
if(!ptr){return NULL;}
item->child->stringKey = item->child->stringValue;
item->child->stringValue = NULL;
// 判断 :
if(*ptr != ':'){return NULL;}
// 读取 value
ptr = parseValue(item->child,++ptr);
if(!ptr){return NULL;}
while(*ptr == ','){
JsonItem *newitem = newItem();
if(!newitem){return NULL;}
item->child->next = newitem;
newitem->pre = item->child;
item->child = newitem;
ptr = readString(item->child,trim(++ptr));
if(!ptr){return NULL;}
item->child->stringKey = item->child->stringValue;
item->child->stringValue = NULL;
if(*ptr != ':'){return NULL;}
ptr = parseValue(item->child,++ptr);
if(!ptr){return NULL;}
}
if (*ptr=='}') return trim(ptr+1);
return NULL;
}
JsonItem * newItem(){
JsonItem *node = (JsonItem*)malloc(sizeof(JsonItem));
if(node) memset(node,0,sizeof(JsonItem));
return node;
}
static void printCharArray(char *array) {
if (array != NULL) {
while (*array != '\0') {
printf("%c", *array);
array++;
}
}
printf("\n"); // 输出换行符,以便在控制台中格式化输出
}
const char * trim(const char * str){
while(*str == ' ') str++;
return str;
}