1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
3 * Simple streaming JSON writer
5 * This takes care of the annoying bits of JSON syntax like the commas
8 * Authors: Stephen Hemminger <stephen@networkplumber.org>
18 #include <linux/compiler.h>
20 #include "json_writer.h"
23 FILE *out; /* output file */
24 unsigned depth; /* nesting */
25 bool pretty; /* optional whitepace */
26 char sep; /* either nul or comma */
29 /* indentation for pretty print */
30 static void jsonw_indent(json_writer_t *self)
33 for (i = 0; i < self->depth; ++i)
34 fputs(" ", self->out);
37 /* end current line and indent if pretty printing */
38 static void jsonw_eol(json_writer_t *self)
43 putc('\n', self->out);
47 /* If current object is not empty print a comma */
48 static void jsonw_eor(json_writer_t *self)
50 if (self->sep != '\0')
51 putc(self->sep, self->out);
56 /* Output JSON encoded string */
57 /* Handles C escapes, does not do Unicode */
58 static void jsonw_puts(json_writer_t *self, const char *str)
64 fputs("\\t", self->out);
67 fputs("\\n", self->out);
70 fputs("\\r", self->out);
73 fputs("\\f", self->out);
76 fputs("\\b", self->out);
79 fputs("\\n", self->out);
82 fputs("\\\"", self->out);
85 fputs("\\\'", self->out);
88 putc(*str, self->out);
93 /* Create a new JSON stream */
94 json_writer_t *jsonw_new(FILE *f)
96 json_writer_t *self = malloc(sizeof(*self));
100 self->pretty = false;
106 /* End output to JSON stream */
107 void jsonw_destroy(json_writer_t **self_p)
109 json_writer_t *self = *self_p;
111 assert(self->depth == 0);
112 fputs("\n", self->out);
118 void jsonw_pretty(json_writer_t *self, bool on)
124 static void jsonw_begin(json_writer_t *self, int c)
132 static void jsonw_end(json_writer_t *self, int c)
134 assert(self->depth > 0);
137 if (self->sep != '\0')
144 /* Add a JSON property name */
145 void jsonw_name(json_writer_t *self, const char *name)
150 jsonw_puts(self, name);
151 putc(':', self->out);
153 putc(' ', self->out);
157 jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
160 putc('"', self->out);
161 vfprintf(self->out, fmt, ap);
162 putc('"', self->out);
165 void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...)
171 vfprintf(self->out, fmt, ap);
176 void jsonw_start_object(json_writer_t *self)
178 jsonw_begin(self, '{');
181 void jsonw_end_object(json_writer_t *self)
183 jsonw_end(self, '}');
186 void jsonw_start_array(json_writer_t *self)
188 jsonw_begin(self, '[');
191 void jsonw_end_array(json_writer_t *self)
193 jsonw_end(self, ']');
196 /* JSON value types */
197 void jsonw_string(json_writer_t *self, const char *value)
200 jsonw_puts(self, value);
203 void jsonw_bool(json_writer_t *self, bool val)
205 jsonw_printf(self, "%s", val ? "true" : "false");
208 void jsonw_null(json_writer_t *self)
210 jsonw_printf(self, "null");
213 void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num)
215 jsonw_printf(self, fmt, num);
219 void jsonw_float(json_writer_t *self, double num)
221 jsonw_printf(self, "%g", num);
225 void jsonw_hu(json_writer_t *self, unsigned short num)
227 jsonw_printf(self, "%hu", num);
230 void jsonw_uint(json_writer_t *self, uint64_t num)
232 jsonw_printf(self, "%"PRIu64, num);
235 void jsonw_lluint(json_writer_t *self, unsigned long long int num)
237 jsonw_printf(self, "%llu", num);
240 void jsonw_int(json_writer_t *self, int64_t num)
242 jsonw_printf(self, "%"PRId64, num);
245 /* Basic name/value objects */
246 void jsonw_string_field(json_writer_t *self, const char *prop, const char *val)
248 jsonw_name(self, prop);
249 jsonw_string(self, val);
252 void jsonw_bool_field(json_writer_t *self, const char *prop, bool val)
254 jsonw_name(self, prop);
255 jsonw_bool(self, val);
259 void jsonw_float_field(json_writer_t *self, const char *prop, double val)
261 jsonw_name(self, prop);
262 jsonw_float(self, val);
266 void jsonw_float_field_fmt(json_writer_t *self,
271 jsonw_name(self, prop);
272 jsonw_float_fmt(self, fmt, val);
275 void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num)
277 jsonw_name(self, prop);
278 jsonw_uint(self, num);
281 void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num)
283 jsonw_name(self, prop);
287 void jsonw_lluint_field(json_writer_t *self,
289 unsigned long long int num)
291 jsonw_name(self, prop);
292 jsonw_lluint(self, num);
295 void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num)
297 jsonw_name(self, prop);
298 jsonw_int(self, num);
301 void jsonw_null_field(json_writer_t *self, const char *prop)
303 jsonw_name(self, prop);
308 int main(int argc, char **argv)
310 json_writer_t *wr = jsonw_new(stdout);
312 jsonw_start_object(wr);
313 jsonw_pretty(wr, true);
314 jsonw_name(wr, "Vyatta");
315 jsonw_start_object(wr);
316 jsonw_string_field(wr, "url", "http://vyatta.com");
317 jsonw_uint_field(wr, "downloads", 2000000ul);
318 jsonw_float_field(wr, "stock", 8.16);
320 jsonw_name(wr, "ARGV");
321 jsonw_start_array(wr);
323 jsonw_string(wr, *++argv);
326 jsonw_name(wr, "empty");
327 jsonw_start_array(wr);
330 jsonw_name(wr, "NIL");
331 jsonw_start_object(wr);
332 jsonw_end_object(wr);
334 jsonw_null_field(wr, "my_null");
336 jsonw_name(wr, "special chars");
337 jsonw_start_array(wr);
338 jsonw_string_field(wr, "slash", "/");
339 jsonw_string_field(wr, "newline", "\n");
340 jsonw_string_field(wr, "tab", "\t");
341 jsonw_string_field(wr, "ff", "\f");
342 jsonw_string_field(wr, "quote", "\"");
343 jsonw_string_field(wr, "tick", "\'");
344 jsonw_string_field(wr, "backslash", "\\");
347 jsonw_end_object(wr);
349 jsonw_end_object(wr);