]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add support for optional boolean values
authorThomas Weißschuh <thomas@t-8ch.de>
Wed, 26 May 2021 11:30:31 +0000 (13:30 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Sun, 30 May 2021 21:17:23 +0000 (23:17 +0200)
These default to `null` instead of `false`.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
include/jsonwrt.h
lib/jsonwrt.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/print.c

index 5be2d70cd6ee25c7d70df088f6fba426dcda5b82..4587b60a9c300f54f3b53ac22c7bc3b4ebd53a9d 100644 (file)
@@ -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 */
index f2003e80836b36f5000a4e1474e267de2596d1d4..9331dec0e77a67eb6cc17863361dcdb460634c10 100644 (file)
@@ -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);
+}
index 4c17eb1dc750308a16ebe5c9e19010176cb2b185..8c4ba814e00cd8192f2d6925311b8a2826c41a11 100644 (file)
@@ -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,
 };
 
 /*
index 9f60148481e8e65f431d4c7ee4039131821be23d..6f6d88e55f760747cafda4bb2c5ac3ca1ada33aa 100644 (file)
@@ -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: