mirror of
https://github.com/git/git
synced 2024-11-05 18:59:29 +00:00
8c97e38731
The test guarded by PERLJSON added in 75459410ed
("json_writer: new
routines to create JSON data", 2018-07-13) assumed that a JSON boolean
value like "true" or "false" would be represented as "1" or "0" in
Perl.
This behavior can't be relied upon, e.g. with JSON.pm 2.50 and
JSON::PP. A JSON::PP::Boolean object will be represented as "true"
or "false". To work around this let's check if we have any refs left
after we check for hashes and arrays, assume those are JSON objects,
and coerce them to a known boolean value.
The behavior of this test still looks odd to me. Why implement our own
ad-hoc encoder just for some one-off test, as opposed to say Perl's
own Data::Dumper with Sortkeys et al? But with this change it works,
so let's leave it be.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
55 lines
1 KiB
Perl
55 lines
1 KiB
Perl
#!/usr/bin/perl
|
|
use strict;
|
|
use warnings;
|
|
use JSON;
|
|
|
|
sub dump_array {
|
|
my ($label_in, $ary_ref) = @_;
|
|
my @ary = @$ary_ref;
|
|
|
|
for ( my $i = 0; $i <= $#{ $ary_ref }; $i++ )
|
|
{
|
|
my $label = "$label_in\[$i\]";
|
|
dump_item($label, $ary[$i]);
|
|
}
|
|
}
|
|
|
|
sub dump_hash {
|
|
my ($label_in, $obj_ref) = @_;
|
|
my %obj = %$obj_ref;
|
|
|
|
foreach my $k (sort keys %obj) {
|
|
my $label = (length($label_in) > 0) ? "$label_in.$k" : "$k";
|
|
my $value = $obj{$k};
|
|
|
|
dump_item($label, $value);
|
|
}
|
|
}
|
|
|
|
sub dump_item {
|
|
my ($label_in, $value) = @_;
|
|
if (ref($value) eq 'ARRAY') {
|
|
print "$label_in array\n";
|
|
dump_array($label_in, $value);
|
|
} elsif (ref($value) eq 'HASH') {
|
|
print "$label_in hash\n";
|
|
dump_hash($label_in, $value);
|
|
} elsif (ref $value) {
|
|
my $bool = $value ? 1 : 0;
|
|
print "$label_in $bool\n";
|
|
} elsif (defined $value) {
|
|
print "$label_in $value\n";
|
|
} else {
|
|
print "$label_in null\n";
|
|
}
|
|
}
|
|
|
|
my $row = 0;
|
|
while (<>) {
|
|
my $data = decode_json( $_ );
|
|
my $label = "row[$row]";
|
|
|
|
dump_hash($label, $data);
|
|
$row++;
|
|
}
|
|
|