1907b2236SJakub Kicinski /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ 2b66e907cSQuentin Monnet /* 3b66e907cSQuentin Monnet * Simple streaming JSON writer 4b66e907cSQuentin Monnet * 5b66e907cSQuentin Monnet * This takes care of the annoying bits of JSON syntax like the commas 6b66e907cSQuentin Monnet * after elements 7b66e907cSQuentin Monnet * 8b66e907cSQuentin Monnet * Authors: Stephen Hemminger <stephen@networkplumber.org> 9b66e907cSQuentin Monnet */ 10b66e907cSQuentin Monnet 11b66e907cSQuentin Monnet #ifndef _JSON_WRITER_H_ 12b66e907cSQuentin Monnet #define _JSON_WRITER_H_ 13b66e907cSQuentin Monnet 14b66e907cSQuentin Monnet #include <stdbool.h> 15b66e907cSQuentin Monnet #include <stdint.h> 16f05e2c32SQuentin Monnet #include <stdarg.h> 17*d8d8b008SManu Bretelle #include <stdio.h> 188918dc42SQuentin Monnet #include <linux/compiler.h> 19b66e907cSQuentin Monnet 20b66e907cSQuentin Monnet /* Opaque class structure */ 21b66e907cSQuentin Monnet typedef struct json_writer json_writer_t; 22b66e907cSQuentin Monnet 23b66e907cSQuentin Monnet /* Create a new JSON stream */ 24b66e907cSQuentin Monnet json_writer_t *jsonw_new(FILE *f); 25b66e907cSQuentin Monnet /* End output to JSON stream */ 26b66e907cSQuentin Monnet void jsonw_destroy(json_writer_t **self_p); 27b66e907cSQuentin Monnet 28b66e907cSQuentin Monnet /* Cause output to have pretty whitespace */ 29b66e907cSQuentin Monnet void jsonw_pretty(json_writer_t *self, bool on); 30b66e907cSQuentin Monnet 31aff52e68SYiFei Zhu /* Reset separator to create new JSON */ 32aff52e68SYiFei Zhu void jsonw_reset(json_writer_t *self); 33aff52e68SYiFei Zhu 34b66e907cSQuentin Monnet /* Add property name */ 35b66e907cSQuentin Monnet void jsonw_name(json_writer_t *self, const char *name); 36b66e907cSQuentin Monnet 37b66e907cSQuentin Monnet /* Add value */ 388918dc42SQuentin Monnet void __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, 398918dc42SQuentin Monnet va_list ap); 408918dc42SQuentin Monnet void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...); 41b66e907cSQuentin Monnet void jsonw_string(json_writer_t *self, const char *value); 42b66e907cSQuentin Monnet void jsonw_bool(json_writer_t *self, bool value); 43b66e907cSQuentin Monnet void jsonw_float(json_writer_t *self, double number); 44b66e907cSQuentin Monnet void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num); 45b66e907cSQuentin Monnet void jsonw_uint(json_writer_t *self, uint64_t number); 46b66e907cSQuentin Monnet void jsonw_hu(json_writer_t *self, unsigned short number); 47b66e907cSQuentin Monnet void jsonw_int(json_writer_t *self, int64_t number); 48b66e907cSQuentin Monnet void jsonw_null(json_writer_t *self); 49b66e907cSQuentin Monnet void jsonw_lluint(json_writer_t *self, unsigned long long int num); 50b66e907cSQuentin Monnet 51b66e907cSQuentin Monnet /* Useful Combinations of name and value */ 52b66e907cSQuentin Monnet void jsonw_string_field(json_writer_t *self, const char *prop, const char *val); 53b66e907cSQuentin Monnet void jsonw_bool_field(json_writer_t *self, const char *prop, bool value); 54b66e907cSQuentin Monnet void jsonw_float_field(json_writer_t *self, const char *prop, double num); 55b66e907cSQuentin Monnet void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num); 56b66e907cSQuentin Monnet void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num); 57b66e907cSQuentin Monnet void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num); 58b66e907cSQuentin Monnet void jsonw_null_field(json_writer_t *self, const char *prop); 59b66e907cSQuentin Monnet void jsonw_lluint_field(json_writer_t *self, const char *prop, 60b66e907cSQuentin Monnet unsigned long long int num); 61b66e907cSQuentin Monnet void jsonw_float_field_fmt(json_writer_t *self, const char *prop, 62b66e907cSQuentin Monnet const char *fmt, double val); 63b66e907cSQuentin Monnet 64b66e907cSQuentin Monnet /* Collections */ 65b66e907cSQuentin Monnet void jsonw_start_object(json_writer_t *self); 66b66e907cSQuentin Monnet void jsonw_end_object(json_writer_t *self); 67b66e907cSQuentin Monnet 68b66e907cSQuentin Monnet void jsonw_start_array(json_writer_t *self); 69b66e907cSQuentin Monnet void jsonw_end_array(json_writer_t *self); 70b66e907cSQuentin Monnet 71b66e907cSQuentin Monnet /* Override default exception handling */ 72b66e907cSQuentin Monnet typedef void (jsonw_err_handler_fn)(const char *); 73b66e907cSQuentin Monnet 74b66e907cSQuentin Monnet #endif /* _JSON_WRITER_H_ */ 75