json-format.pl: fix display of empty [] and {}

This commit is contained in:
Přemysl Eric Janouch 2017-01-25 19:52:09 +01:00
parent 66f1a6afa3
commit 17331073a4
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 19 additions and 12 deletions

View File

@ -77,6 +77,7 @@ sub do_value ($$$);
sub do_object ($) {
my $json = shift;
my $in_field_name = 1;
my $first = 1;
while (my ($token, $text) = nexttoken $json) {
if ($token eq 'COLON') {
$in_field_name = 0;
@ -88,6 +89,9 @@ sub do_object ($) {
if ($token eq 'RBRACE') {
$indent--;
printindent;
} elsif ($first) {
printindent;
$first = 0;
}
do_value $token, $text, $json;
return if $token eq 'RBRACE';
@ -96,10 +100,14 @@ sub do_object ($) {
sub do_array ($) {
my $json = shift;
my $first = 1;
while (my ($token, $text) = nexttoken $json) {
if ($token eq 'RBRACKET') {
$indent--;
printindent;
} elsif ($first) {
printindent;
$first = 0;
}
do_value $token, $text, $json;
return if $token eq 'RBRACKET';
@ -115,11 +123,9 @@ sub do_value ($$$) {
}
if ($token eq 'LBRACE') {
$indent++;
printindent;
do_object $json;
} elsif ($token eq 'LBRACKET') {
$indent++;
printindent;
do_array $json;
} elsif ($token eq 'COMMA') {
printindent;
@ -129,6 +135,7 @@ sub do_value ($$$) {
}
while (<>) {
# FIXME: this way it doesn't work with pre-formatted JSON
my $json = $_;
my @matches = $json =~ /$any_token/gsc;