From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: [RFC iproute2-next 14/16] json: make pretty printing optional Date: Thu, 1 Feb 2018 17:19:44 -0800 Message-ID: <20180202011946.21929-15-sthemmin@microsoft.com> References: <20180202011946.21929-1-sthemmin@microsoft.com> Cc: netdev@vger.kernel.org, Stephen Hemminger , Stephen Hemminger To: dsahern@gmail.com Return-path: Received: from mail-pf0-f194.google.com ([209.85.192.194]:42280 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751935AbeBBBUV (ORCPT ); Thu, 1 Feb 2018 20:20:21 -0500 Received: by mail-pf0-f194.google.com with SMTP id b25so16409031pfd.9 for ; Thu, 01 Feb 2018 17:20:21 -0800 (PST) In-Reply-To: <20180202011946.21929-1-sthemmin@microsoft.com> Sender: netdev-owner@vger.kernel.org List-ID: 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 --- include/json_print.h | 2 ++ include/utils.h | 1 - ip/ip.c | 3 +++ lib/json_print.c | 3 ++- tc/tc.c | 3 +++ 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/json_print.h b/include/json_print.h index 2ca7830adbd6..45a817ce6b9a 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -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/include/utils.h b/include/utils.h index 27ba37c5cd2f..e35ea32c1d3b 100644 --- a/include/utils.h +++ b/include/utils.h @@ -23,7 +23,6 @@ extern int resolve_hosts; extern int oneline; extern int brief; extern int json; -extern int pretty; extern int timestamp; extern int timestamp_short; extern const char * _SL_; diff --git a/ip/ip.c b/ip/ip.c index b15e6b66b3f6..a6611292808d 100644 --- 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; diff --git a/lib/json_print.c b/lib/json_print.c index 6518ba98f5bf..e3da1bdfd5b0 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -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 63e64fece87d..aba5c101739c 100644 --- 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; -- 2.15.1