From c3f0bff9456dcb79f5c929f3d9b15a1a884735df Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 17 Nov 2022 15:40:54 +0100 Subject: [PATCH] bootctl: use output mode where "[]" is written instead for empty output It's easier for the caller if output is always a list, even if there are no entries. --- src/shared/bootspec.c | 2 +- src/shared/json.c | 8 ++++++-- src/shared/json.h | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 435fa4c1bb..8ae891e8a0 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -1448,7 +1448,7 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) { return log_oom(); } - json_variant_dump(array, json_format, NULL, NULL); + json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL); } else { for (size_t n = 0; n < config->n_entries; n++) { diff --git a/src/shared/json.c b/src/shared/json.c index fe03f23046..b1ef0ed349 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -1810,8 +1810,12 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) { } int json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const char *prefix) { - if (!v) - return 0; + if (!v) { + if (flags & JSON_FORMAT_EMPTY_ARRAY) + v = JSON_VARIANT_MAGIC_EMPTY_ARRAY; + else + return 0; + } if (!f) f = stdout; diff --git a/src/shared/json.h b/src/shared/json.h index b1658b5f4b..8d060e7877 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -194,7 +194,8 @@ typedef enum JsonFormatFlags { JSON_FORMAT_SSE = 1 << 6, /* prefix/suffix with W3C server-sent events */ JSON_FORMAT_SEQ = 1 << 7, /* prefix/suffix with RFC 7464 application/json-seq */ JSON_FORMAT_FLUSH = 1 << 8, /* call fflush() after dumping JSON */ - JSON_FORMAT_OFF = 1 << 9, /* make json_variant_format() fail with -ENOEXEC */ + JSON_FORMAT_EMPTY_ARRAY = 1 << 9, /* output "[]" for empty input */ + JSON_FORMAT_OFF = 1 << 10, /* make json_variant_format() fail with -ENOEXEC */ } JsonFormatFlags; int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret); -- 2.25.1