From b4b11394d071810d694b66962e8d48cb866af473 Mon Sep 17 00:00:00 2001 From: Andrea Claudi Date: Mon, 25 Mar 2019 13:30:53 +0100 Subject: [PATCH] json_writer: add new json handlers (null, float with format, lluint, hu) Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1642479 Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641914 Upstream Status: iproute2.git commit 7252f16b2d191 commit 7252f16b2d1919b31f0a2ec094dd3516b1e33a55 Author: Julien Fortin Date: Thu Aug 17 10:35:50 2017 -0700 json_writer: add new json handlers (null, float with format, lluint, hu) Signed-off-by: Julien Fortin --- include/json_writer.h | 9 +++++++++ lib/json_writer.c | 44 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/include/json_writer.h b/include/json_writer.h index ab9a008a67994..1516aafba59df 100644 --- a/include/json_writer.h +++ b/include/json_writer.h @@ -33,20 +33,29 @@ void jsonw_pretty(json_writer_t *self, bool on); void jsonw_name(json_writer_t *self, const char *name); /* Add value */ +void jsonw_printf(json_writer_t *self, const char *fmt, ...); void jsonw_string(json_writer_t *self, const char *value); void jsonw_bool(json_writer_t *self, bool value); void jsonw_float(json_writer_t *self, double number); +void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num); void jsonw_uint(json_writer_t *self, uint64_t number); +void jsonw_hu(json_writer_t *self, unsigned short number); void jsonw_int(json_writer_t *self, int64_t number); void jsonw_null(json_writer_t *self); +void jsonw_lluint(json_writer_t *self, unsigned long long int num); /* Useful Combinations of name and value */ void jsonw_string_field(json_writer_t *self, const char *prop, const char *val); void jsonw_bool_field(json_writer_t *self, const char *prop, bool value); void jsonw_float_field(json_writer_t *self, const char *prop, double num); void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num); +void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num); void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num); void jsonw_null_field(json_writer_t *self, const char *prop); +void jsonw_lluint_field(json_writer_t *self, const char *prop, + unsigned long long int num); +void jsonw_float_field_fmt(json_writer_t *self, const char *prop, + const char *fmt, double val); /* Collections */ void jsonw_start_object(json_writer_t *self); diff --git a/lib/json_writer.c b/lib/json_writer.c index 9fc05e96b605f..6b77d288cce2b 100644 --- a/lib/json_writer.c +++ b/lib/json_writer.c @@ -156,7 +156,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; @@ -199,23 +199,38 @@ 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); +} + +#ifdef notused void jsonw_float(json_writer_t *self, double num) { jsonw_printf(self, "%g", num); } #endif +void jsonw_hu(json_writer_t *self, unsigned short num) +{ + jsonw_printf(self, "%hu", num); +} + void jsonw_uint(json_writer_t *self, uint64_t num) { jsonw_printf(self, "%"PRIu64, 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, int64_t num) { jsonw_printf(self, "%"PRId64, num); @@ -242,25 +257,46 @@ void jsonw_float_field(json_writer_t *self, const char *prop, double val) } #endif +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, uint64_t num) { jsonw_name(self, prop); jsonw_uint(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_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, int64_t num) { jsonw_name(self, prop); jsonw_int(self, num); } -#ifdef notused 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) -- 2.21.0