libsmartcols: add support for optional boolean values
These default to `null` instead of `false`. Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
This commit is contained in:
parent
17353ee0cd
commit
f48554d48b
|
@ -40,5 +40,7 @@ void ul_jsonwrt_value_u64(struct ul_jsonwrt *fmt,
|
|||
const char *name, uint64_t data);
|
||||
void ul_jsonwrt_value_boolean(struct ul_jsonwrt *fmt,
|
||||
const char *name, int data);
|
||||
void ul_jsonwrt_value_null(struct ul_jsonwrt *fmt,
|
||||
const char *name);
|
||||
|
||||
#endif /* UTIL_LINUX_JSONWRT_H */
|
||||
|
|
|
@ -214,3 +214,11 @@ void ul_jsonwrt_value_boolean(struct ul_jsonwrt *fmt,
|
|||
fputs(data ? "true" : "false", fmt->out);
|
||||
ul_jsonwrt_value_close(fmt);
|
||||
}
|
||||
|
||||
void ul_jsonwrt_value_null(struct ul_jsonwrt *fmt,
|
||||
const char *name)
|
||||
{
|
||||
ul_jsonwrt_value_open(fmt, name);
|
||||
fputs("null", fmt->out);
|
||||
ul_jsonwrt_value_close(fmt);
|
||||
}
|
||||
|
|
|
@ -95,7 +95,8 @@ enum {
|
|||
SCOLS_JSON_NUMBER = 1,
|
||||
SCOLS_JSON_BOOLEAN = 2,
|
||||
SCOLS_JSON_ARRAY_STRING = 3, /* e.g. for multi-line (SCOLS_FL_WRAP) cells */
|
||||
SCOLS_JSON_ARRAY_NUMBER = 4
|
||||
SCOLS_JSON_ARRAY_NUMBER = 4,
|
||||
SCOLS_JSON_BOOLEAN_OPTIONAL = 5,
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
|
@ -524,11 +524,16 @@ static void print_json_data(struct libscols_table *tb,
|
|||
ul_jsonwrt_value_raw(&tb->json, name, data);
|
||||
break;
|
||||
case SCOLS_JSON_BOOLEAN:
|
||||
/* name: true|false */
|
||||
case SCOLS_JSON_BOOLEAN_OPTIONAL:
|
||||
/* name: true|false|null */
|
||||
if (cl->json_type == SCOLS_JSON_BOOLEAN_OPTIONAL && (!*data || !strcmp(data, "-"))) {
|
||||
ul_jsonwrt_value_null(&tb->json, name);
|
||||
} else {
|
||||
ul_jsonwrt_value_boolean(&tb->json, name,
|
||||
!*data ? 0 :
|
||||
*data == '0' ? 0 :
|
||||
*data == 'N' || *data == 'n' ? 0 : 1);
|
||||
}
|
||||
break;
|
||||
case SCOLS_JSON_ARRAY_STRING:
|
||||
case SCOLS_JSON_ARRAY_NUMBER:
|
||||
|
|
Loading…
Reference in New Issue