Skip to content

Commit 752d852

Browse files
committed
Create test_json.c
Test file for json.c
1 parent c967d4d commit 752d852

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

tests/test_json.c

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/stat.h>
4+
5+
#include "json.h"
6+
7+
/*
8+
* Test for json.c
9+
*
10+
* Compile with
11+
* gcc -o test_json -g test_json.c json.c -lm
12+
*
13+
* USAGE: ./test_json <json_file>
14+
*/
15+
16+
static void print_depth_shift(int depth)
17+
{
18+
int j;
19+
for (j=0; j < depth; j++) {
20+
printf(" ");
21+
}
22+
}
23+
24+
static void process_value(json_value* value, int depth);
25+
26+
static void process_object(json_value* value, int depth)
27+
{
28+
int length, x;
29+
if (value == NULL) {
30+
return;
31+
}
32+
length = value->u.object.length;
33+
for (x = 0; x < length; x++) {
34+
print_depth_shift(depth);
35+
printf("object[%d].name = %s\n", x, value->u.object.values[x].name);
36+
process_value(value->u.object.values[x].value, depth+1);
37+
}
38+
}
39+
40+
static void process_array(json_value* value, int depth)
41+
{
42+
int length, x;
43+
if (value == NULL) {
44+
return;
45+
}
46+
length = value->u.array.length;
47+
printf("array\n");
48+
for (x = 0; x < length; x++) {
49+
process_value(value->u.array.values[x], depth);
50+
}
51+
}
52+
53+
static void process_value(json_value* value, int depth)
54+
{
55+
int j;
56+
if (value == NULL) {
57+
return;
58+
}
59+
if (value->type != json_object) {
60+
print_depth_shift(depth);
61+
}
62+
switch (value->type) {
63+
case json_none:
64+
printf("none\n");
65+
break;
66+
case json_object:
67+
process_object(value, depth+1);
68+
break;
69+
case json_array:
70+
process_array(value, depth+1);
71+
break;
72+
case json_integer:
73+
printf("int: %d\n", value->u.integer);
74+
break;
75+
case json_double:
76+
printf("double: %f\n", value->u.dbl);
77+
break;
78+
case json_string:
79+
printf("string: %s\n", value->u.string.ptr);
80+
break;
81+
case json_boolean:
82+
printf("bool: %d\n", value->u.boolean);
83+
break;
84+
}
85+
}
86+
87+
void main(int argc, char** argv)
88+
{
89+
char* filename;
90+
FILE *fp;
91+
struct stat filestatus;
92+
int file_size;
93+
char* file_contents;
94+
json_char* json;
95+
json_value* value;
96+
97+
if (argc != 2) {
98+
fprintf(stderr, "%s <file_json>\n", argv[0]);
99+
exit(1);
100+
}
101+
filename = argv[1];
102+
103+
if ( stat(filename, &filestatus) != 0) {
104+
fprintf(stderr, "File %s not found\n", filename);
105+
exit(1);
106+
}
107+
file_size = filestatus.st_size;
108+
file_contents = (char*)malloc(filestatus.st_size);
109+
if ( file_contents == NULL) {
110+
fprintf(stderr, "Memory error: unable to allocate %d bytes\n", file_size);
111+
exit(1);
112+
}
113+
114+
fp = fopen(filename, "rt");
115+
if (fp == NULL) {
116+
fprintf(stderr, "Unable to open %s\n", filename);
117+
fclose(fp);
118+
free(file_contents);
119+
exit(1);
120+
}
121+
if ( fread(file_contents, file_size, 1, fp) != 1 ) {
122+
fprintf(stderr, "Unable t read content of %s\n", filename);
123+
fclose(fp);
124+
free(file_contents);
125+
exit(1);
126+
}
127+
fclose(fp);
128+
129+
printf("%s\n", file_contents);
130+
131+
printf("--------------------------------\n\n");
132+
133+
json = (json_char*)file_contents;
134+
135+
value = json_parse(json,file_size);
136+
137+
if (value == NULL) {
138+
fprintf(stderr, "Unable to parse data\n");
139+
free(file_contents);
140+
exit(1);
141+
}
142+
143+
process_value(value, 0);
144+
145+
json_value_free(value);
146+
free(file_contents);
147+
exit(0);
148+
}

0 commit comments

Comments
 (0)