1*de1e4e89SAndroid Build Coastguard Worker /* 2*de1e4e89SAndroid Build Coastguard Worker * Simple streaming JSON writer 3*de1e4e89SAndroid Build Coastguard Worker * 4*de1e4e89SAndroid Build Coastguard Worker * This takes care of the annoying bits of JSON syntax like the commas 5*de1e4e89SAndroid Build Coastguard Worker * after elements 6*de1e4e89SAndroid Build Coastguard Worker * 7*de1e4e89SAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or 8*de1e4e89SAndroid Build Coastguard Worker * modify it under the terms of the GNU General Public License 9*de1e4e89SAndroid Build Coastguard Worker * as published by the Free Software Foundation; either version 10*de1e4e89SAndroid Build Coastguard Worker * 2 of the License, or (at your option) any later version. 11*de1e4e89SAndroid Build Coastguard Worker * 12*de1e4e89SAndroid Build Coastguard Worker * Authors: Stephen Hemminger <[email protected]> 13*de1e4e89SAndroid Build Coastguard Worker */ 14*de1e4e89SAndroid Build Coastguard Worker 15*de1e4e89SAndroid Build Coastguard Worker #ifndef _JSON_WRITER_H_ 16*de1e4e89SAndroid Build Coastguard Worker #define _JSON_WRITER_H_ 17*de1e4e89SAndroid Build Coastguard Worker 18*de1e4e89SAndroid Build Coastguard Worker #include <stdbool.h> 19*de1e4e89SAndroid Build Coastguard Worker #include <stdint.h> 20*de1e4e89SAndroid Build Coastguard Worker 21*de1e4e89SAndroid Build Coastguard Worker /* Opaque class structure */ 22*de1e4e89SAndroid Build Coastguard Worker typedef struct json_writer json_writer_t; 23*de1e4e89SAndroid Build Coastguard Worker 24*de1e4e89SAndroid Build Coastguard Worker /* Create a new JSON stream */ 25*de1e4e89SAndroid Build Coastguard Worker json_writer_t *jsonw_new(FILE *f); 26*de1e4e89SAndroid Build Coastguard Worker /* End output to JSON stream */ 27*de1e4e89SAndroid Build Coastguard Worker void jsonw_destroy(json_writer_t **self_p); 28*de1e4e89SAndroid Build Coastguard Worker 29*de1e4e89SAndroid Build Coastguard Worker /* Cause output to have pretty whitespace */ 30*de1e4e89SAndroid Build Coastguard Worker void jsonw_pretty(json_writer_t *self, bool on); 31*de1e4e89SAndroid Build Coastguard Worker 32*de1e4e89SAndroid Build Coastguard Worker /* Add property name */ 33*de1e4e89SAndroid Build Coastguard Worker void jsonw_name(json_writer_t *self, const char *name); 34*de1e4e89SAndroid Build Coastguard Worker 35*de1e4e89SAndroid Build Coastguard Worker /* Add value */ 36*de1e4e89SAndroid Build Coastguard Worker void jsonw_printf(json_writer_t *self, const char *fmt, ...); 37*de1e4e89SAndroid Build Coastguard Worker void jsonw_string(json_writer_t *self, const char *value); 38*de1e4e89SAndroid Build Coastguard Worker void jsonw_bool(json_writer_t *self, bool value); 39*de1e4e89SAndroid Build Coastguard Worker void jsonw_float(json_writer_t *self, double number); 40*de1e4e89SAndroid Build Coastguard Worker void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num); 41*de1e4e89SAndroid Build Coastguard Worker void jsonw_uint(json_writer_t *self, uint64_t number); 42*de1e4e89SAndroid Build Coastguard Worker void jsonw_hu(json_writer_t *self, unsigned short number); 43*de1e4e89SAndroid Build Coastguard Worker void jsonw_int(json_writer_t *self, int64_t number); 44*de1e4e89SAndroid Build Coastguard Worker void jsonw_null(json_writer_t *self); 45*de1e4e89SAndroid Build Coastguard Worker void jsonw_lluint(json_writer_t *self, unsigned long long int num); 46*de1e4e89SAndroid Build Coastguard Worker 47*de1e4e89SAndroid Build Coastguard Worker /* Useful Combinations of name and value */ 48*de1e4e89SAndroid Build Coastguard Worker void jsonw_string_field(json_writer_t *self, const char *prop, const char *val); 49*de1e4e89SAndroid Build Coastguard Worker void jsonw_bool_field(json_writer_t *self, const char *prop, bool value); 50*de1e4e89SAndroid Build Coastguard Worker void jsonw_float_field(json_writer_t *self, const char *prop, double num); 51*de1e4e89SAndroid Build Coastguard Worker void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num); 52*de1e4e89SAndroid Build Coastguard Worker void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num); 53*de1e4e89SAndroid Build Coastguard Worker void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num); 54*de1e4e89SAndroid Build Coastguard Worker void jsonw_null_field(json_writer_t *self, const char *prop); 55*de1e4e89SAndroid Build Coastguard Worker void jsonw_lluint_field(json_writer_t *self, const char *prop, 56*de1e4e89SAndroid Build Coastguard Worker unsigned long long int num); 57*de1e4e89SAndroid Build Coastguard Worker void jsonw_float_field_fmt(json_writer_t *self, const char *prop, 58*de1e4e89SAndroid Build Coastguard Worker const char *fmt, double val); 59*de1e4e89SAndroid Build Coastguard Worker 60*de1e4e89SAndroid Build Coastguard Worker /* Collections */ 61*de1e4e89SAndroid Build Coastguard Worker void jsonw_start_object(json_writer_t *self); 62*de1e4e89SAndroid Build Coastguard Worker void jsonw_end_object(json_writer_t *self); 63*de1e4e89SAndroid Build Coastguard Worker 64*de1e4e89SAndroid Build Coastguard Worker void jsonw_start_array(json_writer_t *self); 65*de1e4e89SAndroid Build Coastguard Worker void jsonw_end_array(json_writer_t *self); 66*de1e4e89SAndroid Build Coastguard Worker 67*de1e4e89SAndroid Build Coastguard Worker /* Override default exception handling */ 68*de1e4e89SAndroid Build Coastguard Worker typedef void (jsonw_err_handler_fn)(const char *); 69*de1e4e89SAndroid Build Coastguard Worker 70*de1e4e89SAndroid Build Coastguard Worker #endif /* _JSON_WRITER_H_ */ 71