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