Loading...
1/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
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 *
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#include <stdarg.h>
17#include <stdio.h>
18#include <linux/compiler.h>
19
20/* Opaque class structure */
21typedef struct json_writer json_writer_t;
22
23/* Create a new JSON stream */
24json_writer_t *jsonw_new(FILE *f);
25/* End output to JSON stream */
26void jsonw_destroy(json_writer_t **self_p);
27
28/* Cause output to have pretty whitespace */
29void jsonw_pretty(json_writer_t *self, bool on);
30
31/* Reset separator to create new JSON */
32void jsonw_reset(json_writer_t *self);
33
34/* Add property name */
35void jsonw_name(json_writer_t *self, const char *name);
36
37/* Add value */
38void __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt,
39 va_list ap);
40void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...);
41void jsonw_string(json_writer_t *self, const char *value);
42void jsonw_bool(json_writer_t *self, bool value);
43void jsonw_float(json_writer_t *self, double number);
44void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
45void jsonw_uint(json_writer_t *self, uint64_t number);
46void jsonw_hu(json_writer_t *self, unsigned short number);
47void jsonw_int(json_writer_t *self, int64_t number);
48void jsonw_null(json_writer_t *self);
49void jsonw_lluint(json_writer_t *self, unsigned long long int num);
50
51/* Useful Combinations of name and value */
52void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
53void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
54void jsonw_float_field(json_writer_t *self, const char *prop, double num);
55void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
56void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
57void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
58void jsonw_null_field(json_writer_t *self, const char *prop);
59void jsonw_lluint_field(json_writer_t *self, const char *prop,
60 unsigned long long int num);
61void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
62 const char *fmt, double val);
63
64/* Collections */
65void jsonw_start_object(json_writer_t *self);
66void jsonw_end_object(json_writer_t *self);
67
68void jsonw_start_array(json_writer_t *self);
69void jsonw_end_array(json_writer_t *self);
70
71/* Override default exception handling */
72typedef void (jsonw_err_handler_fn)(const char *);
73
74#endif /* _JSON_WRITER_H_ */
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#include <stdarg.h>
21
22/* Opaque class structure */
23typedef struct json_writer json_writer_t;
24
25/* Create a new JSON stream */
26json_writer_t *jsonw_new(FILE *f);
27/* End output to JSON stream */
28void jsonw_destroy(json_writer_t **self_p);
29
30/* Cause output to have pretty whitespace */
31void jsonw_pretty(json_writer_t *self, bool on);
32
33/* Add property name */
34void jsonw_name(json_writer_t *self, const char *name);
35
36/* Add value */
37void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap);
38void jsonw_printf(json_writer_t *self, const char *fmt, ...);
39void jsonw_string(json_writer_t *self, const char *value);
40void jsonw_bool(json_writer_t *self, bool value);
41void jsonw_float(json_writer_t *self, double number);
42void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
43void jsonw_uint(json_writer_t *self, uint64_t number);
44void jsonw_hu(json_writer_t *self, unsigned short number);
45void jsonw_int(json_writer_t *self, int64_t number);
46void jsonw_null(json_writer_t *self);
47void jsonw_lluint(json_writer_t *self, unsigned long long int num);
48
49/* Useful Combinations of name and value */
50void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
51void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
52void jsonw_float_field(json_writer_t *self, const char *prop, double num);
53void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
54void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
55void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
56void jsonw_null_field(json_writer_t *self, const char *prop);
57void jsonw_lluint_field(json_writer_t *self, const char *prop,
58 unsigned long long int num);
59void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
60 const char *fmt, double val);
61
62/* Collections */
63void jsonw_start_object(json_writer_t *self);
64void jsonw_end_object(json_writer_t *self);
65
66void jsonw_start_array(json_writer_t *self);
67void jsonw_end_array(json_writer_t *self);
68
69/* Override default exception handling */
70typedef void (jsonw_err_handler_fn)(const char *);
71
72#endif /* _JSON_WRITER_H_ */