]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/json_print.c
8e7f32dca2ce18c6d1ffa91aabcd188e274ac296
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 void print_color_##type_name(enum output_type t, \
127 enum color_attr color, \
132 if (_IS_JSON_CONTEXT(t)) { \
134 jsonw_##type_name(_jw, value); \
136 jsonw_##type_name##_field(_jw, key, value); \
137 } else if (_IS_FP_CONTEXT(t)) { \
138 color_fprintf(stdout, color, fmt, value); \
141 _PRINT_FUNC(int, int);
142 _PRINT_FUNC(s64
, int64_t);
143 _PRINT_FUNC(hhu
, unsigned char);
144 _PRINT_FUNC(hu
, unsigned short);
145 _PRINT_FUNC(uint
, unsigned int);
146 _PRINT_FUNC(u64
, uint64_t);
147 _PRINT_FUNC(luint
, unsigned long);
148 _PRINT_FUNC(lluint
, unsigned long long);
149 _PRINT_FUNC(float, double);
152 #define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \
153 void print_##type_name##_name_value(const char *name, type value)\
155 SPRINT_BUF(format); \
157 snprintf(format, SPRINT_BSIZE, \
158 "%s %%"#format_char, name); \
159 print_##type_name(PRINT_ANY, name, format, value); \
161 _PRINT_NAME_VALUE_FUNC(uint
, unsigned int, u
);
162 _PRINT_NAME_VALUE_FUNC(string
, const char*, s
);
163 #undef _PRINT_NAME_VALUE_FUNC
165 void print_color_string(enum output_type type
,
166 enum color_attr color
,
171 if (_IS_JSON_CONTEXT(type
)) {
173 jsonw_name(_jw
, key
);
174 else if (!key
&& value
)
175 jsonw_string(_jw
, value
);
177 jsonw_string_field(_jw
, key
, value
);
178 } else if (_IS_FP_CONTEXT(type
)) {
179 color_fprintf(stdout
, color
, fmt
, value
);
184 * value's type is bool. When using this function in FP context you can't pass
185 * a value to it, you will need to use "is_json_context()" to have different
186 * branch for json and regular output. grep -r "print_bool" for example
188 void print_color_bool(enum output_type type
,
189 enum color_attr color
,
194 if (_IS_JSON_CONTEXT(type
)) {
196 jsonw_bool_field(_jw
, key
, value
);
198 jsonw_bool(_jw
, value
);
199 } else if (_IS_FP_CONTEXT(type
)) {
200 color_fprintf(stdout
, color
, fmt
, value
? "true" : "false");
205 * In JSON context uses hardcode %#x format: 42 -> 0x2a
207 void print_color_0xhex(enum output_type type
,
208 enum color_attr color
,
211 unsigned long long hex
)
213 if (_IS_JSON_CONTEXT(type
)) {
216 snprintf(b1
, sizeof(b1
), "%#llx", hex
);
217 print_string(PRINT_JSON
, key
, NULL
, b1
);
218 } else if (_IS_FP_CONTEXT(type
)) {
219 color_fprintf(stdout
, color
, fmt
, hex
);
223 void print_color_hex(enum output_type type
,
224 enum color_attr color
,
229 if (_IS_JSON_CONTEXT(type
)) {
232 snprintf(b1
, sizeof(b1
), "%x", hex
);
234 jsonw_string_field(_jw
, key
, b1
);
236 jsonw_string(_jw
, b1
);
237 } else if (_IS_FP_CONTEXT(type
)) {
238 color_fprintf(stdout
, color
, fmt
, hex
);
243 * In JSON context we don't use the argument "value" we simply call jsonw_null
244 * whereas FP context can use "value" to output anything
246 void print_color_null(enum output_type type
,
247 enum color_attr color
,
252 if (_IS_JSON_CONTEXT(type
)) {
254 jsonw_null_field(_jw
, key
);
257 } else if (_IS_FP_CONTEXT(type
)) {
258 color_fprintf(stdout
, color
, fmt
, value
);
262 /* Print line separator (if not in JSON mode) */