]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/json_print.c
2 * json_print.c "print regular or json output, based on json_writer".
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Julien Fortin, <julien@cumulusnetworks.com>
16 #include "json_print.h"
18 static json_writer_t
*_jw
;
20 #define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
21 #define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
23 static void __new_json_obj(int json
, bool have_array
)
26 _jw
= jsonw_new(stdout
);
28 perror("json object");
32 jsonw_pretty(_jw
, true);
34 jsonw_start_array(_jw
);
38 static void __delete_json_obj(bool have_array
)
47 void new_json_obj(int json
)
49 __new_json_obj(json
, true);
52 void delete_json_obj(void)
54 __delete_json_obj(true);
57 void new_json_obj_plain(int json
)
59 __new_json_obj(json
, false);
62 void delete_json_obj_plain(void)
64 __delete_json_obj(false);
67 bool is_json_context(void)
72 json_writer_t
*get_json_writer(void)
77 void open_json_object(const char *str
)
79 if (_IS_JSON_CONTEXT(PRINT_JSON
)) {
82 jsonw_start_object(_jw
);
86 void close_json_object(void)
88 if (_IS_JSON_CONTEXT(PRINT_JSON
))
89 jsonw_end_object(_jw
);
93 * Start json array or string array using
94 * the provided string as json key (if not null)
95 * or as array delimiter in non-json context.
97 void open_json_array(enum output_type type
, const char *str
)
99 if (_IS_JSON_CONTEXT(type
)) {
101 jsonw_name(_jw
, str
);
102 jsonw_start_array(_jw
);
103 } else if (_IS_FP_CONTEXT(type
)) {
109 * End json array or string array
111 void close_json_array(enum output_type type
, const char *str
)
113 if (_IS_JSON_CONTEXT(type
)) {
114 jsonw_end_array(_jw
);
115 } else if (_IS_FP_CONTEXT(type
)) {
121 * pre-processor directive to generate similar
122 * functions handling different types
124 #define _PRINT_FUNC(type_name, type) \
125 __attribute__((format(printf, 4, 0))) \
126 int print_color_##type_name(enum output_type t, \
127 enum color_attr color, \
133 if (_IS_JSON_CONTEXT(t)) { \
135 jsonw_##type_name(_jw, value); \
137 jsonw_##type_name##_field(_jw, key, value); \
138 } else if (_IS_FP_CONTEXT(t)) { \
139 ret = color_fprintf(stdout, color, fmt, value); \
143 _PRINT_FUNC(int, int);
144 _PRINT_FUNC(s64
, int64_t);
145 _PRINT_FUNC(hhu
, unsigned char);
146 _PRINT_FUNC(hu
, unsigned short);
147 _PRINT_FUNC(uint
, unsigned int);
148 _PRINT_FUNC(u64
, uint64_t);
149 _PRINT_FUNC(luint
, unsigned long);
150 _PRINT_FUNC(lluint
, unsigned long long);
151 _PRINT_FUNC(float, double);
154 #define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \
155 void print_##type_name##_name_value(const char *name, type value)\
157 SPRINT_BUF(format); \
159 snprintf(format, SPRINT_BSIZE, \
160 "%s %%"#format_char, name); \
161 print_##type_name(PRINT_ANY, name, format, value); \
163 _PRINT_NAME_VALUE_FUNC(uint
, unsigned int, u
);
164 _PRINT_NAME_VALUE_FUNC(string
, const char*, s
);
165 #undef _PRINT_NAME_VALUE_FUNC
167 int print_color_string(enum output_type type
,
168 enum color_attr color
,
175 if (_IS_JSON_CONTEXT(type
)) {
177 jsonw_name(_jw
, key
);
178 else if (!key
&& value
)
179 jsonw_string(_jw
, value
);
181 jsonw_string_field(_jw
, key
, value
);
182 } else if (_IS_FP_CONTEXT(type
)) {
183 ret
= color_fprintf(stdout
, color
, fmt
, value
);
190 * value's type is bool. When using this function in FP context you can't pass
191 * a value to it, you will need to use "is_json_context()" to have different
192 * branch for json and regular output. grep -r "print_bool" for example
194 int print_color_bool(enum output_type type
,
195 enum color_attr color
,
202 if (_IS_JSON_CONTEXT(type
)) {
204 jsonw_bool_field(_jw
, key
, value
);
206 jsonw_bool(_jw
, value
);
207 } else if (_IS_FP_CONTEXT(type
)) {
208 ret
= color_fprintf(stdout
, color
, fmt
,
209 value
? "true" : "false");
216 * In JSON context uses hardcode %#x format: 42 -> 0x2a
218 int print_color_0xhex(enum output_type type
,
219 enum color_attr color
,
222 unsigned long long hex
)
226 if (_IS_JSON_CONTEXT(type
)) {
229 snprintf(b1
, sizeof(b1
), "%#llx", hex
);
230 print_string(PRINT_JSON
, key
, NULL
, b1
);
231 } else if (_IS_FP_CONTEXT(type
)) {
232 ret
= color_fprintf(stdout
, color
, fmt
, hex
);
238 int print_color_hex(enum output_type type
,
239 enum color_attr color
,
246 if (_IS_JSON_CONTEXT(type
)) {
249 snprintf(b1
, sizeof(b1
), "%x", hex
);
251 jsonw_string_field(_jw
, key
, b1
);
253 jsonw_string(_jw
, b1
);
254 } else if (_IS_FP_CONTEXT(type
)) {
255 ret
= color_fprintf(stdout
, color
, fmt
, hex
);
262 * In JSON context we don't use the argument "value" we simply call jsonw_null
263 * whereas FP context can use "value" to output anything
265 int print_color_null(enum output_type type
,
266 enum color_attr color
,
273 if (_IS_JSON_CONTEXT(type
)) {
275 jsonw_null_field(_jw
, key
);
278 } else if (_IS_FP_CONTEXT(type
)) {
279 ret
= color_fprintf(stdout
, color
, fmt
, value
);
285 /* Print line separator (if not in JSON mode) */