aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2024-04-05 09:40:48 +0200
committerKarel Zak <kzak@redhat.com>2024-04-05 09:40:48 +0200
commita2d5c0caf8e54a6622a3d7e5ec140f26cfc671bb (patch)
treeac18430e81a6fb4c1c0b8dcad07a8aaad5001ee5
parent085a313eaed76cc27c36912a81337566f861f3a4 (diff)
parent5a61a4abf681cb9e595ea10021df0ff7bc991045 (diff)
downloadutil-linux-a2d5c0caf8e54a6622a3d7e5ec140f26cfc671bb.tar.gz
Merge branch 'PR/libsmartcols-json-empty' of github.com:karelzak/util-linux-work
* 'PR/libsmartcols-json-empty' of github.com:karelzak/util-linux-work: libsmartcols: print empty arrays in better way lib/jsonwrt: introduce ul_jsonwrt_empty()
-rw-r--r--include/jsonwrt.h8
-rw-r--r--lib/jsonwrt.c35
-rw-r--r--libsmartcols/src/print.c40
3 files changed, 58 insertions, 25 deletions
diff --git a/include/jsonwrt.h b/include/jsonwrt.h
index 1944d993dc..31e180abe5 100644
--- a/include/jsonwrt.h
+++ b/include/jsonwrt.h
@@ -23,6 +23,7 @@ int ul_jsonwrt_is_ready(struct ul_jsonwrt *fmt);
void ul_jsonwrt_indent(struct ul_jsonwrt *fmt);
void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type);
void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type);
+void ul_jsonwrt_empty(struct ul_jsonwrt *fmt, const char *name, int type);
void ul_jsonwrt_flush(struct ul_jsonwrt *fmt);
#define ul_jsonwrt_root_open(_f) ul_jsonwrt_open(_f, NULL, UL_JSON_OBJECT)
@@ -30,14 +31,15 @@ void ul_jsonwrt_flush(struct ul_jsonwrt *fmt);
#define ul_jsonwrt_array_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_ARRAY)
#define ul_jsonwrt_array_close(_f) ul_jsonwrt_close(_f, UL_JSON_ARRAY)
+#define ul_jsonwrt_array_empty(_f, _n) ul_jsonwrt_empty(_f, _n, UL_JSON_ARRAY)
#define ul_jsonwrt_object_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_OBJECT)
#define ul_jsonwrt_object_close(_f) ul_jsonwrt_close(_f, UL_JSON_OBJECT)
+#define ul_jsonwrt_object_empty(_f, _n) ul_jsonwrt_empty(_f, _n, UL_JSON_OBJECT)
#define ul_jsonwrt_value_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_VALUE)
#define ul_jsonwrt_value_close(_f) ul_jsonwrt_close(_f, UL_JSON_VALUE)
-
void ul_jsonwrt_value_raw(struct ul_jsonwrt *fmt,
const char *name, const char *data);
void ul_jsonwrt_value_s(struct ul_jsonwrt *fmt,
@@ -50,7 +52,7 @@ void ul_jsonwrt_value_double(struct ul_jsonwrt *fmt,
const char *name, long double 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);
+
+#define ul_jsonwrt_value_null(_f, _n) ul_jsonwrt_empty(_f, _n, UL_JSON_VALUE)
#endif /* UTIL_LINUX_JSONWRT_H */
diff --git a/lib/jsonwrt.c b/lib/jsonwrt.c
index e21368de2a..365d845ce9 100644
--- a/lib/jsonwrt.c
+++ b/lib/jsonwrt.c
@@ -122,7 +122,7 @@ void ul_jsonwrt_indent(struct ul_jsonwrt *fmt)
fputs(" ", fmt->out);
}
-void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
+static void print_name(struct ul_jsonwrt *fmt, const char *name)
{
if (name) {
if (fmt->after_close)
@@ -135,6 +135,11 @@ void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
else
ul_jsonwrt_indent(fmt);
}
+}
+
+void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
+{
+ print_name(fmt, name);
switch (type) {
case UL_JSON_OBJECT:
@@ -152,6 +157,25 @@ void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
fmt->after_close = 0;
}
+void ul_jsonwrt_empty(struct ul_jsonwrt *fmt, const char *name, int type)
+{
+ print_name(fmt, name);
+
+ switch (type) {
+ case UL_JSON_OBJECT:
+ fputs(name ? ": {}" : "{}", fmt->out);
+ break;
+ case UL_JSON_ARRAY:
+ fputs(name ? ": []" : "[]", fmt->out);
+ break;
+ case UL_JSON_VALUE:
+ fputs(name ? ": null" : "null", fmt->out);
+ break;
+ }
+
+ fmt->after_close = 1;
+}
+
void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
{
assert(fmt->indent > 0);
@@ -178,6 +202,7 @@ void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
fmt->after_close = 1;
}
+
void ul_jsonwrt_flush(struct ul_jsonwrt *fmt)
{
fflush(fmt->out);
@@ -239,11 +264,3 @@ 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/print.c b/libsmartcols/src/print.c
index 88ab5a2f0f..a3ef4270a7 100644
--- a/libsmartcols/src/print.c
+++ b/libsmartcols/src/print.c
@@ -499,20 +499,34 @@ static void print_json_data(struct libscols_table *tb,
break;
case SCOLS_JSON_ARRAY_STRING:
case SCOLS_JSON_ARRAY_NUMBER:
- /* name: [ "aaa", "bbb", "ccc" ] */
- ul_jsonwrt_array_open(&tb->json, name);
-
- if (!scols_column_is_customwrap(cl))
- ul_jsonwrt_value_s(&tb->json, NULL, data);
- else do {
- if (cl->json_type == SCOLS_JSON_ARRAY_STRING)
- ul_jsonwrt_value_s(&tb->json, NULL, data);
+ {
+ /* name: [ "aaa", "bbb", "ccc" ] */
+ int items = 0;
+
+ if (!scols_column_is_customwrap(cl)) {
+ if (data && *data) {
+ ul_jsonwrt_array_open(&tb->json, name);
+ ul_jsonwrt_value_s(&tb->json, NULL, data);
+ items++;
+ }
+ } else do {
+ if (!data || !*data)
+ continue;
+ if (!items)
+ ul_jsonwrt_array_open(&tb->json, name);
+ if (cl->json_type == SCOLS_JSON_ARRAY_STRING)
+ ul_jsonwrt_value_s(&tb->json, NULL, data);
+ else
+ ul_jsonwrt_value_raw(&tb->json, NULL, data);
+ items++;
+ } while (scols_column_next_wrap(cl, NULL, &data) == 0);
+
+ if (!items)
+ ul_jsonwrt_array_empty(&tb->json, name);
else
- ul_jsonwrt_value_raw(&tb->json, NULL, data);
- } while (scols_column_next_wrap(cl, NULL, &data) == 0);
-
- ul_jsonwrt_array_close(&tb->json);
- break;
+ ul_jsonwrt_array_close(&tb->json);
+ break;
+ }
}
}