diff options
author | Karel Zak <kzak@redhat.com> | 2024-04-03 14:00:45 +0200 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2024-04-03 14:00:45 +0200 |
commit | 5a61a4abf681cb9e595ea10021df0ff7bc991045 (patch) | |
tree | fa6d91944e3e3917b8db995db22cc5babe7a85b0 | |
parent | b649ff92d2d91864e85a5bba4c89d52236817d9e (diff) | |
download | util-linux-5a61a4abf681cb9e595ea10021df0ff7bc991045.tar.gz |
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 <kzak@redhat.com>
-rw-r--r-- | libsmartcols/src/print.c | 40 |
1 files changed, 27 insertions, 13 deletions
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; + } } } |