json_print: Add new json object function not as array item
Currently new json object opens (and delete_json_obj closes) the object as an array, what adds prints for the matching bracket '[' ']' at the start/end of the object. This patch adds new_json_obj_plain() and the matching delete_json_obj_plain() to enable opening and closing json object, not as array and leave it to the using function to decide which type of object to open/close as the main object. Signed-off-by: Ron Diskin <rondi@mellanox.com> Reviewed-by: Moshe Shemesh <moshe@mellanox.com> Reviewed-by: Jiri Pirko <jiri@mellanox.com> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
parent
31ca29b2be
commit
98e48e7dd0
|
|
@ -31,6 +31,8 @@ enum output_type {
|
||||||
|
|
||||||
void new_json_obj(int json);
|
void new_json_obj(int json);
|
||||||
void delete_json_obj(void);
|
void delete_json_obj(void);
|
||||||
|
void new_json_obj_plain(int json);
|
||||||
|
void delete_json_obj_plain(void);
|
||||||
|
|
||||||
bool is_json_context(void);
|
bool is_json_context(void);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ static json_writer_t *_jw;
|
||||||
#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
|
#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
|
||||||
#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
|
#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
|
||||||
|
|
||||||
void new_json_obj(int json)
|
static void __new_json_obj(int json, bool have_array)
|
||||||
{
|
{
|
||||||
if (json) {
|
if (json) {
|
||||||
_jw = jsonw_new(stdout);
|
_jw = jsonw_new(stdout);
|
||||||
|
|
@ -30,16 +30,38 @@ void new_json_obj(int json)
|
||||||
}
|
}
|
||||||
if (pretty)
|
if (pretty)
|
||||||
jsonw_pretty(_jw, true);
|
jsonw_pretty(_jw, true);
|
||||||
jsonw_start_array(_jw);
|
if (have_array)
|
||||||
|
jsonw_start_array(_jw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void __delete_json_obj(bool have_array)
|
||||||
|
{
|
||||||
|
if (_jw) {
|
||||||
|
if (have_array)
|
||||||
|
jsonw_end_array(_jw);
|
||||||
|
jsonw_destroy(&_jw);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void new_json_obj(int json)
|
||||||
|
{
|
||||||
|
__new_json_obj(json, true);
|
||||||
|
}
|
||||||
|
|
||||||
void delete_json_obj(void)
|
void delete_json_obj(void)
|
||||||
{
|
{
|
||||||
if (_jw) {
|
__delete_json_obj(true);
|
||||||
jsonw_end_array(_jw);
|
}
|
||||||
jsonw_destroy(&_jw);
|
|
||||||
}
|
void new_json_obj_plain(int json)
|
||||||
|
{
|
||||||
|
__new_json_obj(json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_json_obj_plain(void)
|
||||||
|
{
|
||||||
|
__delete_json_obj(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_json_context(void)
|
bool is_json_context(void)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue