Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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