]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/json_writer.c
1 /* SPDX-License-Identifier: (GPL-2.0 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("\\n", self
->out
);
81 fputs("\\\"", self
->out
);
84 fputs("\\\'", self
->out
);
87 putc(*str
, self
->out
);
92 /* Create a new JSON stream */
93 json_writer_t
*jsonw_new(FILE *f
)
95 json_writer_t
*self
= malloc(sizeof(*self
));
105 /* End output to JSON stream */
106 void jsonw_destroy(json_writer_t
**self_p
)
108 json_writer_t
*self
= *self_p
;
110 assert(self
->depth
== 0);
111 fputs("\n", self
->out
);
117 void jsonw_pretty(json_writer_t
*self
, bool on
)
123 static void jsonw_begin(json_writer_t
*self
, int c
)
131 static void jsonw_end(json_writer_t
*self
, int c
)
133 assert(self
->depth
> 0);
136 if (self
->sep
!= '\0')
143 /* Add a JSON property name */
144 void jsonw_name(json_writer_t
*self
, const char *name
)
149 jsonw_puts(self
, name
);
150 putc(':', self
->out
);
152 putc(' ', self
->out
);
155 __attribute__((format(printf
, 2, 3)))
156 void jsonw_printf(json_writer_t
*self
, const char *fmt
, ...)
162 vfprintf(self
->out
, fmt
, ap
);
167 void jsonw_start_object(json_writer_t
*self
)
169 jsonw_begin(self
, '{');
172 void jsonw_end_object(json_writer_t
*self
)
174 jsonw_end(self
, '}');
177 void jsonw_start_array(json_writer_t
*self
)
179 jsonw_begin(self
, '[');
181 putc(' ', self
->out
);
184 void jsonw_end_array(json_writer_t
*self
)
186 if (self
->pretty
&& self
->sep
)
187 putc(' ', self
->out
);
189 jsonw_end(self
, ']');
192 /* JSON value types */
193 void jsonw_string(json_writer_t
*self
, const char *value
)
196 jsonw_puts(self
, value
);
199 void jsonw_bool(json_writer_t
*self
, bool val
)
201 jsonw_printf(self
, "%s", val
? "true" : "false");
204 void jsonw_null(json_writer_t
*self
)
206 jsonw_printf(self
, "null");
209 void jsonw_float(json_writer_t
*self
, double num
)
211 jsonw_printf(self
, "%g", num
);
214 void jsonw_hhu(json_writer_t
*self
, unsigned char num
)
216 jsonw_printf(self
, "%hhu", num
);
219 void jsonw_hu(json_writer_t
*self
, unsigned short num
)
221 jsonw_printf(self
, "%hu", num
);
224 void jsonw_uint(json_writer_t
*self
, unsigned int num
)
226 jsonw_printf(self
, "%u", num
);
229 void jsonw_u64(json_writer_t
*self
, uint64_t num
)
231 jsonw_printf(self
, "%"PRIu64
, num
);
234 void jsonw_xint(json_writer_t
*self
, uint64_t num
)
236 jsonw_printf(self
, "%"PRIx64
, num
);
239 void jsonw_luint(json_writer_t
*self
, unsigned long num
)
241 jsonw_printf(self
, "%lu", num
);
244 void jsonw_lluint(json_writer_t
*self
, unsigned long long num
)
246 jsonw_printf(self
, "%llu", num
);
249 void jsonw_int(json_writer_t
*self
, int num
)
251 jsonw_printf(self
, "%d", num
);
254 void jsonw_s64(json_writer_t
*self
, int64_t num
)
256 jsonw_printf(self
, "%"PRId64
, num
);
259 /* Basic name/value objects */
260 void jsonw_string_field(json_writer_t
*self
, const char *prop
, const char *val
)
262 jsonw_name(self
, prop
);
263 jsonw_string(self
, val
);
266 void jsonw_bool_field(json_writer_t
*self
, const char *prop
, bool val
)
268 jsonw_name(self
, prop
);
269 jsonw_bool(self
, val
);
272 void jsonw_float_field(json_writer_t
*self
, const char *prop
, double val
)
274 jsonw_name(self
, prop
);
275 jsonw_float(self
, val
);
278 void jsonw_uint_field(json_writer_t
*self
, const char *prop
, unsigned int num
)
280 jsonw_name(self
, prop
);
281 jsonw_uint(self
, num
);
284 void jsonw_u64_field(json_writer_t
*self
, const char *prop
, uint64_t num
)
286 jsonw_name(self
, prop
);
287 jsonw_u64(self
, num
);
290 void jsonw_xint_field(json_writer_t
*self
, const char *prop
, uint64_t num
)
292 jsonw_name(self
, prop
);
293 jsonw_xint(self
, num
);
296 void jsonw_hhu_field(json_writer_t
*self
, const char *prop
, unsigned char num
)
298 jsonw_name(self
, prop
);
299 jsonw_hhu(self
, num
);
302 void jsonw_hu_field(json_writer_t
*self
, const char *prop
, unsigned short num
)
304 jsonw_name(self
, prop
);
308 void jsonw_luint_field(json_writer_t
*self
,
312 jsonw_name(self
, prop
);
313 jsonw_luint(self
, num
);
316 void jsonw_lluint_field(json_writer_t
*self
,
318 unsigned long long num
)
320 jsonw_name(self
, prop
);
321 jsonw_lluint(self
, num
);
324 void jsonw_int_field(json_writer_t
*self
, const char *prop
, int num
)
326 jsonw_name(self
, prop
);
327 jsonw_int(self
, num
);
330 void jsonw_s64_field(json_writer_t
*self
, const char *prop
, int64_t num
)
332 jsonw_name(self
, prop
);
333 jsonw_s64(self
, num
);
336 void jsonw_null_field(json_writer_t
*self
, const char *prop
)
338 jsonw_name(self
, prop
);
343 int main(int argc
, char **argv
)
345 json_writer_t
*wr
= jsonw_new(stdout
);
347 jsonw_start_object(wr
);
348 jsonw_pretty(wr
, true);
349 jsonw_name(wr
, "Vyatta");
350 jsonw_start_object(wr
);
351 jsonw_string_field(wr
, "url", "http://vyatta.com");
352 jsonw_uint_field(wr
, "downloads", 2000000ul);
353 jsonw_float_field(wr
, "stock", 8.16);
355 jsonw_name(wr
, "ARGV");
356 jsonw_start_array(wr
);
358 jsonw_string(wr
, *++argv
);
361 jsonw_name(wr
, "empty");
362 jsonw_start_array(wr
);
365 jsonw_name(wr
, "NIL");
366 jsonw_start_object(wr
);
367 jsonw_end_object(wr
);
369 jsonw_null_field(wr
, "my_null");
371 jsonw_name(wr
, "special chars");
372 jsonw_start_array(wr
);
373 jsonw_string_field(wr
, "slash", "/");
374 jsonw_string_field(wr
, "newline", "\n");
375 jsonw_string_field(wr
, "tab", "\t");
376 jsonw_string_field(wr
, "ff", "\f");
377 jsonw_string_field(wr
, "quote", "\"");
378 jsonw_string_field(wr
, "tick", "\'");
379 jsonw_string_field(wr
, "backslash", "\\");
382 jsonw_end_object(wr
);
384 jsonw_end_object(wr
);