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>
19 #include "json_writer.h"
22 FILE *out
; /* output file */
23 unsigned depth
; /* nesting */
24 bool pretty
; /* optional whitepace */
25 char sep
; /* either nul or comma */
28 /* indentation for pretty print */
29 static void jsonw_indent(json_writer_t
*self
)
32 for (i
= 0; i
< self
->depth
; ++i
)
33 fputs(" ", self
->out
);
36 /* end current line and indent if pretty printing */
37 static void jsonw_eol(json_writer_t
*self
)
42 putc('\n', self
->out
);
46 /* If current object is not empty print a comma */
47 static void jsonw_eor(json_writer_t
*self
)
49 if (self
->sep
!= '\0')
50 putc(self
->sep
, self
->out
);
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
)
63 fputs("\\t", self
->out
);
66 fputs("\\n", self
->out
);
69 fputs("\\r", self
->out
);
72 fputs("\\f", self
->out
);
75 fputs("\\b", self
->out
);
78 fputs("\\\\", self
->out
);
81 fputs("\\\"", self
->out
);
84 putc(*str
, self
->out
);
89 /* Create a new JSON stream */
90 json_writer_t
*jsonw_new(FILE *f
)
92 json_writer_t
*self
= malloc(sizeof(*self
));
102 /* End output to JSON stream */
103 void jsonw_destroy(json_writer_t
**self_p
)
105 json_writer_t
*self
= *self_p
;
107 assert(self
->depth
== 0);
108 fputs("\n", self
->out
);
114 void jsonw_pretty(json_writer_t
*self
, bool on
)
119 void jsonw_reset(json_writer_t
*self
)
121 assert(self
->depth
== 0);
126 static void jsonw_begin(json_writer_t
*self
, int c
)
134 static void jsonw_end(json_writer_t
*self
, int c
)
136 assert(self
->depth
> 0);
139 if (self
->sep
!= '\0')
146 /* Add a JSON property name */
147 void jsonw_name(json_writer_t
*self
, const char *name
)
152 jsonw_puts(self
, name
);
153 putc(':', self
->out
);
155 putc(' ', self
->out
);
158 void jsonw_vprintf_enquote(json_writer_t
*self
, const char *fmt
, va_list ap
)
161 putc('"', self
->out
);
162 vfprintf(self
->out
, fmt
, ap
);
163 putc('"', self
->out
);
166 void jsonw_printf(json_writer_t
*self
, const char *fmt
, ...)
172 vfprintf(self
->out
, fmt
, ap
);
177 void jsonw_start_object(json_writer_t
*self
)
179 jsonw_begin(self
, '{');
182 void jsonw_end_object(json_writer_t
*self
)
184 jsonw_end(self
, '}');
187 void jsonw_start_array(json_writer_t
*self
)
189 jsonw_begin(self
, '[');
192 void jsonw_end_array(json_writer_t
*self
)
194 jsonw_end(self
, ']');
197 /* JSON value types */
198 void jsonw_string(json_writer_t
*self
, const char *value
)
201 jsonw_puts(self
, value
);
204 void jsonw_bool(json_writer_t
*self
, bool val
)
206 jsonw_printf(self
, "%s", val
? "true" : "false");
209 void jsonw_null(json_writer_t
*self
)
211 jsonw_printf(self
, "null");
214 void jsonw_float_fmt(json_writer_t
*self
, const char *fmt
, double num
)
216 jsonw_printf(self
, fmt
, num
);
220 void jsonw_float(json_writer_t
*self
, double num
)
222 jsonw_printf(self
, "%g", num
);
226 void jsonw_hu(json_writer_t
*self
, unsigned short num
)
228 jsonw_printf(self
, "%hu", num
);
231 void jsonw_uint(json_writer_t
*self
, uint64_t num
)
233 jsonw_printf(self
, "%"PRIu64
, num
);
236 void jsonw_lluint(json_writer_t
*self
, unsigned long long int num
)
238 jsonw_printf(self
, "%llu", num
);
241 void jsonw_int(json_writer_t
*self
, int64_t num
)
243 jsonw_printf(self
, "%"PRId64
, num
);
246 /* Basic name/value objects */
247 void jsonw_string_field(json_writer_t
*self
, const char *prop
, const char *val
)
249 jsonw_name(self
, prop
);
250 jsonw_string(self
, val
);
253 void jsonw_bool_field(json_writer_t
*self
, const char *prop
, bool val
)
255 jsonw_name(self
, prop
);
256 jsonw_bool(self
, val
);
260 void jsonw_float_field(json_writer_t
*self
, const char *prop
, double val
)
262 jsonw_name(self
, prop
);
263 jsonw_float(self
, val
);
267 void jsonw_float_field_fmt(json_writer_t
*self
,
272 jsonw_name(self
, prop
);
273 jsonw_float_fmt(self
, fmt
, val
);
276 void jsonw_uint_field(json_writer_t
*self
, const char *prop
, uint64_t num
)
278 jsonw_name(self
, prop
);
279 jsonw_uint(self
, num
);
282 void jsonw_hu_field(json_writer_t
*self
, const char *prop
, unsigned short num
)
284 jsonw_name(self
, prop
);
288 void jsonw_lluint_field(json_writer_t
*self
,
290 unsigned long long int num
)
292 jsonw_name(self
, prop
);
293 jsonw_lluint(self
, num
);
296 void jsonw_int_field(json_writer_t
*self
, const char *prop
, int64_t num
)
298 jsonw_name(self
, prop
);
299 jsonw_int(self
, num
);
302 void jsonw_null_field(json_writer_t
*self
, const char *prop
)
304 jsonw_name(self
, prop
);
309 int main(int argc
, char **argv
)
311 json_writer_t
*wr
= jsonw_new(stdout
);
313 jsonw_start_object(wr
);
314 jsonw_pretty(wr
, true);
315 jsonw_name(wr
, "Vyatta");
316 jsonw_start_object(wr
);
317 jsonw_string_field(wr
, "url", "http://vyatta.com");
318 jsonw_uint_field(wr
, "downloads", 2000000ul);
319 jsonw_float_field(wr
, "stock", 8.16);
321 jsonw_name(wr
, "ARGV");
322 jsonw_start_array(wr
);
324 jsonw_string(wr
, *++argv
);
327 jsonw_name(wr
, "empty");
328 jsonw_start_array(wr
);
331 jsonw_name(wr
, "NIL");
332 jsonw_start_object(wr
);
333 jsonw_end_object(wr
);
335 jsonw_null_field(wr
, "my_null");
337 jsonw_name(wr
, "special chars");
338 jsonw_start_array(wr
);
339 jsonw_string_field(wr
, "slash", "/");
340 jsonw_string_field(wr
, "newline", "\n");
341 jsonw_string_field(wr
, "tab", "\t");
342 jsonw_string_field(wr
, "ff", "\f");
343 jsonw_string_field(wr
, "quote", "\"");
344 jsonw_string_field(wr
, "tick", "\'");
345 jsonw_string_field(wr
, "backslash", "\\");
348 jsonw_end_object(wr
);
350 jsonw_end_object(wr
);