2 * Simple streaming JSON writer
4 * This takes care of the annoying bits of JSON syntax like the commas
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Authors: Stephen Hemminger <stephen@networkplumber.org>
23 #include "json_writer.h"
26 FILE *out
; /* output file */
27 unsigned depth
; /* nesting */
28 bool pretty
; /* optional whitepace */
29 char sep
; /* either nul or comma */
32 /* indentation for pretty print */
33 static void jsonw_indent(json_writer_t
*self
)
36 for (i
= 0; i
< self
->depth
; ++i
)
37 fputs(" ", self
->out
);
40 /* end current line and indent if pretty printing */
41 static void jsonw_eol(json_writer_t
*self
)
46 putc('\n', self
->out
);
50 /* If current object is not empty print a comma */
51 static void jsonw_eor(json_writer_t
*self
)
53 if (self
->sep
!= '\0')
54 putc(self
->sep
, self
->out
);
59 /* Output JSON encoded string */
60 /* Handles C escapes, does not do Unicode */
61 static void jsonw_puts(json_writer_t
*self
, const char *str
)
67 fputs("\\t", self
->out
);
70 fputs("\\n", self
->out
);
73 fputs("\\r", self
->out
);
76 fputs("\\f", self
->out
);
79 fputs("\\b", self
->out
);
82 fputs("\\n", self
->out
);
85 fputs("\\\"", self
->out
);
88 fputs("\\\'", self
->out
);
91 putc(*str
, self
->out
);
96 /* Create a new JSON stream */
97 json_writer_t
*jsonw_new(FILE *f
)
99 json_writer_t
*self
= malloc(sizeof(*self
));
103 self
->pretty
= false;
109 /* End output to JSON stream */
110 void jsonw_destroy(json_writer_t
**self_p
)
112 json_writer_t
*self
= *self_p
;
114 assert(self
->depth
== 0);
115 fputs("\n", self
->out
);
121 void jsonw_pretty(json_writer_t
*self
, bool on
)
127 static void jsonw_begin(json_writer_t
*self
, int c
)
135 static void jsonw_end(json_writer_t
*self
, int c
)
137 assert(self
->depth
> 0);
140 if (self
->sep
!= '\0')
147 /* Add a JSON property name */
148 void jsonw_name(json_writer_t
*self
, const char *name
)
153 jsonw_puts(self
, name
);
154 putc(':', self
->out
);
156 putc(' ', self
->out
);
159 void jsonw_vprintf_enquote(json_writer_t
*self
, const char *fmt
, va_list ap
)
162 putc('"', self
->out
);
163 vfprintf(self
->out
, fmt
, ap
);
164 putc('"', self
->out
);
167 void jsonw_printf(json_writer_t
*self
, const char *fmt
, ...)
173 vfprintf(self
->out
, fmt
, ap
);
178 void jsonw_start_object(json_writer_t
*self
)
180 jsonw_begin(self
, '{');
183 void jsonw_end_object(json_writer_t
*self
)
185 jsonw_end(self
, '}');
188 void jsonw_start_array(json_writer_t
*self
)
190 jsonw_begin(self
, '[');
193 void jsonw_end_array(json_writer_t
*self
)
195 jsonw_end(self
, ']');
198 /* JSON value types */
199 void jsonw_string(json_writer_t
*self
, const char *value
)
202 jsonw_puts(self
, value
);
205 void jsonw_bool(json_writer_t
*self
, bool val
)
207 jsonw_printf(self
, "%s", val
? "true" : "false");
210 void jsonw_null(json_writer_t
*self
)
212 jsonw_printf(self
, "null");
215 void jsonw_float_fmt(json_writer_t
*self
, const char *fmt
, double num
)
217 jsonw_printf(self
, fmt
, num
);
221 void jsonw_float(json_writer_t
*self
, double num
)
223 jsonw_printf(self
, "%g", num
);
227 void jsonw_hu(json_writer_t
*self
, unsigned short num
)
229 jsonw_printf(self
, "%hu", num
);
232 void jsonw_uint(json_writer_t
*self
, uint64_t num
)
234 jsonw_printf(self
, "%"PRIu64
, num
);
237 void jsonw_lluint(json_writer_t
*self
, unsigned long long int num
)
239 jsonw_printf(self
, "%llu", num
);
242 void jsonw_int(json_writer_t
*self
, int64_t num
)
244 jsonw_printf(self
, "%"PRId64
, num
);
247 /* Basic name/value objects */
248 void jsonw_string_field(json_writer_t
*self
, const char *prop
, const char *val
)
250 jsonw_name(self
, prop
);
251 jsonw_string(self
, val
);
254 void jsonw_bool_field(json_writer_t
*self
, const char *prop
, bool val
)
256 jsonw_name(self
, prop
);
257 jsonw_bool(self
, val
);
261 void jsonw_float_field(json_writer_t
*self
, const char *prop
, double val
)
263 jsonw_name(self
, prop
);
264 jsonw_float(self
, val
);
268 void jsonw_float_field_fmt(json_writer_t
*self
,
273 jsonw_name(self
, prop
);
274 jsonw_float_fmt(self
, fmt
, val
);
277 void jsonw_uint_field(json_writer_t
*self
, const char *prop
, uint64_t num
)
279 jsonw_name(self
, prop
);
280 jsonw_uint(self
, num
);
283 void jsonw_hu_field(json_writer_t
*self
, const char *prop
, unsigned short num
)
285 jsonw_name(self
, prop
);
289 void jsonw_lluint_field(json_writer_t
*self
,
291 unsigned long long int num
)
293 jsonw_name(self
, prop
);
294 jsonw_lluint(self
, num
);
297 void jsonw_int_field(json_writer_t
*self
, const char *prop
, int64_t num
)
299 jsonw_name(self
, prop
);
300 jsonw_int(self
, num
);
303 void jsonw_null_field(json_writer_t
*self
, const char *prop
)
305 jsonw_name(self
, prop
);
310 int main(int argc
, char **argv
)
312 json_writer_t
*wr
= jsonw_new(stdout
);
314 jsonw_start_object(wr
);
315 jsonw_pretty(wr
, true);
316 jsonw_name(wr
, "Vyatta");
317 jsonw_start_object(wr
);
318 jsonw_string_field(wr
, "url", "http://vyatta.com");
319 jsonw_uint_field(wr
, "downloads", 2000000ul);
320 jsonw_float_field(wr
, "stock", 8.16);
322 jsonw_name(wr
, "ARGV");
323 jsonw_start_array(wr
);
325 jsonw_string(wr
, *++argv
);
328 jsonw_name(wr
, "empty");
329 jsonw_start_array(wr
);
332 jsonw_name(wr
, "NIL");
333 jsonw_start_object(wr
);
334 jsonw_end_object(wr
);
336 jsonw_null_field(wr
, "my_null");
338 jsonw_name(wr
, "special chars");
339 jsonw_start_array(wr
);
340 jsonw_string_field(wr
, "slash", "/");
341 jsonw_string_field(wr
, "newline", "\n");
342 jsonw_string_field(wr
, "tab", "\t");
343 jsonw_string_field(wr
, "ff", "\f");
344 jsonw_string_field(wr
, "quote", "\"");
345 jsonw_string_field(wr
, "tick", "\'");
346 jsonw_string_field(wr
, "backslash", "\\");
349 jsonw_end_object(wr
);
351 jsonw_end_object(wr
);