]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/json_writer.c
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 static void jsonw_printf(json_writer_t
*self
, const char *fmt
, ...)
165 vfprintf(self
->out
, fmt
, ap
);
170 void jsonw_start_object(json_writer_t
*self
)
172 jsonw_begin(self
, '{');
175 void jsonw_end_object(json_writer_t
*self
)
177 jsonw_end(self
, '}');
180 void jsonw_start_array(json_writer_t
*self
)
182 jsonw_begin(self
, '[');
185 void jsonw_end_array(json_writer_t
*self
)
187 jsonw_end(self
, ']');
190 /* JSON value types */
191 void jsonw_string(json_writer_t
*self
, const char *value
)
194 jsonw_puts(self
, value
);
197 void jsonw_bool(json_writer_t
*self
, bool val
)
199 jsonw_printf(self
, "%s", val
? "true" : "false");
203 void jsonw_null(json_writer_t
*self
)
205 jsonw_printf(self
, "null");
208 void jsonw_float(json_writer_t
*self
, double num
)
210 jsonw_printf(self
, "%g", num
);
214 void jsonw_uint(json_writer_t
*self
, uint64_t num
)
216 jsonw_printf(self
, "%"PRIu64
, num
);
219 void jsonw_int(json_writer_t
*self
, int64_t num
)
221 jsonw_printf(self
, "%"PRId64
, num
);
224 /* Basic name/value objects */
225 void jsonw_string_field(json_writer_t
*self
, const char *prop
, const char *val
)
227 jsonw_name(self
, prop
);
228 jsonw_string(self
, val
);
231 void jsonw_bool_field(json_writer_t
*self
, const char *prop
, bool val
)
233 jsonw_name(self
, prop
);
234 jsonw_bool(self
, val
);
238 void jsonw_float_field(json_writer_t
*self
, const char *prop
, double val
)
240 jsonw_name(self
, prop
);
241 jsonw_float(self
, val
);
245 void jsonw_uint_field(json_writer_t
*self
, const char *prop
, uint64_t num
)
247 jsonw_name(self
, prop
);
248 jsonw_uint(self
, num
);
251 void jsonw_int_field(json_writer_t
*self
, const char *prop
, int64_t num
)
253 jsonw_name(self
, prop
);
254 jsonw_int(self
, num
);
258 void jsonw_null_field(json_writer_t
*self
, const char *prop
)
260 jsonw_name(self
, prop
);
266 int main(int argc
, char **argv
)
268 json_writer_t
*wr
= jsonw_new(stdout
);
270 jsonw_start_object(wr
);
271 jsonw_pretty(wr
, true);
272 jsonw_name(wr
, "Vyatta");
273 jsonw_start_object(wr
);
274 jsonw_string_field(wr
, "url", "http://vyatta.com");
275 jsonw_uint_field(wr
, "downloads", 2000000ul);
276 jsonw_float_field(wr
, "stock", 8.16);
278 jsonw_name(wr
, "ARGV");
279 jsonw_start_array(wr
);
281 jsonw_string(wr
, *++argv
);
284 jsonw_name(wr
, "empty");
285 jsonw_start_array(wr
);
288 jsonw_name(wr
, "NIL");
289 jsonw_start_object(wr
);
290 jsonw_end_object(wr
);
292 jsonw_null_field(wr
, "my_null");
294 jsonw_name(wr
, "special chars");
295 jsonw_start_array(wr
);
296 jsonw_string_field(wr
, "slash", "/");
297 jsonw_string_field(wr
, "newline", "\n");
298 jsonw_string_field(wr
, "tab", "\t");
299 jsonw_string_field(wr
, "ff", "\f");
300 jsonw_string_field(wr
, "quote", "\"");
301 jsonw_string_field(wr
, "tick", "\'");
302 jsonw_string_field(wr
, "backslash", "\\");
305 jsonw_end_object(wr
);
307 jsonw_end_object(wr
);