Linux Audio

Check our new training course

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