-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonparser.cpp
259 lines (223 loc) · 5.14 KB
/
jsonparser.cpp
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
#include "jsonparser.h"
jsonparsercustom::jsonparsercustom(string FILE) // parameterized constructor to open input file when object is created
{
file.open(FILE);
}
bool jsonparsercustom::checkDelimit(char piece) // Checks whether a given character is a delimiter
{
char delimit[]={'{','}','[',']',',',':','"'};
int i=-1;
do
{
i++;
if(piece==delimit[i])
return true;
}while(delimit[i]!='"');
return false;
}
string jsonparsercustom::readstring() //Reads and stores a string
{
char piece;
string token="";
while(1)
{
file>>std::noskipws>>piece; //reads string without skipping white spaces
if(piece == '"')
return token;
token +=piece;
}
}
vector<string> jsonparsercustom::lexer() //This functions job is to tokenize the various elements of a json grammar
// in a vector
{
stack<char> s;
char piece;
string temp;
string token="";
std::vector<string> value;
int flag=0;
do
{
file >>std::skipws>>piece;
if(piece=='{')
{
flag=1;
token+=piece;
s.push(piece);
value.push_back(token);
token="";
}
else if(piece=='}')
{
token+=piece;
s.pop();
value.push_back(token);
token="";
}
else if(flag==1)
{
if (checkDelimit(piece))
{
if(piece=='"')
{
temp=piece;
value.push_back(temp);
char ab;
while(1)
{
ab=piece;
file>>std::noskipws>>piece;
if(piece=='"')
{
if(ab!='\\')
break;
}
token=token+piece;
}
//token=readstring();
value.push_back(token);
value.push_back(temp);
token="";
}
else
{
temp = piece;
if (token != "") {
value.push_back(token);
value.push_back(temp);
token = "";
}
else
value.push_back(temp);
}
}
else
token += piece;
}
}while((!s.empty())||(flag==0));
return value;
}
bool jsonparsercustom::boolchecker(string s) //check a given string is boolean datatype
{
if(s=="true")
return true;
else
return false;
}
bool jsonparsercustom::isFloat(string s) //check a given string is float or not
{
char k;
for(int i=0;i<s.length();i++)
{
k=s.at(i);
if(!((k>='0'&&k<='9')||k=='.'))
return false;
}
return true;
}
int jsonparsercustom::checker(string s) //driver function of checking functions
{
if(s=="true"||s=="false")
return 1;
if(isFloat(s))
return 2;
if(s=="[")
return 3;
if(s=="{")
return 4;
if(s[0]=='"')
return 5;
/*if(checkDelimit(s[0]))
return 4;*/
return 0;
}
void jsonparsercustom::printval(std::vector<string> v) //prints the elements in current json object
{
cout<<endl;
int count=0;
for (auto it = v.begin(); it != v.end(); ++it)
cout<<*it;
cout<<endl;
}
json_list jsonparsercustom::addList() //to add list in the json file to our container
{
//stack<string> list1;
json_list jl;
while(*it!="]")
{
int check=checker(*it);
switch(check)
{
case 1:
jl.list.push_back(json_value(boolchecker(*it)));
break;
case 2:
jl.list.push_back(json_value(stof(*it)));
break;
case 3:
it++;
jl.list.push_back(json_value(addList()));
break;
case 4:
it++;
jl.list.push_back(json_value(addObject()));
break;
case 5:
jl.list.push_back(json_value(*(++it)));
++it;
break;
}
++it;
}
return jl;
}
json_object jsonparsercustom::addObject() //same as above with json_object
{
json_object j1;
string temp;
while((*it)!="}")
{
if((*it)[0]=='"' && *(it+3)==":")
{
int check=checker(*(it+4));
temp=*(it+1);
switch(check)
{
case 1:
it=it+4;
j1.obj[temp]=json_value(boolchecker(*(it)));
break;
case 2:
it=it+4;
j1.obj[temp]=json_value(stof(*(it)));
break;
case 3:
it=it+5;
j1.obj[temp]=json_value(addList());
break;
case 4:
it=it+5;
j1.obj[temp]=json_value(addObject());
break;
case 5:
it=it+5;
j1.obj[temp]=json_value(*(it));
++it;
break;
}
}
++it;
}
return j1;
}
json_object jsonparsercustom::JSONreadFile(bool print) //the main file reader and calls other functions
{
json_object jo;
std::vector<string> v=lexer();
it=v.begin();
it=it+1;
jo=addObject();
if(print)
printval(v);
return jo;
}