^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: (GPL-2.0-only 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) #ifndef _JSON_WRITER_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define _JSON_WRITER_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* Opaque class structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) typedef struct json_writer json_writer_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* Create a new JSON stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) json_writer_t *jsonw_new(FILE *f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* End output to JSON stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void jsonw_destroy(json_writer_t **self_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* Cause output to have pretty whitespace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void jsonw_pretty(json_writer_t *self, bool on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Reset separator to create new JSON */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void jsonw_reset(json_writer_t *self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Add property name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void jsonw_name(json_writer_t *self, const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Add value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) void __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) va_list ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void jsonw_string(json_writer_t *self, const char *value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void jsonw_bool(json_writer_t *self, bool value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void jsonw_float(json_writer_t *self, double number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void jsonw_uint(json_writer_t *self, uint64_t number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void jsonw_hu(json_writer_t *self, unsigned short number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void jsonw_int(json_writer_t *self, int64_t number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void jsonw_null(json_writer_t *self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void jsonw_lluint(json_writer_t *self, unsigned long long int num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Useful Combinations of name and value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void jsonw_float_field(json_writer_t *self, const char *prop, double num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) void jsonw_null_field(json_writer_t *self, const char *prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void jsonw_lluint_field(json_writer_t *self, const char *prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned long long int num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) const char *fmt, double val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* Collections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) void jsonw_start_object(json_writer_t *self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) void jsonw_end_object(json_writer_t *self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) void jsonw_start_array(json_writer_t *self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void jsonw_end_array(json_writer_t *self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Override default exception handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) typedef void (jsonw_err_handler_fn)(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #endif /* _JSON_WRITER_H_ */