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