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