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