2018-03-07 06:39:19 +08:00
|
|
|
/* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
|
2015-08-22 05:14:51 +08:00
|
|
|
/*
|
|
|
|
* Simple streaming JSON writer
|
|
|
|
*
|
|
|
|
* This takes care of the annoying bits of JSON syntax like the commas
|
|
|
|
* after elements
|
|
|
|
*
|
|
|
|
* Authors: Stephen Hemminger <stephen@networkplumber.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _JSON_WRITER_H_
|
|
|
|
#define _JSON_WRITER_H_
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/* Opaque class structure */
|
|
|
|
typedef struct json_writer json_writer_t;
|
|
|
|
|
|
|
|
/* Create a new JSON stream */
|
|
|
|
json_writer_t *jsonw_new(FILE *f);
|
|
|
|
/* End output to JSON stream */
|
|
|
|
void jsonw_destroy(json_writer_t **self_p);
|
|
|
|
|
|
|
|
/* Cause output to have pretty whitespace */
|
|
|
|
void jsonw_pretty(json_writer_t *self, bool on);
|
|
|
|
|
|
|
|
/* Add property name */
|
|
|
|
void jsonw_name(json_writer_t *self, const char *name);
|
|
|
|
|
|
|
|
/* Add value */
|
2017-08-18 01:35:50 +08:00
|
|
|
void jsonw_printf(json_writer_t *self, const char *fmt, ...);
|
2015-08-22 05:14:51 +08:00
|
|
|
void jsonw_string(json_writer_t *self, const char *value);
|
|
|
|
void jsonw_bool(json_writer_t *self, bool value);
|
|
|
|
void jsonw_float(json_writer_t *self, double number);
|
2017-08-18 01:35:50 +08:00
|
|
|
void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
|
2018-04-25 23:28:57 +08:00
|
|
|
void jsonw_uint(json_writer_t *self, unsigned int number);
|
|
|
|
void jsonw_u64(json_writer_t *self, uint64_t number);
|
2018-03-30 00:10:41 +08:00
|
|
|
void jsonw_xint(json_writer_t *self, uint64_t number);
|
2017-08-18 01:35:50 +08:00
|
|
|
void jsonw_hu(json_writer_t *self, unsigned short number);
|
2018-04-25 23:28:57 +08:00
|
|
|
void jsonw_int(json_writer_t *self, int number);
|
|
|
|
void jsonw_s64(json_writer_t *self, int64_t number);
|
2015-08-22 05:14:51 +08:00
|
|
|
void jsonw_null(json_writer_t *self);
|
2018-04-25 23:28:57 +08:00
|
|
|
void jsonw_luint(json_writer_t *self, unsigned long int num);
|
2017-08-18 01:35:50 +08:00
|
|
|
void jsonw_lluint(json_writer_t *self, unsigned long long int num);
|
2015-08-22 05:14:51 +08:00
|
|
|
|
|
|
|
/* Useful Combinations of name and value */
|
|
|
|
void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
|
|
|
|
void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
|
|
|
|
void jsonw_float_field(json_writer_t *self, const char *prop, double num);
|
2018-04-25 23:28:57 +08:00
|
|
|
void jsonw_uint_field(json_writer_t *self, const char *prop, unsigned int num);
|
|
|
|
void jsonw_u64_field(json_writer_t *self, const char *prop, uint64_t num);
|
2018-03-30 00:10:41 +08:00
|
|
|
void jsonw_xint_field(json_writer_t *self, const char *prop, uint64_t num);
|
2017-08-18 01:35:50 +08:00
|
|
|
void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
|
2018-04-25 23:28:57 +08:00
|
|
|
void jsonw_int_field(json_writer_t *self, const char *prop, int num);
|
|
|
|
void jsonw_s64_field(json_writer_t *self, const char *prop, int64_t num);
|
2015-08-22 05:14:51 +08:00
|
|
|
void jsonw_null_field(json_writer_t *self, const char *prop);
|
2018-04-25 23:28:57 +08:00
|
|
|
void jsonw_luint_field(json_writer_t *self, const char *prop,
|
|
|
|
unsigned long int num);
|
2017-08-18 01:35:50 +08:00
|
|
|
void jsonw_lluint_field(json_writer_t *self, const char *prop,
|
|
|
|
unsigned long long int num);
|
|
|
|
void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
|
|
|
|
const char *fmt, double val);
|
2015-08-22 05:14:51 +08:00
|
|
|
|
|
|
|
/* Collections */
|
|
|
|
void jsonw_start_object(json_writer_t *self);
|
|
|
|
void jsonw_end_object(json_writer_t *self);
|
|
|
|
|
|
|
|
void jsonw_start_array(json_writer_t *self);
|
|
|
|
void jsonw_end_array(json_writer_t *self);
|
|
|
|
|
|
|
|
/* Override default exception handling */
|
|
|
|
typedef void (jsonw_err_handler_fn)(const char *);
|
|
|
|
|
|
|
|
#endif /* _JSON_WRITER_H_ */
|