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

source code of linux/tools/net/ynl/ynltool/json_writer.h