From: Karel Zak Date: Wed, 3 Apr 2024 12:00:45 +0000 (+0200) Subject: libsmartcols: print empty arrays in better way X-Git-Tag: v2.42-start~449^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5a61a4abf681cb9e595ea10021df0ff7bc991045;p=thirdparty%2Futil-linux.git libsmartcols: print empty arrays in better way Use "[]" rather than "[null]" to print empty array. Old version: "mountpoints": [ null ], New version: "mountpoints": [], Fixes: https://github.com/util-linux/util-linux/issues/2871 Signed-off-by: Karel Zak --- 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; + } } }