From: WanBingjiang Date: Mon, 23 Feb 2026 08:32:48 +0000 (+0800) Subject: jsonwrt: simplify ul_jsonwrt_empty() and add comments for COMPACT format X-Git-Tag: v2.43-devel~50^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c0c03f6b80471eb51149eb6ecb90b3c70f0a5f70;p=thirdparty%2Futil-linux.git jsonwrt: simplify ul_jsonwrt_empty() and add comments for COMPACT format 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. --- diff --git a/lib/jsonwrt.c b/lib/jsonwrt.c index 454cfb6b2..57f5703da 100644 --- a/lib/jsonwrt.c +++ b/lib/jsonwrt.c @@ -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); diff --git a/misc-utils/blkid.c b/misc-utils/blkid.c index 8c1f4fc61..29f2046f7 100644 --- a/misc-utils/blkid.c +++ b/misc-utils/blkid.c @@ -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"); }