-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_json_test.c
178 lines (147 loc) · 3.04 KB
/
easy_json_test.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
#include "easy_json.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
void print_type(ejson_base* ejson) {
if (!ejson) {
return;
}
printf("type: ");
switch(ejson->type) {
case EJSON_INT:
printf("int\n");
break;
case EJSON_DOUBLE:
printf("double\n");
break;
case EJSON_STRING:
printf("string\n");
break;
case EJSON_OBJECT:
printf("object\n");
break;
case EJSON_ARRAY:
printf("array\n");
break;
case EJSON_BOOLEAN:
printf("boolean\n");
break;
case EJSON_NULL:
printf("null\n");
break;
}
}
void print_structure(ejson_base* root);
void print_object(ejson_object* root) {
int i;
printf("{\n");
for (i = 0; i < root->length; i++) {
printf("%s: ", root->keys[i]->key);
print_structure(root->keys[i]->value);
printf("\n");
}
printf("}\n");
}
void print_array(ejson_array* root) {
printf("[\n");
int i;
for (i = 0; i < root->length; i++) {
print_structure(root->values[i]);
printf("\n");
}
printf("]");
}
void print_structure(ejson_base* root) {
if (root) {
switch (root->type) {
case EJSON_INT:
printf("%ld", ((ejson_number*) root)->value);
break;
case EJSON_DOUBLE:
printf("%f", ((ejson_real*) root)->value);
break;
case EJSON_STRING:
printf("%s", ((ejson_string*) root)->value);
break;
case EJSON_BOOLEAN:
printf("%s", ((ejson_bool*) root)->value ? "true" : "false");
break;
case EJSON_NULL:
printf("<null>");
break;
case EJSON_OBJECT:
print_object((ejson_object*) root);
break;
case EJSON_ARRAY:
print_array((ejson_array*) root);
break;
default:
printf("<Unkown type>");
break;
}
}
}
char* read_file(char* filename) {
FILE *f = fopen(filename, "rb");
if (!f) {
printf("Cannot open file.\n");
return NULL;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
if (fsize < 0) {
fprintf(stderr, "Cannot get size of file: %s\n", strerror(errno));
fclose(f);
return NULL;
}
fseek(f, 0, SEEK_SET);
char* string = calloc(fsize + 1, sizeof(char));
size_t offset = 0;
size_t read = 0;
while (offset < fsize) {
read = fread(string + offset, 1, fsize - offset, f);
if (read == 0) {
fprintf(stderr, "Cannot read full file.\n");
fclose(f);
free(string);
return NULL;
}
offset += read;
}
fclose(f);
string[fsize] = 0;
return string;
}
int main(int argc, char** argv) {
ejson_base* root = NULL;
char* test = strdup("{\"test\":1202}");
int i = 1;
int print = 0;
if (argc > 1) {
if (!strcmp(argv[1], "-h")) {
printf("%s <file>\t validates a json file.\n", argv[0]);
printf("%s -h\t shows this help.\n", argv[0]);
printf("%s -p <file>\n", argv[0]);
return 0;
} else if (!strcmp(argv[1], "-p")) {
print = 1;
i++;
}
if (argc > i) {
free(test);
test = read_file(argv[i]);
}
}
if (!test) {
return 1;
}
enum ejson_errors error = ejson_parse_warnings(test, strlen(test), true, stderr, &root);
if (print) {
print_structure(root);
printf("\n");
}
ejson_cleanup(root);
free(test);
return error != EJSON_OK;
}