summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-01-09 16:56:00 +0100
committerLennart Poettering <lennart@poettering.net>2021-01-09 17:35:58 +0100
commit10d7126365587e449ffe81b9ab7335be4e10cc68 (patch)
treeb56de028f1b5e58ee1a5fef5cadd9c3e6a7c1945
parent28abf5ad3483a417d3d4de561533d282493a7f2a (diff)
downloadsystemd-10d7126365587e449ffe81b9ab7335be4e10cc68.tar.gz
json: add new json format flag for disabling JSON output
This adds a new flag JSON_FORMAT_OFF that is a marker for "no JSON output please!". Of course, this flag sounds pointless in a JSON implementation, however this is useful in code that can generate JSON output, but also more human friendly output (for example our table formatters). With this in place various tools that so far maintained one boolean field "arg_json" that controlled whether JSON output was requested at all and another field "arg_json_format_flags" for selecing the precise json output flags may merge them into one, simplifying code a bit.
-rw-r--r--src/shared/format-table.c3
-rw-r--r--src/shared/json.c3
-rw-r--r--src/shared/json.h1
3 files changed, 7 insertions, 0 deletions
diff --git a/src/shared/format-table.c b/src/shared/format-table.c
index a13a198b7a..645e5b9afa 100644
--- a/src/shared/format-table.c
+++ b/src/shared/format-table.c
@@ -2536,6 +2536,9 @@ int table_print_json(Table *t, FILE *f, JsonFormatFlags flags) {
assert(t);
+ if (flags & JSON_FORMAT_OFF) /* If JSON output is turned off, use regular output */
+ return table_print(t, f);
+
if (!f)
f = stdout;
diff --git a/src/shared/json.c b/src/shared/json.c
index 6beb56cfa0..9a1a1787df 100644
--- a/src/shared/json.c
+++ b/src/shared/json.c
@@ -1766,6 +1766,9 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) {
assert_return(v, -EINVAL);
assert_return(ret, -EINVAL);
+ if (flags & JSON_FORMAT_OFF)
+ return -ENOEXEC;
+
{
_cleanup_fclose_ FILE *f = NULL;
diff --git a/src/shared/json.h b/src/shared/json.h
index abc0dccddb..a69e1de5c0 100644
--- a/src/shared/json.h
+++ b/src/shared/json.h
@@ -175,6 +175,7 @@ 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 */
} JsonFormatFlags;
int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret);