forked from azadkuh/gason--
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
248 lines (198 loc) · 6.91 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stddef.h>
#include "gason.hpp"
///////////////////////////////////////////////////////////////////////////////
/* sample1.json is:
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
*/
///////////////////////////////////////////////////////////////////////////////
class JSonGason
{
gason::JsonAllocator iallocator;
public:
bool doTest_sample1() {
puts("\n /---- sample1.json ---\\");
FILE* fp = fopen("../sample-jsons/sample1.json", "r+t");
if ( fp == 0 ) {
puts("sample1.json not found!");
return false;
}
// hold json data through the test
char buffer[257] = {0};
fread(buffer, 256, 1, fp);
fclose(fp);
// parsing
gason::JsonValue root;
gason::JsonParseStatus status = gason::jsonParse(buffer, root, iallocator);
// buffer will be over-written by jsonParse
if ( status != gason::JSON_PARSE_OK ) {
fprintf(stdout, "parsing failed! %d", static_cast<int>(status));
return false;
}
// finding / reading values
bool ok = false;
if ( root.child("array").at(1).toInt(&ok) != 2 ) {
puts("reading array element failed!");
}
if ( !root("number").isNumber() ) {
puts("number is not a number!");
}
if ( strncmp(root("object")("c").toString(), "d", 1) != 0 ) {
puts("readinf object.c failed!");
}
printf("string is: %s\n", root("string").toString());
// iteration
gason::JsonIterator it = gason::begin( root.child("object") );
while ( it.isValid() ) {
printf("%s = %s\n", it->key, it->value.toString());
it++;
}
// invalid object / index
size_t index = 7;
gason::JsonValue item = root("array")[index];
if ( !item ) {
printf("array[%lu] does not exist.\n", index);
} else if ( !item.isNumber() ) {
printf("array[%lu] is not a number.\n", index);
} else {
printf("array[%lu] = %d\n", index, item.toInt());
}
return true;
}
bool doTest_fathers() {
puts("\n /---- fathers.json ---\\");
FILE* fp = fopen("../sample-jsons/fathers.json", "r+t");
if ( fp == 0 ) {
puts("fathers.json not found!");
return false;
}
// hold json data through the test
static const size_t KBufferLength = 128*1024;
char buffer[KBufferLength+1] = {0};
fread(buffer, KBufferLength, 1, fp);
fclose(fp);
// parsing
gason::JsonValue root;
gason::JsonParseStatus status = gason::jsonParse(buffer, root, iallocator);
// buffer will be over-written by jsonParse
if ( status != gason::JSON_PARSE_OK ) {
puts("parsing failed!");
return false;
}
size_t childrenStat[10] = {0};
gason::JsonIterator it = gason::begin(root("fathers"));
while ( it.isValid() ) {
gason::JsonValue father = it->value;
size_t childrenCount = 0;
for ( gason::JsonIterator chit = gason::begin(father("sons")); chit.isValid(); chit++ ) {
childrenCount++;
}
for ( gason::JsonIterator chit = gason::begin(father("daughters")); chit.isValid(); chit++ ) {
childrenCount++;
}
childrenStat[childrenCount] = childrenStat[childrenCount] + 1;
it++;
}
for ( size_t i = 0; i < 10; i++ ) {
printf("%02lu father(s) has/have %lu child/children.\n", childrenStat[i], i);
}
return true;
}
bool doTest_servlet() {
puts("\n /---- servlet.json ---\\");
FILE* fp = fopen("../sample-jsons/servlet.json", "r+t");
if ( fp == 0 ) {
puts("servlet.json not found!");
return false;
}
// hold json data through the test
static const size_t KBufferLength = 4*1024;
char buffer[KBufferLength+1] = {0};
fread(buffer, KBufferLength, 1, fp);
fclose(fp);
// parsing
gason::JsonValue root;
gason::JsonParseStatus status = gason::jsonParse(buffer, root, iallocator);
// buffer will be over-written by jsonParse
if ( status != gason::JSON_PARSE_OK ) {
puts("parsing failed!");
return false;
}
bool ok = false;
gason::JsonValue deepChild = root
.child("web-app")
.child ("servlet").at(0)
.child("init-param")
.child("maxUrlLength");
if ( deepChild && deepChild.isNumber() ) {
int maxUrlLength = deepChild.toInt(&ok);
if ( maxUrlLength != 500 ) {
printf("maxUrlLength = %d, should be 500!", maxUrlLength);
} else {
puts("maxUrlLength = 500");
}
} else {
puts("can not find maxUrlLength in the specified path!");
}
return true;
}
bool doTest_makefile() {
puts("\n /---- makefile.json ---\\");
FILE* fp = fopen("../sample-jsons/makefile.json", "r+t");
if ( fp == 0 ) {
puts("makefile.json not found!");
return false;
}
// hold json data through the test
static const size_t KBufferLength = 32*1024;
char buffer[KBufferLength+1] = {0};
fread(buffer, KBufferLength, 1, fp);
fclose(fp);
// parsing
gason::JsonValue root;
gason::JsonParseStatus status = gason::jsonParse(buffer, root, iallocator);
// buffer will be over-written by jsonParse
if ( status != gason::JSON_PARSE_OK ) {
puts("parsing failed!");
return false;
}
gason::JsonValue cmd = root("command");
gason::JsonValue mak = root("data");
printf("command is: %s\n",
(cmd.isString()) ? cmd.toString() : "!!! not found" );
printf("data is: %lu bytes.\n",
(mak.isString()) ? strlen(mak.toString()) : 0 );
return true;
}
};
///////////////////////////////////////////////////////////////////////////////
int main(int , char **)
{
JSonGason tester;
for ( size_t i = 0; i < 100; i++ ) {
printf("\n\n---------------------> test iteration %03lu\n", i+1);
tester.doTest_servlet();
tester.doTest_fathers();
tester.doTest_sample1();
tester.doTest_makefile();
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////