-
Notifications
You must be signed in to change notification settings - Fork 81
/
test.c
182 lines (162 loc) · 2.8 KB
/
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
179
180
181
#include <stdlib.h>
#include <stdio.h>
#include "json_parser.h"
#define BUFSIZE (64 * 1024 * 1024)
void print_json_value(const json_value_t *val, int depth);
void print_json_string(const char *str)
{
printf("\"");
while (*str)
{
switch (*str)
{
case '\r':
printf("\\r");
break;
case '\n':
printf("\\n");
break;
case '\f':
printf("\\f");
break;
case '\b':
printf("\\b");
break;
case '\"':
printf("\\\"");
break;
case '\t':
printf("\\t");
break;
case '\\':
printf("\\\\");
break;
default:
if ((unsigned char)*str < 0x20)
printf("\\u00%02x", *str);
else
printf("%c", *str);
break;
}
str++;
}
printf("\"");
}
void print_json_object(const json_object_t *obj, int depth)
{
const char *name;
const json_value_t *val;
int n = 0;
int i;
if (json_object_size(obj) == 0)
{
printf("{}");
return;
}
printf("{\n");
json_object_for_each(name, val, obj)
{
if (n != 0)
printf(",\n");
n++;
for (i = 0; i < depth + 1; i++)
printf(" ");
print_json_string(name);
printf(": ");
print_json_value(val, depth + 1);
}
printf("\n");
for (i = 0; i < depth; i++)
printf(" ");
printf("}");
}
void print_json_array(const json_array_t *arr, int depth)
{
const json_value_t *val;
int n = 0;
int i;
if (json_array_size(arr) == 0)
{
printf("[]");
return;
}
printf("[\n");
json_array_for_each(val, arr)
{
if (n != 0)
printf(",\n");
n++;
for (i = 0; i < depth + 1; i++)
printf(" ");
print_json_value(val, depth + 1);
}
printf("\n");
for (i = 0; i < depth; i++)
printf(" ");
printf("]");
}
void print_json_number(double number)
{
long long integer = number;
if (integer == number)
printf("%lld", integer);
else
printf("%lf", number);
}
void print_json_value(const json_value_t *val, int depth)
{
switch (json_value_type(val))
{
case JSON_VALUE_STRING:
print_json_string(json_value_string(val));
break;
case JSON_VALUE_NUMBER:
print_json_number(json_value_number(val));
break;
case JSON_VALUE_OBJECT:
print_json_object(json_value_object(val), depth);
break;
case JSON_VALUE_ARRAY:
print_json_array(json_value_array(val), depth);
break;
case JSON_VALUE_TRUE:
printf("true");
break;
case JSON_VALUE_FALSE:
printf("false");
break;
case JSON_VALUE_NULL:
printf("null");
break;
}
}
int main()
{
static char buf[BUFSIZE];
size_t n = fread(buf, 1, BUFSIZE, stdin);
if (n > 0)
{
if (n == BUFSIZE)
{
fprintf(stderr, "File too large.\n");
exit(1);
}
buf[n] = '\0';
}
else
{
perror("fread");
exit(1);
}
json_value_t *val = json_value_parse(buf);
if (val)
{
json_value_t *val1 = json_value_copy(val);
json_value_destroy(val);
print_json_value(val1, 0);
json_value_destroy(val1);
}
else
fprintf(stderr, "Invalid JSON document.\n");
return 0;
}