]>
Commit | Line | Data |
---|---|---|
fcc16c22 SH |
1 | /* |
2 | * Simple streaming JSON writer | |
3 | * | |
4 | * This takes care of the annoying bits of JSON syntax like the commas | |
5 | * after elements | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation; either version | |
10 | * 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * Authors: Stephen Hemminger <stephen@networkplumber.org> | |
13 | */ | |
14 | ||
15 | #include <stdio.h> | |
16 | #include <stdbool.h> | |
17 | #include <stdarg.h> | |
18 | #include <assert.h> | |
19 | #include <malloc.h> | |
20 | #include <inttypes.h> | |
21 | #include <stdint.h> | |
22 | ||
23 | #include "json_writer.h" | |
24 | ||
25 | struct json_writer { | |
26 | FILE *out; /* output file */ | |
27 | unsigned depth; /* nesting */ | |
28 | bool pretty; /* optional whitepace */ | |
29 | char sep; /* either nul or comma */ | |
30 | }; | |
31 | ||
32 | /* indentation for pretty print */ | |
33 | static void jsonw_indent(json_writer_t *self) | |
34 | { | |
35 | unsigned i; | |
d721a145 | 36 | for (i = 0; i < self->depth; ++i) |
fcc16c22 SH |
37 | fputs(" ", self->out); |
38 | } | |
39 | ||
40 | /* end current line and indent if pretty printing */ | |
41 | static void jsonw_eol(json_writer_t *self) | |
42 | { | |
43 | if (!self->pretty) | |
44 | return; | |
45 | ||
46 | putc('\n', self->out); | |
47 | jsonw_indent(self); | |
48 | } | |
49 | ||
50 | /* If current object is not empty print a comma */ | |
51 | static void jsonw_eor(json_writer_t *self) | |
52 | { | |
53 | if (self->sep != '\0') | |
54 | putc(self->sep, self->out); | |
55 | self->sep = ','; | |
56 | } | |
57 | ||
58 | ||
59 | /* Output JSON encoded string */ | |
60 | /* Handles C escapes, does not do Unicode */ | |
61 | static void jsonw_puts(json_writer_t *self, const char *str) | |
62 | { | |
63 | putc('"', self->out); | |
64 | for (; *str; ++str) | |
65 | switch (*str) { | |
66 | case '\t': | |
67 | fputs("\\t", self->out); | |
68 | break; | |
69 | case '\n': | |
70 | fputs("\\n", self->out); | |
71 | break; | |
72 | case '\r': | |
73 | fputs("\\r", self->out); | |
74 | break; | |
75 | case '\f': | |
76 | fputs("\\f", self->out); | |
77 | break; | |
78 | case '\b': | |
79 | fputs("\\b", self->out); | |
80 | break; | |
81 | case '\\': | |
82 | fputs("\\n", self->out); | |
83 | break; | |
84 | case '"': | |
85 | fputs("\\\"", self->out); | |
86 | break; | |
87 | case '\'': | |
88 | fputs("\\\'", self->out); | |
89 | break; | |
90 | default: | |
91 | putc(*str, self->out); | |
92 | } | |
93 | putc('"', self->out); | |
94 | } | |
95 | ||
96 | /* Create a new JSON stream */ | |
97 | json_writer_t *jsonw_new(FILE *f) | |
98 | { | |
99 | json_writer_t *self = malloc(sizeof(*self)); | |
100 | if (self) { | |
101 | self->out = f; | |
102 | self->depth = 0; | |
103 | self->pretty = false; | |
104 | self->sep = '\0'; | |
fcc16c22 SH |
105 | } |
106 | return self; | |
107 | } | |
108 | ||
109 | /* End output to JSON stream */ | |
110 | void jsonw_destroy(json_writer_t **self_p) | |
111 | { | |
112 | json_writer_t *self = *self_p; | |
113 | ||
114 | assert(self->depth == 0); | |
d721a145 | 115 | fputs("\n", self->out); |
fcc16c22 SH |
116 | fflush(self->out); |
117 | free(self); | |
118 | *self_p = NULL; | |
119 | } | |
120 | ||
121 | void jsonw_pretty(json_writer_t *self, bool on) | |
122 | { | |
123 | self->pretty = on; | |
124 | } | |
125 | ||
126 | /* Basic blocks */ | |
127 | static void jsonw_begin(json_writer_t *self, int c) | |
128 | { | |
129 | jsonw_eor(self); | |
130 | putc(c, self->out); | |
131 | ++self->depth; | |
132 | self->sep = '\0'; | |
133 | } | |
134 | ||
135 | static void jsonw_end(json_writer_t *self, int c) | |
136 | { | |
137 | assert(self->depth > 0); | |
138 | ||
139 | --self->depth; | |
140 | if (self->sep != '\0') | |
141 | jsonw_eol(self); | |
142 | putc(c, self->out); | |
143 | self->sep = ','; | |
144 | } | |
145 | ||
146 | ||
147 | /* Add a JSON property name */ | |
148 | void jsonw_name(json_writer_t *self, const char *name) | |
149 | { | |
150 | jsonw_eor(self); | |
151 | jsonw_eol(self); | |
152 | self->sep = '\0'; | |
153 | jsonw_puts(self, name); | |
154 | putc(':', self->out); | |
155 | if (self->pretty) | |
156 | putc(' ', self->out); | |
157 | } | |
158 | ||
7252f16b | 159 | void jsonw_printf(json_writer_t *self, const char *fmt, ...) |
fcc16c22 SH |
160 | { |
161 | va_list ap; | |
162 | ||
163 | va_start(ap, fmt); | |
164 | jsonw_eor(self); | |
165 | vfprintf(self->out, fmt, ap); | |
166 | va_end(ap); | |
167 | } | |
168 | ||
169 | /* Collections */ | |
170 | void jsonw_start_object(json_writer_t *self) | |
171 | { | |
172 | jsonw_begin(self, '{'); | |
173 | } | |
174 | ||
175 | void jsonw_end_object(json_writer_t *self) | |
176 | { | |
177 | jsonw_end(self, '}'); | |
178 | } | |
179 | ||
180 | void jsonw_start_array(json_writer_t *self) | |
181 | { | |
182 | jsonw_begin(self, '['); | |
183 | } | |
184 | ||
185 | void jsonw_end_array(json_writer_t *self) | |
186 | { | |
187 | jsonw_end(self, ']'); | |
188 | } | |
189 | ||
190 | /* JSON value types */ | |
191 | void jsonw_string(json_writer_t *self, const char *value) | |
192 | { | |
193 | jsonw_eor(self); | |
194 | jsonw_puts(self, value); | |
195 | } | |
196 | ||
197 | void jsonw_bool(json_writer_t *self, bool val) | |
198 | { | |
199 | jsonw_printf(self, "%s", val ? "true" : "false"); | |
200 | } | |
201 | ||
fcc16c22 SH |
202 | void jsonw_null(json_writer_t *self) |
203 | { | |
204 | jsonw_printf(self, "null"); | |
205 | } | |
206 | ||
7252f16b JF |
207 | void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num) |
208 | { | |
209 | jsonw_printf(self, fmt, num); | |
210 | } | |
211 | ||
fcc16c22 SH |
212 | void jsonw_float(json_writer_t *self, double num) |
213 | { | |
214 | jsonw_printf(self, "%g", num); | |
215 | } | |
fcc16c22 | 216 | |
7252f16b JF |
217 | void jsonw_hu(json_writer_t *self, unsigned short num) |
218 | { | |
219 | jsonw_printf(self, "%hu", num); | |
220 | } | |
221 | ||
fcc16c22 SH |
222 | void jsonw_uint(json_writer_t *self, uint64_t num) |
223 | { | |
224 | jsonw_printf(self, "%"PRIu64, num); | |
225 | } | |
226 | ||
7252f16b JF |
227 | void jsonw_lluint(json_writer_t *self, unsigned long long int num) |
228 | { | |
229 | jsonw_printf(self, "%llu", num); | |
230 | } | |
231 | ||
fcc16c22 SH |
232 | void jsonw_int(json_writer_t *self, int64_t num) |
233 | { | |
234 | jsonw_printf(self, "%"PRId64, num); | |
235 | } | |
236 | ||
237 | /* Basic name/value objects */ | |
238 | void jsonw_string_field(json_writer_t *self, const char *prop, const char *val) | |
239 | { | |
240 | jsonw_name(self, prop); | |
241 | jsonw_string(self, val); | |
242 | } | |
243 | ||
244 | void jsonw_bool_field(json_writer_t *self, const char *prop, bool val) | |
245 | { | |
246 | jsonw_name(self, prop); | |
247 | jsonw_bool(self, val); | |
248 | } | |
249 | ||
fcc16c22 SH |
250 | void jsonw_float_field(json_writer_t *self, const char *prop, double val) |
251 | { | |
252 | jsonw_name(self, prop); | |
253 | jsonw_float(self, val); | |
254 | } | |
fcc16c22 | 255 | |
7252f16b JF |
256 | void jsonw_float_field_fmt(json_writer_t *self, |
257 | const char *prop, | |
258 | const char *fmt, | |
259 | double val) | |
260 | { | |
261 | jsonw_name(self, prop); | |
262 | jsonw_float_fmt(self, fmt, val); | |
263 | } | |
264 | ||
fcc16c22 SH |
265 | void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num) |
266 | { | |
267 | jsonw_name(self, prop); | |
268 | jsonw_uint(self, num); | |
269 | } | |
270 | ||
7252f16b JF |
271 | void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num) |
272 | { | |
273 | jsonw_name(self, prop); | |
274 | jsonw_hu(self, num); | |
275 | } | |
276 | ||
277 | void jsonw_lluint_field(json_writer_t *self, | |
278 | const char *prop, | |
279 | unsigned long long int num) | |
280 | { | |
281 | jsonw_name(self, prop); | |
282 | jsonw_lluint(self, num); | |
283 | } | |
284 | ||
fcc16c22 SH |
285 | void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num) |
286 | { | |
287 | jsonw_name(self, prop); | |
288 | jsonw_int(self, num); | |
289 | } | |
290 | ||
fcc16c22 SH |
291 | void jsonw_null_field(json_writer_t *self, const char *prop) |
292 | { | |
293 | jsonw_name(self, prop); | |
294 | jsonw_null(self); | |
295 | } | |
fcc16c22 SH |
296 | |
297 | #ifdef TEST | |
298 | int main(int argc, char **argv) | |
299 | { | |
300 | json_writer_t *wr = jsonw_new(stdout); | |
301 | ||
d721a145 | 302 | jsonw_start_object(wr); |
fcc16c22 SH |
303 | jsonw_pretty(wr, true); |
304 | jsonw_name(wr, "Vyatta"); | |
305 | jsonw_start_object(wr); | |
306 | jsonw_string_field(wr, "url", "http://vyatta.com"); | |
307 | jsonw_uint_field(wr, "downloads", 2000000ul); | |
308 | jsonw_float_field(wr, "stock", 8.16); | |
309 | ||
310 | jsonw_name(wr, "ARGV"); | |
311 | jsonw_start_array(wr); | |
312 | while (--argc) | |
313 | jsonw_string(wr, *++argv); | |
314 | jsonw_end_array(wr); | |
315 | ||
316 | jsonw_name(wr, "empty"); | |
317 | jsonw_start_array(wr); | |
318 | jsonw_end_array(wr); | |
319 | ||
320 | jsonw_name(wr, "NIL"); | |
321 | jsonw_start_object(wr); | |
322 | jsonw_end_object(wr); | |
323 | ||
324 | jsonw_null_field(wr, "my_null"); | |
325 | ||
326 | jsonw_name(wr, "special chars"); | |
327 | jsonw_start_array(wr); | |
328 | jsonw_string_field(wr, "slash", "/"); | |
329 | jsonw_string_field(wr, "newline", "\n"); | |
330 | jsonw_string_field(wr, "tab", "\t"); | |
331 | jsonw_string_field(wr, "ff", "\f"); | |
332 | jsonw_string_field(wr, "quote", "\""); | |
333 | jsonw_string_field(wr, "tick", "\'"); | |
334 | jsonw_string_field(wr, "backslash", "\\"); | |
335 | jsonw_end_array(wr); | |
336 | ||
337 | jsonw_end_object(wr); | |
338 | ||
d721a145 | 339 | jsonw_end_object(wr); |
fcc16c22 SH |
340 | jsonw_destroy(&wr); |
341 | return 0; | |
342 | } | |
343 | ||
344 | #endif |