]>
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 void new_json_obj(int json
)
26 _jw
= jsonw_new(stdout
);
28 perror("json object");
32 jsonw_pretty(_jw
, true);
33 jsonw_start_array(_jw
);
37 void delete_json_obj(void)
45 bool is_json_context(void)
50 json_writer_t
*get_json_writer(void)
55 void open_json_object(const char *str
)
57 if (_IS_JSON_CONTEXT(PRINT_JSON
)) {
60 jsonw_start_object(_jw
);
64 void close_json_object(void)
66 if (_IS_JSON_CONTEXT(PRINT_JSON
))
67 jsonw_end_object(_jw
);
71 * Start json array or string array using
72 * the provided string as json key (if not null)
73 * or as array delimiter in non-json context.
75 void open_json_array(enum output_type type
, const char *str
)
77 if (_IS_JSON_CONTEXT(type
)) {
80 jsonw_start_array(_jw
);
81 } else if (_IS_FP_CONTEXT(type
)) {
87 * End json array or string array
89 void close_json_array(enum output_type type
, const char *str
)
91 if (_IS_JSON_CONTEXT(type
)) {
93 } else if (_IS_FP_CONTEXT(type
)) {
99 * pre-processor directive to generate similar
100 * functions handling different types
102 #define _PRINT_FUNC(type_name, type) \
103 __attribute__((format(printf, 4, 0))) \
104 void print_color_##type_name(enum output_type t, \
105 enum color_attr color, \
110 if (_IS_JSON_CONTEXT(t)) { \
112 jsonw_##type_name(_jw, value); \
114 jsonw_##type_name##_field(_jw, key, value); \
115 } else if (_IS_FP_CONTEXT(t)) { \
116 color_fprintf(stdout, color, fmt, value); \
119 _PRINT_FUNC(int, int);
120 _PRINT_FUNC(s64
, int64_t);
121 _PRINT_FUNC(hhu
, unsigned char);
122 _PRINT_FUNC(hu
, unsigned short);
123 _PRINT_FUNC(uint
, unsigned int);
124 _PRINT_FUNC(u64
, uint64_t);
125 _PRINT_FUNC(luint
, unsigned long);
126 _PRINT_FUNC(lluint
, unsigned long long);
127 _PRINT_FUNC(float, double);
130 void print_color_string(enum output_type type
,
131 enum color_attr color
,
136 if (_IS_JSON_CONTEXT(type
)) {
138 jsonw_name(_jw
, key
);
139 else if (!key
&& value
)
140 jsonw_string(_jw
, value
);
142 jsonw_string_field(_jw
, key
, value
);
143 } else if (_IS_FP_CONTEXT(type
)) {
144 color_fprintf(stdout
, color
, fmt
, value
);
149 * value's type is bool. When using this function in FP context you can't pass
150 * a value to it, you will need to use "is_json_context()" to have different
151 * branch for json and regular output. grep -r "print_bool" for example
153 void print_color_bool(enum output_type type
,
154 enum color_attr color
,
159 if (_IS_JSON_CONTEXT(type
)) {
161 jsonw_bool_field(_jw
, key
, value
);
163 jsonw_bool(_jw
, value
);
164 } else if (_IS_FP_CONTEXT(type
)) {
165 color_fprintf(stdout
, color
, fmt
, value
? "true" : "false");
170 * In JSON context uses hardcode %#x format: 42 -> 0x2a
172 void print_color_0xhex(enum output_type type
,
173 enum color_attr color
,
176 unsigned long long hex
)
178 if (_IS_JSON_CONTEXT(type
)) {
181 snprintf(b1
, sizeof(b1
), "%#llx", hex
);
182 print_string(PRINT_JSON
, key
, NULL
, b1
);
183 } else if (_IS_FP_CONTEXT(type
)) {
184 color_fprintf(stdout
, color
, fmt
, hex
);
188 void print_color_hex(enum output_type type
,
189 enum color_attr color
,
194 if (_IS_JSON_CONTEXT(type
)) {
197 snprintf(b1
, sizeof(b1
), "%x", hex
);
199 jsonw_string_field(_jw
, key
, b1
);
201 jsonw_string(_jw
, b1
);
202 } else if (_IS_FP_CONTEXT(type
)) {
203 color_fprintf(stdout
, color
, fmt
, hex
);
208 * In JSON context we don't use the argument "value" we simply call jsonw_null
209 * whereas FP context can use "value" to output anything
211 void print_color_null(enum output_type type
,
212 enum color_attr color
,
217 if (_IS_JSON_CONTEXT(type
)) {
219 jsonw_null_field(_jw
, key
);
222 } else if (_IS_FP_CONTEXT(type
)) {
223 color_fprintf(stdout
, color
, fmt
, value
);
227 /* Print line seperator (if not in JSON mode) */