From: Thomas Weißschuh Date: Wed, 26 May 2021 11:30:31 +0000 (+0200) Subject: libsmartcols: add support for optional boolean values X-Git-Tag: v2.38-rc1~485^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f48554d48b002ac2881569ecb088aeef710f9369;p=thirdparty%2Futil-linux.git libsmartcols: add support for optional boolean values These default to `null` instead of `false`. Signed-off-by: Thomas Weißschuh --- diff --git a/include/jsonwrt.h b/include/jsonwrt.h index 5be2d70cd6..4587b60a9c 100644 --- a/include/jsonwrt.h +++ b/include/jsonwrt.h @@ -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 */ diff --git a/lib/jsonwrt.c b/lib/jsonwrt.c index f2003e8083..9331dec0e7 100644 --- a/lib/jsonwrt.c +++ b/lib/jsonwrt.c @@ -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); +} diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 4c17eb1dc7..8c4ba814e0 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -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, }; /* diff --git a/libsmartcols/src/print.c b/libsmartcols/src/print.c index 9f60148481..6f6d88e55f 100644 --- a/libsmartcols/src/print.c +++ b/libsmartcols/src/print.c @@ -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 */ - ul_jsonwrt_value_boolean(&tb->json, name, - !*data ? 0 : - *data == '0' ? 0 : - *data == 'N' || *data == 'n' ? 0 : 1); + 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: