]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
jsonwrt: simplify ul_jsonwrt_empty() and add comments for COMPACT format
authorWanBingjiang <wanbingjiang@webray.com.cn>
Mon, 23 Feb 2026 08:32:48 +0000 (16:32 +0800)
committerWanBingjiang <wanbingjiang@webray.com.cn>
Mon, 23 Feb 2026 08:32:48 +0000 (16:32 +0800)
Consolidate duplicated LINE/COMPACT branches in ul_jsonwrt_empty()
into a single else-branch, since they produce identical output.

Add comments explaining the indent depth checks in ul_jsonwrt_open()
and ul_jsonwrt_close() for COMPACT format.

lib/jsonwrt.c
misc-utils/blkid.c

index 454cfb6b2d7f43ce24bc4c2a2d6e88ec66c87300..57f5703da5686631d5c558d6a4497ddca96fecee 100644 (file)
@@ -159,6 +159,8 @@ void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
                if (fmt->json_format == UL_JSON_LINE)
                        s = "{";
                else if (fmt->json_format == UL_JSON_COMPACT) {
+                       /* At nesting depth 2, put the object opens on a new line
+                        * for readability; otherwise keep fully compact (no newline). */
                        if (fmt->indent == 2)
                                s = name ? ":{" : "\n{";
                        else
@@ -197,28 +199,22 @@ void ul_jsonwrt_empty(struct ul_jsonwrt *fmt, const char *name, int type)
 
        switch (type) {
        case UL_JSON_OBJECT:
-               if (fmt->json_format == UL_JSON_LINE)
-                       s = name ? ":{}" : "{}";
-               else if (fmt->json_format == UL_JSON_COMPACT)
-                       s = name ? ":{}" : "{}";
-               else
+               if (fmt->json_format == UL_JSON_PRETTY)
                        s = name ? ": {}" : "{}";
+               else
+                       s = name ? ":{}" : "{}";
                break;
        case UL_JSON_ARRAY:
-               if (fmt->json_format == UL_JSON_LINE)
-                       s = name ? ":[]" : "[]";
-               else if (fmt->json_format == UL_JSON_COMPACT)
-                       s = name ? ":[]" : "[]";
-               else
+               if (fmt->json_format == UL_JSON_PRETTY)
                        s = name ? ": []" : "[]";
+               else
+                       s = name ? ":[]" : "[]";
                break;
        case UL_JSON_VALUE:
-               if (fmt->json_format == UL_JSON_LINE)
-                       s = name ? ":null" : "null";
-               else if (fmt->json_format == UL_JSON_COMPACT)
-                       s = name ? ":null" : "null";
-               else
+               if (fmt->json_format == UL_JSON_PRETTY)
                        s = name ? ": null" : "null";
+               else
+                       s = name ? ":null" : "null";
                break;
        }
        fputs(s, fmt->out);
@@ -248,6 +244,8 @@ void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
                return;
        }
 
+       /* In COMPACT, when closing a direct child of root (indent==1),
+        * insert a newline so the closing bracket starts on its own line. */
        if (fmt->json_format == UL_JSON_COMPACT && fmt->indent == 1)
                fputc('\n', fmt->out);
 
index 8c1f4fc616f132ef897d2dfd99a70caa2653aa3d..29f2046f70a8a4b6c2448330f31d66393beabff4 100644 (file)
@@ -925,7 +925,7 @@ int main(int argc, char **argv)
                       "search type specified using -t"));
 
        if (!ctl.eval && ctl.output & OUTPUT_JSON) {
-               ul_jsonwrt_init(ctl.json_fmt, stdout, 0);
+               ul_jsonwrt_init(ctl.json_fmt, stdout, 0, UL_JSON_PRETTY);
                ul_jsonwrt_root_open(ctl.json_fmt);
                ul_jsonwrt_array_open(ctl.json_fmt, "blkid");
        }