-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonReader.c
More file actions
322 lines (256 loc) · 7.99 KB
/
JsonReader.c
File metadata and controls
322 lines (256 loc) · 7.99 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* JsonReader.c
*
* Created on: Dec 6, 2013
* Last Edited: Jan 26, 2015
* Author: Nehchal J.
* Email: nehchalj@gmail.com
*/
/* ======= Revision History ========
* Dec 6, 2013 | New JsonReader library created.
*/
#include "JsonReader.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define isAlphabet(ch) ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
#define isDigit(ch) (ch >= '0' && ch <= '9')
void JsonReader_init(tJsonReader *jsonReader, const char *jsonObj){
jsonReader->ptr=0; //point to the start of the string
jsonReader->str = jsonObj;
}
void JsonReader_beginArray(tJsonReader *jsonReader){
/* Array can be outermost container of Json string. It can be a value in
* key-value pair. It can be an element of array.
*/
while(jsonReader->str[jsonReader->ptr++] != '[');
}
void JsonReader_beginObject(tJsonReader* jsonReader){
/* Object may be outermost container of Json string. It can be a value in
* key-value pair. It can be element of an array.
*/
while(jsonReader->str[jsonReader->ptr++] != '{');
}
void JsonReader_endArray(tJsonReader* jsonReader){
/* The array should not be ended till all the elements have been read
* because ']' may be a part of name/string in subsequent elements. */
while(jsonReader->str[jsonReader->ptr++] != ']');
}
void JsonReader_endObject(tJsonReader* jsonReader){
/* An object should not be ended till all the pairs have been read
* because '}' may be a part of name/string in subsequent pairs. */
while(jsonReader->str[jsonReader->ptr++]!='}');
}
boolean JsonReader_hasNext(tJsonReader* jsonReader){
/* Return false if all characters before '}' or ']' are not alphabets,
* or are not digits or none of '[', '{', '"', ',' */
int lookAheadPtr;
char ch;
lookAheadPtr = jsonReader->ptr;
while ( (ch = jsonReader->str[lookAheadPtr]) != '\0' ){ //till end of str
if ( isAlphabet(ch) || isDigit(ch) ||
ch=='[' || ch=='{' || ch=='"' || ch==',' )
return true;
if (ch=='}' || ch==']')
return false;
lookAheadPtr++;
}
return false; //this line will not be reached
}
/*
* Returns the boolean value of next token, consuming it
*/
boolean JsonReader_nextBoolean(tJsonReader* jsonReader){
while(jsonReader->str[jsonReader->ptr]!='t' &&
jsonReader->str[jsonReader->ptr]!='f'){
jsonReader->ptr++;
}
if (!strncmp("true", jsonReader->str + jsonReader->ptr, 4)){
jsonReader->ptr += 4;
return true;
}
else if (!strncmp("false",jsonReader->str + jsonReader->ptr,5)){
jsonReader->ptr += 5;
return false;
}
return false; //this line will not be reached for valid Json string
}
int JsonReader_nextInt(tJsonReader* jsonReader){
/* For 32-bit range, integer lies between -32768 and 32767 (2^15-1).
* So, max string length for double can be 5. */
char ch;
char sInt[6];
int index = 0;
/* move ahead ptr till +/- symbol or digit is encountered */
ch = jsonReader->str[jsonReader->ptr];
while( ch != '+' && ch != '-' && !isDigit(ch) ) ch = jsonReader->str[++jsonReader->ptr];
ch = jsonReader->str[jsonReader->ptr];
while ( ch == '+' || ch == '-' || isDigit(ch) ){ //till is +/- symbol or digit
sInt[index++] = ch;
jsonReader->ptr++;
ch = jsonReader->str[jsonReader->ptr];
}
sInt[index]='\0';
return atoi(sInt);
}
long JsonReader_nextLong(tJsonReader* jsonReader){
/* For 32-bit range, double lies between -2147483647 and
* 2147483647 (2^31-1). So, maximum string length for double can be 11.
* Code will be same as nextInt() fxn with few minor changes. */
char ch;
char sLong[12];
int index = 0;
/* move ahead ptr till +/- symbol or digit is encountered */
ch = jsonReader->str[jsonReader->ptr];
while( ch != '+' && ch != '-' && !isDigit(ch) ) ch = jsonReader->str[++jsonReader->ptr];
ch = jsonReader->str[jsonReader->ptr];
while ( ch == '+' || ch == '-' || isDigit(ch) ){ //till is +/- symbol or digit
sLong[index++] = ch;
jsonReader->ptr++;
ch = jsonReader->str[jsonReader->ptr];
}
sLong[index]='\0';
return atol(sLong);
}
float JsonReader_nextFloat(tJsonReader* jsonReader){
/* For 32-bit range, double lies between -2147483647 and
* 2147483647 (2^31-1). So, maximum string length for double can be 11.
* Code will be same as nextInt() fxn with few minor changes. */
char ch;
char sFloat[12];
int index = 0;
/* move ahead ptr till +/- symbol or digit or '.' is encountered */
ch = jsonReader->str[jsonReader->ptr];
while( ch != '+' && ch != '-' && !isDigit(ch) && ch != '.') ch = jsonReader->str[++jsonReader->ptr];
ch = jsonReader->str[jsonReader->ptr];
while ( ch == '+' || ch == '-' || isDigit(ch) || ch =='.' ){ //till is +/- symbol or digit
sFloat[index++] = ch;
jsonReader->ptr++;
ch = jsonReader->str[jsonReader->ptr];
}
sFloat[index]='\0';
return atof(sFloat);
}
char* JsonReader_nextName(tJsonReader* jsonReader, char *name){
/* Read the string between next two double quotes. Double quote can be
* a part of name too, but then it would be preceded by backslash. This
* has been handled.
*/
int index=0;
char ch;
/* Move ahead till double quote is encountered */
while (jsonReader->str[jsonReader->ptr++] != '"');
while( (ch = jsonReader->str[jsonReader->ptr]) != '"' ||
jsonReader->str[jsonReader->ptr - 1] == '\\' ){
name[index++] = ch;
jsonReader->ptr++;
}
jsonReader->ptr++; //to move the ptr ahead of closing double quote
name[index]='\0';
return name;
}
void JsonReader_nextNull(tJsonReader* jsonReader){
/* Move forward till character 'n' is encoutered */
while(jsonReader->str[jsonReader->ptr++] != 'n');
/* ptr is pointing at 'u' */
if (!strncmp("null", jsonReader->str + jsonReader->ptr - 1, 4)){
jsonReader->ptr += 3;
return;
}
}
/*
* Returns the string value of next token, consuming it
*/
char* JsonReader_nextString(tJsonReader* jsonReader, char *str){
/* It is same as getting the nextName() fxn */
return JsonReader_nextName(jsonReader, str);
}
/*
* skips a string and moves the ptr forward
*/
static void skipString(tJsonReader *jsonReader){
/* Code is similar to nextString() fxn */
/* Move ahead till double quote is encountered */
while (jsonReader->str[jsonReader->ptr++] != '"');
while( jsonReader->str[jsonReader->ptr] != '"' ||
jsonReader->str[jsonReader->ptr - 1] == '\\' ){
jsonReader->ptr++;
}
jsonReader->ptr++; //to move the ptr ahead of closing double quote
}
/*
* skips a object or array value and moves the ptr forward
*/
static void skipObjectOrArray(tJsonReader *jsonReader){
int numCurlyOpBrckt=0, numCurlyClBrckt=0;
int numSqOpBrckt=0, numSqClBrckt=0;
char ch;
/* find at the beginning of object/array */
ch = jsonReader->str[jsonReader->ptr++];
while(ch != '{' && ch != '[' && ch != '\0') ch=jsonReader->str[jsonReader->ptr++];
if (jsonReader->str[jsonReader->ptr - 1] == '{'){
numCurlyOpBrckt = 1;
numSqOpBrckt = 0;
}
else{
numCurlyOpBrckt = 0;
numSqOpBrckt = 1;
}
while(numCurlyOpBrckt != numCurlyClBrckt || numSqOpBrckt != numSqClBrckt ){
switch ( (ch = jsonReader->str[jsonReader->ptr++]) ){
case '{':
numCurlyOpBrckt++;
break;
case '}':
numCurlyClBrckt++;
break;
case '[':
numSqOpBrckt++;
break;
case ']':
numSqClBrckt++;
break;
default:
break;
}
}
return;
}
void JsonReader_skipValue(tJsonReader* jsonReader){
int lookAhead;
char ch;
int isSkipped=0;
lookAhead = jsonReader->ptr;
while( (ch = jsonReader->str[lookAhead++]) != '\0'){
switch(ch){
case '"': //value is a string
skipString(jsonReader);
isSkipped=1;
break;
case 'n': //value is null
JsonReader_nextNull(jsonReader);
isSkipped=1;
break;
case 't': case 'f': //value is Boolean
JsonReader_nextBoolean(jsonReader);
isSkipped=1;
break;
case '{': case '[': //value is object or array
skipObjectOrArray(jsonReader);
isSkipped = 1;
break;
case '+': case '-':
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
case '.': //value is a integer or a float
JsonReader_nextFloat(jsonReader);
isSkipped = 1;
break;
default:
break;
}
if (isSkipped){
return;
}
}
return;
}