X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=lib%2Fjson_writer.c;h=aa9ce1c65e513567f1160248772b7ea4a9caa18b;hb=04cb3c0d4386c6c50d8cdfb90043e2ac022404ac;hp=2af16e101c203999e48299fddb4b1353d71017ab;hpb=fcc16c2287bf8449b1b9e1359d375fb6cbe49842;p=mirror_iproute2.git diff --git a/lib/json_writer.c b/lib/json_writer.c index 2af16e10..aa9ce1c6 100644 --- a/lib/json_writer.c +++ b/lib/json_writer.c @@ -1,14 +1,10 @@ +/* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */ /* * Simple streaming JSON writer * * This takes care of the annoying bits of JSON syntax like the commas * after elements * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version - * 2 of the License, or (at your option) any later version. - * * Authors: Stephen Hemminger */ @@ -33,7 +29,7 @@ struct json_writer { static void jsonw_indent(json_writer_t *self) { unsigned i; - for (i = 0; i <= self->depth; ++i) + for (i = 0; i < self->depth; ++i) fputs(" ", self->out); } @@ -102,7 +98,6 @@ json_writer_t *jsonw_new(FILE *f) self->depth = 0; self->pretty = false; self->sep = '\0'; - putc('{', self->out); } return self; } @@ -113,8 +108,7 @@ void jsonw_destroy(json_writer_t **self_p) json_writer_t *self = *self_p; assert(self->depth == 0); - jsonw_eol(self); - fputs("}\n", self->out); + fputs("\n", self->out); fflush(self->out); free(self); *self_p = NULL; @@ -158,7 +152,7 @@ void jsonw_name(json_writer_t *self, const char *name) putc(' ', self->out); } -static void jsonw_printf(json_writer_t *self, const char *fmt, ...) +void jsonw_printf(json_writer_t *self, const char *fmt, ...) { va_list ap; @@ -182,10 +176,15 @@ void jsonw_end_object(json_writer_t *self) void jsonw_start_array(json_writer_t *self) { jsonw_begin(self, '['); + if (self->pretty) + putc(' ', self->out); } void jsonw_end_array(json_writer_t *self) { + if (self->pretty && self->sep) + putc(' ', self->out); + self->sep = '\0'; jsonw_end(self, ']'); } @@ -201,24 +200,57 @@ void jsonw_bool(json_writer_t *self, bool val) jsonw_printf(self, "%s", val ? "true" : "false"); } -#ifdef notused void jsonw_null(json_writer_t *self) { jsonw_printf(self, "null"); } +void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num) +{ + jsonw_printf(self, fmt, num); +} + void jsonw_float(json_writer_t *self, double num) { jsonw_printf(self, "%g", num); } -#endif -void jsonw_uint(json_writer_t *self, uint64_t num) +void jsonw_hu(json_writer_t *self, unsigned short num) +{ + jsonw_printf(self, "%hu", num); +} + +void jsonw_uint(json_writer_t *self, unsigned int num) +{ + jsonw_printf(self, "%u", num); +} + +void jsonw_u64(json_writer_t *self, uint64_t num) { jsonw_printf(self, "%"PRIu64, num); } -void jsonw_int(json_writer_t *self, int64_t num) +void jsonw_xint(json_writer_t *self, uint64_t num) +{ + jsonw_printf(self, "%"PRIx64, num); +} + +void jsonw_luint(json_writer_t *self, unsigned long int num) +{ + jsonw_printf(self, "%lu", num); +} + +void jsonw_lluint(json_writer_t *self, unsigned long long int num) +{ + jsonw_printf(self, "%llu", num); +} + +void jsonw_int(json_writer_t *self, int num) +{ + jsonw_printf(self, "%d", num); +} + +void jsonw_s64(json_writer_t *self, int64_t num) { jsonw_printf(self, "%"PRId64, num); } @@ -236,39 +268,85 @@ void jsonw_bool_field(json_writer_t *self, const char *prop, bool val) jsonw_bool(self, val); } -#ifdef notused void jsonw_float_field(json_writer_t *self, const char *prop, double val) { jsonw_name(self, prop); jsonw_float(self, val); } -#endif -void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num) +void jsonw_float_field_fmt(json_writer_t *self, + const char *prop, + const char *fmt, + double val) +{ + jsonw_name(self, prop); + jsonw_float_fmt(self, fmt, val); +} + +void jsonw_uint_field(json_writer_t *self, const char *prop, unsigned int num) { jsonw_name(self, prop); jsonw_uint(self, num); } -void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num) +void jsonw_u64_field(json_writer_t *self, const char *prop, uint64_t num) +{ + jsonw_name(self, prop); + jsonw_u64(self, num); +} + +void jsonw_xint_field(json_writer_t *self, const char *prop, uint64_t num) +{ + jsonw_name(self, prop); + jsonw_xint(self, num); +} + +void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num) +{ + jsonw_name(self, prop); + jsonw_hu(self, num); +} + +void jsonw_luint_field(json_writer_t *self, + const char *prop, + unsigned long int num) +{ + jsonw_name(self, prop); + jsonw_luint(self, num); +} + +void jsonw_lluint_field(json_writer_t *self, + const char *prop, + unsigned long long int num) +{ + jsonw_name(self, prop); + jsonw_lluint(self, num); +} + +void jsonw_int_field(json_writer_t *self, const char *prop, int num) { jsonw_name(self, prop); jsonw_int(self, num); } -#ifdef notused +void jsonw_s64_field(json_writer_t *self, const char *prop, int64_t num) +{ + jsonw_name(self, prop); + jsonw_s64(self, num); +} + void jsonw_null_field(json_writer_t *self, const char *prop) { jsonw_name(self, prop); jsonw_null(self); } -#endif #ifdef TEST int main(int argc, char **argv) { json_writer_t *wr = jsonw_new(stdout); + jsonw_start_object(wr); jsonw_pretty(wr, true); jsonw_name(wr, "Vyatta"); jsonw_start_object(wr); @@ -305,6 +383,7 @@ int main(int argc, char **argv) jsonw_end_object(wr); + jsonw_end_object(wr); jsonw_destroy(&wr); return 0; }