]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
json: add new output flag JSON_PRETTY_AUTO 14208/head
authorLennart Poettering <lennart@poettering.net>
Thu, 15 Aug 2019 07:36:04 +0000 (09:36 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 2 Dec 2019 08:47:00 +0000 (09:47 +0100)
This takes inspiration from JSON_COLOR_AUTO: it will automatically map
to JSON_PRETTY if connected to a TTY and JSON_NEWLINE otherwise.

src/shared/json.c
src/shared/json.h

index 2327cbcc9ce431590d712c3d6a0bf717ceeb3cca..869aa279eeef008a0216272913ea5981327b16d4 100644 (file)
@@ -1757,6 +1757,9 @@ void json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const cha
         if (((flags & (JSON_FORMAT_COLOR_AUTO|JSON_FORMAT_COLOR)) == JSON_FORMAT_COLOR_AUTO) && colors_enabled())
                 flags |= JSON_FORMAT_COLOR;
 
+        if (((flags & (JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_PRETTY)) == JSON_FORMAT_PRETTY_AUTO))
+                flags |= on_tty() ? JSON_FORMAT_PRETTY : JSON_FORMAT_NEWLINE;
+
         if (flags & JSON_FORMAT_SSE)
                 fputs("data: ", f);
         if (flags & JSON_FORMAT_SEQ)
index a89381dc516880e0865b153ebabb9741b0d75717..b9b300a1d2ab9e4527c28e0d11bc2143c2ef596e 100644 (file)
@@ -161,14 +161,15 @@ struct json_variant_foreach_state {
 int json_variant_get_source(JsonVariant *v, const char **ret_source, unsigned *ret_line, unsigned *ret_column);
 
 typedef enum JsonFormatFlags {
-        JSON_FORMAT_NEWLINE    = 1 << 0, /* suffix with newline */
-        JSON_FORMAT_PRETTY     = 1 << 1, /* add internal whitespace to appeal to human readers */
-        JSON_FORMAT_COLOR      = 1 << 2, /* insert ANSI color sequences */
-        JSON_FORMAT_COLOR_AUTO = 1 << 3, /* insert ANSI color sequences if colors_enabled() says so */
-        JSON_FORMAT_SOURCE     = 1 << 4, /* prefix with source filename/line/column */
-        JSON_FORMAT_SSE        = 1 << 5, /* prefix/suffix with W3C server-sent events */
-        JSON_FORMAT_SEQ        = 1 << 6, /* prefix/suffix with RFC 7464 application/json-seq */
-        JSON_FORMAT_FLUSH      = 1 << 7, /* call fflush() after dumping JSON */
+        JSON_FORMAT_NEWLINE     = 1 << 0, /* suffix with newline */
+        JSON_FORMAT_PRETTY      = 1 << 1, /* add internal whitespace to appeal to human readers */
+        JSON_FORMAT_PRETTY_AUTO = 1 << 2, /* same, but only if connected to a tty (and JSON_FORMAT_NEWLINE otherwise) */
+        JSON_FORMAT_COLOR       = 1 << 3, /* insert ANSI color sequences */
+        JSON_FORMAT_COLOR_AUTO  = 1 << 4, /* insert ANSI color sequences if colors_enabled() says so */
+        JSON_FORMAT_SOURCE      = 1 << 5, /* prefix with source filename/line/column */
+        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 */
 } JsonFormatFlags;
 
 int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret);