xref: /openbmc/linux/tools/bpf/bpftool/json_writer.c (revision 22fc4c4c9fd60427bcda00878cee94e7622cfa7a)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /*
3  * Simple streaming JSON writer
4  *
5  * This takes care of the annoying bits of JSON syntax like the commas
6  * after elements
7  *
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.
12  *
13  * Authors:	Stephen Hemminger <stephen@networkplumber.org>
14  */
15 
16 #include <stdio.h>
17 #include <stdbool.h>
18 #include <stdarg.h>
19 #include <assert.h>
20 #include <malloc.h>
21 #include <inttypes.h>
22 #include <stdint.h>
23 #include <linux/compiler.h>
24 
25 #include "json_writer.h"
26 
27 struct json_writer {
28 	FILE		*out;	/* output file */
29 	unsigned	depth;  /* nesting */
30 	bool		pretty; /* optional whitepace */
31 	char		sep;	/* either nul or comma */
32 };
33 
34 /* indentation for pretty print */
35 static void jsonw_indent(json_writer_t *self)
36 {
37 	unsigned i;
38 	for (i = 0; i < self->depth; ++i)
39 		fputs("    ", self->out);
40 }
41 
42 /* end current line and indent if pretty printing */
43 static void jsonw_eol(json_writer_t *self)
44 {
45 	if (!self->pretty)
46 		return;
47 
48 	putc('\n', self->out);
49 	jsonw_indent(self);
50 }
51 
52 /* If current object is not empty print a comma */
53 static void jsonw_eor(json_writer_t *self)
54 {
55 	if (self->sep != '\0')
56 		putc(self->sep, self->out);
57 	self->sep = ',';
58 }
59 
60 
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)
64 {
65 	putc('"', self->out);
66 	for (; *str; ++str)
67 		switch (*str) {
68 		case '\t':
69 			fputs("\\t", self->out);
70 			break;
71 		case '\n':
72 			fputs("\\n", self->out);
73 			break;
74 		case '\r':
75 			fputs("\\r", self->out);
76 			break;
77 		case '\f':
78 			fputs("\\f", self->out);
79 			break;
80 		case '\b':
81 			fputs("\\b", self->out);
82 			break;
83 		case '\\':
84 			fputs("\\n", self->out);
85 			break;
86 		case '"':
87 			fputs("\\\"", self->out);
88 			break;
89 		case '\'':
90 			fputs("\\\'", self->out);
91 			break;
92 		default:
93 			putc(*str, self->out);
94 		}
95 	putc('"', self->out);
96 }
97 
98 /* Create a new JSON stream */
99 json_writer_t *jsonw_new(FILE *f)
100 {
101 	json_writer_t *self = malloc(sizeof(*self));
102 	if (self) {
103 		self->out = f;
104 		self->depth = 0;
105 		self->pretty = false;
106 		self->sep = '\0';
107 	}
108 	return self;
109 }
110 
111 /* End output to JSON stream */
112 void jsonw_destroy(json_writer_t **self_p)
113 {
114 	json_writer_t *self = *self_p;
115 
116 	assert(self->depth == 0);
117 	fputs("\n", self->out);
118 	fflush(self->out);
119 	free(self);
120 	*self_p = NULL;
121 }
122 
123 void jsonw_pretty(json_writer_t *self, bool on)
124 {
125 	self->pretty = on;
126 }
127 
128 /* Basic blocks */
129 static void jsonw_begin(json_writer_t *self, int c)
130 {
131 	jsonw_eor(self);
132 	putc(c, self->out);
133 	++self->depth;
134 	self->sep = '\0';
135 }
136 
137 static void jsonw_end(json_writer_t *self, int c)
138 {
139 	assert(self->depth > 0);
140 
141 	--self->depth;
142 	if (self->sep != '\0')
143 		jsonw_eol(self);
144 	putc(c, self->out);
145 	self->sep = ',';
146 }
147 
148 
149 /* Add a JSON property name */
150 void jsonw_name(json_writer_t *self, const char *name)
151 {
152 	jsonw_eor(self);
153 	jsonw_eol(self);
154 	self->sep = '\0';
155 	jsonw_puts(self, name);
156 	putc(':', self->out);
157 	if (self->pretty)
158 		putc(' ', self->out);
159 }
160 
161 void __printf(2, 0)
162 jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
163 {
164 	jsonw_eor(self);
165 	putc('"', self->out);
166 	vfprintf(self->out, fmt, ap);
167 	putc('"', self->out);
168 }
169 
170 void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...)
171 {
172 	va_list ap;
173 
174 	va_start(ap, fmt);
175 	jsonw_eor(self);
176 	vfprintf(self->out, fmt, ap);
177 	va_end(ap);
178 }
179 
180 /* Collections */
181 void jsonw_start_object(json_writer_t *self)
182 {
183 	jsonw_begin(self, '{');
184 }
185 
186 void jsonw_end_object(json_writer_t *self)
187 {
188 	jsonw_end(self, '}');
189 }
190 
191 void jsonw_start_array(json_writer_t *self)
192 {
193 	jsonw_begin(self, '[');
194 }
195 
196 void jsonw_end_array(json_writer_t *self)
197 {
198 	jsonw_end(self, ']');
199 }
200 
201 /* JSON value types */
202 void jsonw_string(json_writer_t *self, const char *value)
203 {
204 	jsonw_eor(self);
205 	jsonw_puts(self, value);
206 }
207 
208 void jsonw_bool(json_writer_t *self, bool val)
209 {
210 	jsonw_printf(self, "%s", val ? "true" : "false");
211 }
212 
213 void jsonw_null(json_writer_t *self)
214 {
215 	jsonw_printf(self, "null");
216 }
217 
218 void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num)
219 {
220 	jsonw_printf(self, fmt, num);
221 }
222 
223 #ifdef notused
224 void jsonw_float(json_writer_t *self, double num)
225 {
226 	jsonw_printf(self, "%g", num);
227 }
228 #endif
229 
230 void jsonw_hu(json_writer_t *self, unsigned short num)
231 {
232 	jsonw_printf(self, "%hu", num);
233 }
234 
235 void jsonw_uint(json_writer_t *self, uint64_t num)
236 {
237 	jsonw_printf(self, "%"PRIu64, num);
238 }
239 
240 void jsonw_lluint(json_writer_t *self, unsigned long long int num)
241 {
242 	jsonw_printf(self, "%llu", num);
243 }
244 
245 void jsonw_int(json_writer_t *self, int64_t num)
246 {
247 	jsonw_printf(self, "%"PRId64, num);
248 }
249 
250 /* Basic name/value objects */
251 void jsonw_string_field(json_writer_t *self, const char *prop, const char *val)
252 {
253 	jsonw_name(self, prop);
254 	jsonw_string(self, val);
255 }
256 
257 void jsonw_bool_field(json_writer_t *self, const char *prop, bool val)
258 {
259 	jsonw_name(self, prop);
260 	jsonw_bool(self, val);
261 }
262 
263 #ifdef notused
264 void jsonw_float_field(json_writer_t *self, const char *prop, double val)
265 {
266 	jsonw_name(self, prop);
267 	jsonw_float(self, val);
268 }
269 #endif
270 
271 void jsonw_float_field_fmt(json_writer_t *self,
272 			   const char *prop,
273 			   const char *fmt,
274 			   double val)
275 {
276 	jsonw_name(self, prop);
277 	jsonw_float_fmt(self, fmt, val);
278 }
279 
280 void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num)
281 {
282 	jsonw_name(self, prop);
283 	jsonw_uint(self, num);
284 }
285 
286 void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num)
287 {
288 	jsonw_name(self, prop);
289 	jsonw_hu(self, num);
290 }
291 
292 void jsonw_lluint_field(json_writer_t *self,
293 			const char *prop,
294 			unsigned long long int num)
295 {
296 	jsonw_name(self, prop);
297 	jsonw_lluint(self, num);
298 }
299 
300 void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num)
301 {
302 	jsonw_name(self, prop);
303 	jsonw_int(self, num);
304 }
305 
306 void jsonw_null_field(json_writer_t *self, const char *prop)
307 {
308 	jsonw_name(self, prop);
309 	jsonw_null(self);
310 }
311 
312 #ifdef TEST
313 int main(int argc, char **argv)
314 {
315 	json_writer_t *wr = jsonw_new(stdout);
316 
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);
324 
325 	jsonw_name(wr, "ARGV");
326 	jsonw_start_array(wr);
327 	while (--argc)
328 		jsonw_string(wr, *++argv);
329 	jsonw_end_array(wr);
330 
331 	jsonw_name(wr, "empty");
332 	jsonw_start_array(wr);
333 	jsonw_end_array(wr);
334 
335 	jsonw_name(wr, "NIL");
336 	jsonw_start_object(wr);
337 	jsonw_end_object(wr);
338 
339 	jsonw_null_field(wr, "my_null");
340 
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", "\\");
350 	jsonw_end_array(wr);
351 
352 	jsonw_end_object(wr);
353 
354 	jsonw_end_object(wr);
355 	jsonw_destroy(&wr);
356 	return 0;
357 }
358 
359 #endif
360