summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-01-25 21:13:36 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2017-01-25 21:13:36 +0100
commitcd4107b782ad91e7d9ee0eb15528fec6472223a8 (patch)
tree7400fe161f5fe2429105dd41f85003eb4595d1dc
parent18c25e8bff2e16796e23f921e6add38030576c84 (diff)
downloadjson-rpc-shell-cd4107b782ad91e7d9ee0eb15528fec6472223a8.tar.gz
json-rpc-shell-cd4107b782ad91e7d9ee0eb15528fec6472223a8.tar.xz
json-rpc-shell-cd4107b782ad91e7d9ee0eb15528fec6472223a8.zip
json-format.pl: add a --keep-ws switch
-rwxr-xr-xjson-format.pl23
1 files changed, 12 insertions, 11 deletions
diff --git a/json-format.pl b/json-format.pl
index 49fb5ce..f43488c 100755
--- a/json-format.pl
+++ b/json-format.pl
@@ -16,14 +16,15 @@ my %format = (
ERROR => color('bold white on_red'),
);
-my $color = 'auto';
-my $help;
-if (!GetOptions('color=s' => \$color, 'help' => \$help) || $help) {
+my ($color, $keep_ws, $help) = 'auto';
+if (!GetOptions('color=s' => \$color, 'keep-ws' => \$keep_ws, 'help' => \$help)
+ || $help) {
print STDERR
"Usage: $0 [OPTION...] [FILE...]\n" .
"Pretty-print and colorify JSON\n" .
"\n" .
" --help print this help\n" .
+ " --keep-ws retain all original whitespace\n" .
" --color=COLOR 'always', 'never' or 'auto' (the default)\n";
exit 2;
}
@@ -77,7 +78,7 @@ sub nexttoken ($) {
sub skip_ws ($) {
my $json = shift;
while (my ($token, $text) = nexttoken $json) {
- next if $token eq 'WS';
+ next if !$keep_ws && $token eq 'WS';
return $token, $text;
}
return;
@@ -103,9 +104,9 @@ sub do_object ($) {
}
if ($token eq 'RBRACE') {
$indent--;
- printindent;
+ printindent unless $keep_ws;
} elsif ($first) {
- printindent;
+ printindent unless $keep_ws;
$first = 0;
}
do_value $token, $text, $json;
@@ -119,9 +120,9 @@ sub do_array ($) {
while (my ($token, $text) = skip_ws $json) {
if ($token eq 'RBRACKET') {
$indent--;
- printindent;
+ printindent unless $keep_ws;
} elsif ($first) {
- printindent;
+ printindent unless $keep_ws;
$first = 0;
}
do_value $token, $text, $json;
@@ -143,9 +144,9 @@ sub do_value ($$$) {
$indent++;
do_array $json;
} elsif ($token eq 'COMMA') {
- printindent;
+ printindent unless $keep_ws;
} elsif ($token eq 'COLON') {
- print ' ';
+ print ' ' unless $keep_ws;
}
}
@@ -153,4 +154,4 @@ my @buffer;
while (my ($token, $text) = skip_ws \@buffer) {
do_value $token, $text, \@buffer;
}
-print "\n";
+print "\n" unless $keep_ws;