json-format.pl: add a --keep-ws switch
This commit is contained in:
parent
18c25e8bff
commit
cd4107b782
@ -16,14 +16,15 @@ my %format = (
|
|||||||
ERROR => color('bold white on_red'),
|
ERROR => color('bold white on_red'),
|
||||||
);
|
);
|
||||||
|
|
||||||
my $color = 'auto';
|
my ($color, $keep_ws, $help) = 'auto';
|
||||||
my $help;
|
if (!GetOptions('color=s' => \$color, 'keep-ws' => \$keep_ws, 'help' => \$help)
|
||||||
if (!GetOptions('color=s' => \$color, 'help' => \$help) || $help) {
|
|| $help) {
|
||||||
print STDERR
|
print STDERR
|
||||||
"Usage: $0 [OPTION...] [FILE...]\n" .
|
"Usage: $0 [OPTION...] [FILE...]\n" .
|
||||||
"Pretty-print and colorify JSON\n" .
|
"Pretty-print and colorify JSON\n" .
|
||||||
"\n" .
|
"\n" .
|
||||||
" --help print this help\n" .
|
" --help print this help\n" .
|
||||||
|
" --keep-ws retain all original whitespace\n" .
|
||||||
" --color=COLOR 'always', 'never' or 'auto' (the default)\n";
|
" --color=COLOR 'always', 'never' or 'auto' (the default)\n";
|
||||||
exit 2;
|
exit 2;
|
||||||
}
|
}
|
||||||
@ -77,7 +78,7 @@ sub nexttoken ($) {
|
|||||||
sub skip_ws ($) {
|
sub skip_ws ($) {
|
||||||
my $json = shift;
|
my $json = shift;
|
||||||
while (my ($token, $text) = nexttoken $json) {
|
while (my ($token, $text) = nexttoken $json) {
|
||||||
next if $token eq 'WS';
|
next if !$keep_ws && $token eq 'WS';
|
||||||
return $token, $text;
|
return $token, $text;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -103,9 +104,9 @@ sub do_object ($) {
|
|||||||
}
|
}
|
||||||
if ($token eq 'RBRACE') {
|
if ($token eq 'RBRACE') {
|
||||||
$indent--;
|
$indent--;
|
||||||
printindent;
|
printindent unless $keep_ws;
|
||||||
} elsif ($first) {
|
} elsif ($first) {
|
||||||
printindent;
|
printindent unless $keep_ws;
|
||||||
$first = 0;
|
$first = 0;
|
||||||
}
|
}
|
||||||
do_value $token, $text, $json;
|
do_value $token, $text, $json;
|
||||||
@ -119,9 +120,9 @@ sub do_array ($) {
|
|||||||
while (my ($token, $text) = skip_ws $json) {
|
while (my ($token, $text) = skip_ws $json) {
|
||||||
if ($token eq 'RBRACKET') {
|
if ($token eq 'RBRACKET') {
|
||||||
$indent--;
|
$indent--;
|
||||||
printindent;
|
printindent unless $keep_ws;
|
||||||
} elsif ($first) {
|
} elsif ($first) {
|
||||||
printindent;
|
printindent unless $keep_ws;
|
||||||
$first = 0;
|
$first = 0;
|
||||||
}
|
}
|
||||||
do_value $token, $text, $json;
|
do_value $token, $text, $json;
|
||||||
@ -143,9 +144,9 @@ sub do_value ($$$) {
|
|||||||
$indent++;
|
$indent++;
|
||||||
do_array $json;
|
do_array $json;
|
||||||
} elsif ($token eq 'COMMA') {
|
} elsif ($token eq 'COMMA') {
|
||||||
printindent;
|
printindent unless $keep_ws;
|
||||||
} elsif ($token eq 'COLON') {
|
} elsif ($token eq 'COLON') {
|
||||||
print ' ';
|
print ' ' unless $keep_ws;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,4 +154,4 @@ my @buffer;
|
|||||||
while (my ($token, $text) = skip_ws \@buffer) {
|
while (my ($token, $text) = skip_ws \@buffer) {
|
||||||
do_value $token, $text, \@buffer;
|
do_value $token, $text, \@buffer;
|
||||||
}
|
}
|
||||||
print "\n";
|
print "\n" unless $keep_ws;
|
||||||
|
Loading…
Reference in New Issue
Block a user