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