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