]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: print empty arrays in better way
authorKarel Zak <kzak@redhat.com>
Wed, 3 Apr 2024 12:00:45 +0000 (14:00 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 3 Apr 2024 12:00:45 +0000 (14:00 +0200)
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>
libsmartcols/src/print.c

index 88ab5a2f0f91414261b538c3e239fcfece806496..a3ef4270a764b931306b047bf171e381bb9f33d3 100644 (file)
@@ -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;
+               }
        }
 }