1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
3 * Simple streaming JSON writer
5 * This takes care of the annoying bits of JSON syntax like the commas
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * Authors: Stephen Hemminger <stephen@networkplumber.org>
23 #include <linux/compiler.h>
25 #include "json_writer.h"
28 FILE *out; /* output file */
29 unsigned depth; /* nesting */
30 bool pretty; /* optional whitepace */
31 char sep; /* either nul or comma */
34 /* indentation for pretty print */
35 static void jsonw_indent(json_writer_t *self)
38 for (i = 0; i < self->depth; ++i)
39 fputs(" ", self->out);
42 /* end current line and indent if pretty printing */
43 static void jsonw_eol(json_writer_t *self)
48 putc('\n', self->out);
52 /* If current object is not empty print a comma */
53 static void jsonw_eor(json_writer_t *self)
55 if (self->sep != '\0')
56 putc(self->sep, self->out);
61 /* Output JSON encoded string */
62 /* Handles C escapes, does not do Unicode */
63 static void jsonw_puts(json_writer_t *self, const char *str)
69 fputs("\\t", self->out);
72 fputs("\\n", self->out);
75 fputs("\\r", self->out);
78 fputs("\\f", self->out);
81 fputs("\\b", self->out);
84 fputs("\\n", self->out);
87 fputs("\\\"", self->out);
90 fputs("\\\'", self->out);
93 putc(*str, self->out);
98 /* Create a new JSON stream */
99 json_writer_t *jsonw_new(FILE *f)
101 json_writer_t *self = malloc(sizeof(*self));
105 self->pretty = false;
111 /* End output to JSON stream */
112 void jsonw_destroy(json_writer_t **self_p)
114 json_writer_t *self = *self_p;
116 assert(self->depth == 0);
117 fputs("\n", self->out);
123 void jsonw_pretty(json_writer_t *self, bool on)
129 static void jsonw_begin(json_writer_t *self, int c)
137 static void jsonw_end(json_writer_t *self, int c)
139 assert(self->depth > 0);
142 if (self->sep != '\0')
149 /* Add a JSON property name */
150 void jsonw_name(json_writer_t *self, const char *name)
155 jsonw_puts(self, name);
156 putc(':', self->out);
158 putc(' ', self->out);
162 jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
165 putc('"', self->out);
166 vfprintf(self->out, fmt, ap);
167 putc('"', self->out);
170 void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...)
176 vfprintf(self->out, fmt, ap);
181 void jsonw_start_object(json_writer_t *self)
183 jsonw_begin(self, '{');
186 void jsonw_end_object(json_writer_t *self)
188 jsonw_end(self, '}');
191 void jsonw_start_array(json_writer_t *self)
193 jsonw_begin(self, '[');
196 void jsonw_end_array(json_writer_t *self)
198 jsonw_end(self, ']');
201 /* JSON value types */
202 void jsonw_string(json_writer_t *self, const char *value)
205 jsonw_puts(self, value);
208 void jsonw_bool(json_writer_t *self, bool val)
210 jsonw_printf(self, "%s", val ? "true" : "false");
213 void jsonw_null(json_writer_t *self)
215 jsonw_printf(self, "null");
218 void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num)
220 jsonw_printf(self, fmt, num);
224 void jsonw_float(json_writer_t *self, double num)
226 jsonw_printf(self, "%g", num);
230 void jsonw_hu(json_writer_t *self, unsigned short num)
232 jsonw_printf(self, "%hu", num);
235 void jsonw_uint(json_writer_t *self, uint64_t num)
237 jsonw_printf(self, "%"PRIu64, num);
240 void jsonw_lluint(json_writer_t *self, unsigned long long int num)
242 jsonw_printf(self, "%llu", num);
245 void jsonw_int(json_writer_t *self, int64_t num)
247 jsonw_printf(self, "%"PRId64, num);
250 /* Basic name/value objects */
251 void jsonw_string_field(json_writer_t *self, const char *prop, const char *val)
253 jsonw_name(self, prop);
254 jsonw_string(self, val);
257 void jsonw_bool_field(json_writer_t *self, const char *prop, bool val)
259 jsonw_name(self, prop);
260 jsonw_bool(self, val);
264 void jsonw_float_field(json_writer_t *self, const char *prop, double val)
266 jsonw_name(self, prop);
267 jsonw_float(self, val);
271 void jsonw_float_field_fmt(json_writer_t *self,
276 jsonw_name(self, prop);
277 jsonw_float_fmt(self, fmt, val);
280 void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num)
282 jsonw_name(self, prop);
283 jsonw_uint(self, num);
286 void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num)
288 jsonw_name(self, prop);
292 void jsonw_lluint_field(json_writer_t *self,
294 unsigned long long int num)
296 jsonw_name(self, prop);
297 jsonw_lluint(self, num);
300 void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num)
302 jsonw_name(self, prop);
303 jsonw_int(self, num);
306 void jsonw_null_field(json_writer_t *self, const char *prop)
308 jsonw_name(self, prop);
313 int main(int argc, char **argv)
315 json_writer_t *wr = jsonw_new(stdout);
317 jsonw_start_object(wr);
318 jsonw_pretty(wr, true);
319 jsonw_name(wr, "Vyatta");
320 jsonw_start_object(wr);
321 jsonw_string_field(wr, "url", "http://vyatta.com");
322 jsonw_uint_field(wr, "downloads", 2000000ul);
323 jsonw_float_field(wr, "stock", 8.16);
325 jsonw_name(wr, "ARGV");
326 jsonw_start_array(wr);
328 jsonw_string(wr, *++argv);
331 jsonw_name(wr, "empty");
332 jsonw_start_array(wr);
335 jsonw_name(wr, "NIL");
336 jsonw_start_object(wr);
337 jsonw_end_object(wr);
339 jsonw_null_field(wr, "my_null");
341 jsonw_name(wr, "special chars");
342 jsonw_start_array(wr);
343 jsonw_string_field(wr, "slash", "/");
344 jsonw_string_field(wr, "newline", "\n");
345 jsonw_string_field(wr, "tab", "\t");
346 jsonw_string_field(wr, "ff", "\f");
347 jsonw_string_field(wr, "quote", "\"");
348 jsonw_string_field(wr, "tick", "\'");
349 jsonw_string_field(wr, "backslash", "\\");
352 jsonw_end_object(wr);
354 jsonw_end_object(wr);