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