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