]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
json: make pretty printing optional
authorStephen Hemminger <stephen@networkplumber.org>
Thu, 8 Feb 2018 16:26:22 +0000 (08:26 -0800)
committerDavid Ahern <dsahern@gmail.com>
Sat, 10 Feb 2018 16:15:08 +0000 (08:15 -0800)
Since JSON is intended for programmatic consumption, it makes
sense for the default output format to be concise as possible.

For programmer and other uses, it is helpful to keep the pretty
whitespace format; therefore enable it with -p flag.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
include/json_print.h
ip/ip.c
lib/json_print.c
tc/tc.c

index 2ca7830adbd6cfb6f89e690a419fa7da35894e56..45a817ce6b9a9ac8be363191e2c2564235521b01 100644 (file)
@@ -15,6 +15,8 @@
 #include "json_writer.h"
 #include "color.h"
 
+extern int show_pretty;
+
 json_writer_t *get_json_writer(void);
 
 /*
diff --git a/ip/ip.c b/ip/ip.c
index b15e6b66b3f6688fd31cb5b9260a91060e531459..a6611292808d014f289f914d9aa1180ac0980f29 100644 (file)
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -31,6 +31,7 @@ int show_stats;
 int show_details;
 int oneline;
 int brief;
+int show_pretty;
 int json;
 int timestamp;
 const char *_SL_;
@@ -259,6 +260,8 @@ int main(int argc, char **argv)
                        ++brief;
                } else if (matches(opt, "-json") == 0) {
                        ++json;
+               } else if (matches(opt, "-pretty") == 0) {
+                       ++show_pretty;
                } else if (matches(opt, "-rcvbuf") == 0) {
                        unsigned int size;
 
index 6518ba98f5bf8cc0875fa089dcfa2fa7e185f121..e3da1bdfd5b0cfb8080e0406173cba7478c83d21 100644 (file)
@@ -28,7 +28,8 @@ void new_json_obj(int json)
                        perror("json object");
                        exit(1);
                }
-               jsonw_pretty(_jw, true);
+               if (show_pretty)
+                       jsonw_pretty(_jw, true);
                jsonw_start_array(_jw);
        }
 }
diff --git a/tc/tc.c b/tc/tc.c
index 63e64fece87dc8b950b3784060e444f344d05053..aba5c101739c32ac3dc084c84d04a578effd8563 100644 (file)
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -42,6 +42,7 @@ int use_iec;
 int force;
 bool use_names;
 int json;
+int pretty;
 
 static char *conf_file;
 
@@ -484,6 +485,8 @@ int main(int argc, char **argv)
                        ++timestamp_short;
                } else if (matches(argv[1], "-json") == 0) {
                        ++json;
+               } else if (matches(argv[1], "-pretty") == 0) {
+                       ++pretty;
                } else {
                        fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
                        return -1;