]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/jsonwrt: introduce ul_jsonwrt_empty()
authorKarel Zak <kzak@redhat.com>
Wed, 3 Apr 2024 11:57:18 +0000 (13:57 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 3 Apr 2024 11:57:18 +0000 (13:57 +0200)
The new function optimizes the printing of empty objects and arrays in
a "pretty" way, instead of using the ul_jsonwrt_..._open() and
ul_jsonwrt_..._close() functions, which add extra line breaks to the
output.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/jsonwrt.h
lib/jsonwrt.c

index 1944d993dc3414033c27e808ddd488a7fc74dc24..31e180abe526eee32f4b812dd7d11513f14af71f 100644 (file)
@@ -23,6 +23,7 @@ int ul_jsonwrt_is_ready(struct ul_jsonwrt *fmt);
 void ul_jsonwrt_indent(struct ul_jsonwrt *fmt);
 void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type);
 void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type);
+void ul_jsonwrt_empty(struct ul_jsonwrt *fmt, const char *name, int type);
 void ul_jsonwrt_flush(struct ul_jsonwrt *fmt);
 
 #define ul_jsonwrt_root_open(_f)       ul_jsonwrt_open(_f, NULL, UL_JSON_OBJECT)
@@ -30,14 +31,15 @@ void ul_jsonwrt_flush(struct ul_jsonwrt *fmt);
 
 #define ul_jsonwrt_array_open(_f, _n)  ul_jsonwrt_open(_f, _n, UL_JSON_ARRAY)
 #define ul_jsonwrt_array_close(_f)     ul_jsonwrt_close(_f, UL_JSON_ARRAY)
+#define ul_jsonwrt_array_empty(_f, _n) ul_jsonwrt_empty(_f, _n, UL_JSON_ARRAY)
 
 #define ul_jsonwrt_object_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_OBJECT)
 #define ul_jsonwrt_object_close(_f)    ul_jsonwrt_close(_f, UL_JSON_OBJECT)
+#define ul_jsonwrt_object_empty(_f, _n)        ul_jsonwrt_empty(_f, _n, UL_JSON_OBJECT)
 
 #define ul_jsonwrt_value_open(_f, _n)  ul_jsonwrt_open(_f, _n, UL_JSON_VALUE)
 #define ul_jsonwrt_value_close(_f)     ul_jsonwrt_close(_f, UL_JSON_VALUE)
 
-
 void ul_jsonwrt_value_raw(struct ul_jsonwrt *fmt,
                        const char *name, const char *data);
 void ul_jsonwrt_value_s(struct ul_jsonwrt *fmt,
@@ -50,7 +52,7 @@ void ul_jsonwrt_value_double(struct ul_jsonwrt *fmt,
                        const char *name, long double data);
 void ul_jsonwrt_value_boolean(struct ul_jsonwrt *fmt,
                        const char *name, int data);
-void ul_jsonwrt_value_null(struct ul_jsonwrt *fmt,
-                       const char *name);
+
+#define ul_jsonwrt_value_null(_f, _n)  ul_jsonwrt_empty(_f, _n, UL_JSON_VALUE)
 
 #endif /* UTIL_LINUX_JSONWRT_H */
index e21368de2ac1ab3f5a85022509c60b3f59a4bbab..365d845ce91582e212e38ccb76bb6d9ece9cca5b 100644 (file)
@@ -122,7 +122,7 @@ void ul_jsonwrt_indent(struct ul_jsonwrt *fmt)
                fputs("   ", fmt->out);
 }
 
-void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
+static void print_name(struct ul_jsonwrt *fmt, const char *name)
 {
        if (name) {
                if (fmt->after_close)
@@ -135,6 +135,11 @@ void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
                else
                        ul_jsonwrt_indent(fmt);
        }
+}
+
+void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
+{
+       print_name(fmt, name);
 
        switch (type) {
        case UL_JSON_OBJECT:
@@ -152,6 +157,25 @@ void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
        fmt->after_close = 0;
 }
 
+void ul_jsonwrt_empty(struct ul_jsonwrt *fmt, const char *name, int type)
+{
+       print_name(fmt, name);
+
+       switch (type) {
+       case UL_JSON_OBJECT:
+               fputs(name ? ": {}" : "{}", fmt->out);
+               break;
+       case UL_JSON_ARRAY:
+               fputs(name ? ": []" : "[]", fmt->out);
+               break;
+       case UL_JSON_VALUE:
+               fputs(name ? ": null" : "null", fmt->out);
+               break;
+       }
+
+       fmt->after_close = 1;
+}
+
 void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
 {
        assert(fmt->indent > 0);
@@ -178,6 +202,7 @@ void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
        fmt->after_close = 1;
 }
 
+
 void ul_jsonwrt_flush(struct ul_jsonwrt *fmt)
 {
        fflush(fmt->out);
@@ -239,11 +264,3 @@ void ul_jsonwrt_value_boolean(struct ul_jsonwrt *fmt,
        fputs(data ? "true" : "false", fmt->out);
        ul_jsonwrt_value_close(fmt);
 }
-
-void ul_jsonwrt_value_null(struct ul_jsonwrt *fmt,
-                       const char *name)
-{
-       ul_jsonwrt_value_open(fmt, name);
-       fputs("null", fmt->out);
-       ul_jsonwrt_value_close(fmt);
-}