aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2024-04-03 13:57:18 +0200
committerKarel Zak <kzak@redhat.com>2024-04-03 13:57:18 +0200
commitb649ff92d2d91864e85a5bba4c89d52236817d9e (patch)
tree99ca0c000d32c0590f0824fbfc70da32312d22a6
parentacdba9c454506cdd29ac400df3f72bde4c74647d (diff)
downloadutil-linux-b649ff92d2d91864e85a5bba4c89d52236817d9e.tar.gz
lib/jsonwrt: introduce ul_jsonwrt_empty()
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>
-rw-r--r--include/jsonwrt.h8
-rw-r--r--lib/jsonwrt.c35
2 files changed, 31 insertions, 12 deletions
diff --git a/include/jsonwrt.h b/include/jsonwrt.h
index 1944d993dc..31e180abe5 100644
--- a/include/jsonwrt.h
+++ b/include/jsonwrt.h
@@ -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 */
diff --git a/lib/jsonwrt.c b/lib/jsonwrt.c
index e21368de2a..365d845ce9 100644
--- a/lib/jsonwrt.c
+++ b/lib/jsonwrt.c
@@ -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);
-}