]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
json: add new json format flag for disabling JSON output
authorLennart Poettering <lennart@poettering.net>
Sat, 9 Jan 2021 15:56:00 +0000 (16:56 +0100)
committerLennart Poettering <lennart@poettering.net>
Sat, 9 Jan 2021 16:35:58 +0000 (17:35 +0100)
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.

src/shared/format-table.c
src/shared/json.c
src/shared/json.h

index a13a198b7a780f0b9b4164704cf7c47dad6036b8..645e5b9afa193da410cff076da6b3a6755531154 100644 (file)
@@ -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;
 
index 6beb56cfa0c547d6df2a6877056efec6b4be50c3..9a1a1787df9b983d0f1f2ff1ee22af5ef576be1c 100644 (file)
@@ -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;
 
index abc0dccddbfa9a42206993c74d13841577873c1d..a69e1de5c054243f2e7ec31f5a9eb0521ac04e5b 100644 (file)
@@ -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);