Skip to content

Commit c8f0af9

Browse files
author
James McLaughlin
committed
Merge pull request json-parser#48 from pasqumirk/master
Added test program for C library
2 parents c967d4d + b83bc11 commit c8f0af9

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

examples/test_json.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/* vim: set et ts=4
2+
*
3+
* Copyright (C) 2015 Mirko Pasqualetti All rights reserved.
4+
* https://github.com/udp/json-parser
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*/
29+
30+
#include <stdio.h>
31+
#include <stdlib.h>
32+
#include <sys/stat.h>
33+
34+
#include "json.h"
35+
36+
/*
37+
* Test for json.c
38+
*
39+
* Compile with
40+
* gcc -o test_json -I.. test_json.c ../json.c -lm
41+
*
42+
* USAGE: ./test_json <json_file>
43+
*/
44+
45+
static void print_depth_shift(int depth)
46+
{
47+
int j;
48+
for (j=0; j < depth; j++) {
49+
printf(" ");
50+
}
51+
}
52+
53+
static void process_value(json_value* value, int depth);
54+
55+
static void process_object(json_value* value, int depth)
56+
{
57+
int length, x;
58+
if (value == NULL) {
59+
return;
60+
}
61+
length = value->u.object.length;
62+
for (x = 0; x < length; x++) {
63+
print_depth_shift(depth);
64+
printf("object[%d].name = %s\n", x, value->u.object.values[x].name);
65+
process_value(value->u.object.values[x].value, depth+1);
66+
}
67+
}
68+
69+
static void process_array(json_value* value, int depth)
70+
{
71+
int length, x;
72+
if (value == NULL) {
73+
return;
74+
}
75+
length = value->u.array.length;
76+
printf("array\n");
77+
for (x = 0; x < length; x++) {
78+
process_value(value->u.array.values[x], depth);
79+
}
80+
}
81+
82+
static void process_value(json_value* value, int depth)
83+
{
84+
int j;
85+
if (value == NULL) {
86+
return;
87+
}
88+
if (value->type != json_object) {
89+
print_depth_shift(depth);
90+
}
91+
switch (value->type) {
92+
case json_none:
93+
printf("none\n");
94+
break;
95+
case json_object:
96+
process_object(value, depth+1);
97+
break;
98+
case json_array:
99+
process_array(value, depth+1);
100+
break;
101+
case json_integer:
102+
printf("int: %d\n", value->u.integer);
103+
break;
104+
case json_double:
105+
printf("double: %f\n", value->u.dbl);
106+
break;
107+
case json_string:
108+
printf("string: %s\n", value->u.string.ptr);
109+
break;
110+
case json_boolean:
111+
printf("bool: %d\n", value->u.boolean);
112+
break;
113+
}
114+
}
115+
116+
int main(int argc, char** argv)
117+
{
118+
char* filename;
119+
FILE *fp;
120+
struct stat filestatus;
121+
int file_size;
122+
char* file_contents;
123+
json_char* json;
124+
json_value* value;
125+
126+
if (argc != 2) {
127+
fprintf(stderr, "%s <file_json>\n", argv[0]);
128+
return 1;
129+
}
130+
filename = argv[1];
131+
132+
if ( stat(filename, &filestatus) != 0) {
133+
fprintf(stderr, "File %s not found\n", filename);
134+
return 1;
135+
}
136+
file_size = filestatus.st_size;
137+
file_contents = (char*)malloc(filestatus.st_size);
138+
if ( file_contents == NULL) {
139+
fprintf(stderr, "Memory error: unable to allocate %d bytes\n", file_size);
140+
return 1;
141+
}
142+
143+
fp = fopen(filename, "rt");
144+
if (fp == NULL) {
145+
fprintf(stderr, "Unable to open %s\n", filename);
146+
fclose(fp);
147+
free(file_contents);
148+
return 1;
149+
}
150+
if ( fread(file_contents, file_size, 1, fp) != 1 ) {
151+
fprintf(stderr, "Unable t read content of %s\n", filename);
152+
fclose(fp);
153+
free(file_contents);
154+
return 1;
155+
}
156+
fclose(fp);
157+
158+
printf("%s\n", file_contents);
159+
160+
printf("--------------------------------\n\n");
161+
162+
json = (json_char*)file_contents;
163+
164+
value = json_parse(json,file_size);
165+
166+
if (value == NULL) {
167+
fprintf(stderr, "Unable to parse data\n");
168+
free(file_contents);
169+
exit(1);
170+
}
171+
172+
process_value(value, 0);
173+
174+
json_value_free(value);
175+
free(file_contents);
176+
return 0;
177+
}

0 commit comments

Comments
 (0)