Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
0002 /*
0003  * Simple streaming JSON writer
0004  *
0005  * This takes care of the annoying bits of JSON syntax like the commas
0006  * after elements
0007  *
0008  * Authors: Stephen Hemminger <stephen@networkplumber.org>
0009  */
0010 
0011 #ifndef _JSON_WRITER_H_
0012 #define _JSON_WRITER_H_
0013 
0014 #include <stdbool.h>
0015 #include <stdint.h>
0016 #include <stdarg.h>
0017 #include <linux/compiler.h>
0018 
0019 /* Opaque class structure */
0020 typedef struct json_writer json_writer_t;
0021 
0022 /* Create a new JSON stream */
0023 json_writer_t *jsonw_new(FILE *f);
0024 /* End output to JSON stream */
0025 void jsonw_destroy(json_writer_t **self_p);
0026 
0027 /* Cause output to have pretty whitespace */
0028 void jsonw_pretty(json_writer_t *self, bool on);
0029 
0030 /* Reset separator to create new JSON */
0031 void jsonw_reset(json_writer_t *self);
0032 
0033 /* Add property name */
0034 void jsonw_name(json_writer_t *self, const char *name);
0035 
0036 /* Add value  */
0037 void __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt,
0038                       va_list ap);
0039 void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...);
0040 void jsonw_string(json_writer_t *self, const char *value);
0041 void jsonw_bool(json_writer_t *self, bool value);
0042 void jsonw_float(json_writer_t *self, double number);
0043 void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
0044 void jsonw_uint(json_writer_t *self, uint64_t number);
0045 void jsonw_hu(json_writer_t *self, unsigned short number);
0046 void jsonw_int(json_writer_t *self, int64_t number);
0047 void jsonw_null(json_writer_t *self);
0048 void jsonw_lluint(json_writer_t *self, unsigned long long int num);
0049 
0050 /* Useful Combinations of name and value */
0051 void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
0052 void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
0053 void jsonw_float_field(json_writer_t *self, const char *prop, double num);
0054 void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
0055 void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
0056 void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
0057 void jsonw_null_field(json_writer_t *self, const char *prop);
0058 void jsonw_lluint_field(json_writer_t *self, const char *prop,
0059             unsigned long long int num);
0060 void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
0061                const char *fmt, double val);
0062 
0063 /* Collections */
0064 void jsonw_start_object(json_writer_t *self);
0065 void jsonw_end_object(json_writer_t *self);
0066 
0067 void jsonw_start_array(json_writer_t *self);
0068 void jsonw_end_array(json_writer_t *self);
0069 
0070 /* Override default exception handling */
0071 typedef void (jsonw_err_handler_fn)(const char *);
0072 
0073 #endif /* _JSON_WRITER_H_ */