| 1 |
#!/home/ben/software/install/bin/perl |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
use lib '/home/ben/projects/json-parse/blib/lib'; |
| 5 |
use lib '/home/ben/projects/json-parse/blib/arch'; |
| 6 |
use JSON::Parse 'assert_valid_json'; |
| 7 |
use Getopt::Long; |
| 8 |
my $ok = GetOptions ( |
| 9 |
"verbose" => \my $verbose, |
| 10 |
"help" => \my $help, |
| 11 |
); |
| 12 |
if (! $ok || $help) { |
| 13 |
usage (); |
| 14 |
exit; |
| 15 |
} |
| 16 |
for my $file (@ARGV) { |
| 17 |
eval { |
| 18 |
open my $in, "<:raw", $file or die "Can't open '$file': $!"; |
| 19 |
my $text = ''; |
| 20 |
while (my $line = <$in>) { |
| 21 |
$text .= $line; |
| 22 |
} |
| 23 |
close $in or die $!; |
| 24 |
assert_valid_json ($text); |
| 25 |
}; |
| 26 |
if ($@) { |
| 27 |
my $error = $@; |
| 28 |
$error =~ s/\n+$//; |
| 29 |
if ($error !~ /\Q$file/) { |
| 30 |
$error = "$file: $error"; |
| 31 |
} |
| 32 |
if ($error =~ /validjson line [0-9]+\.$/) { |
| 33 |
$error =~ s/\sat\s\S+\sline.*$/\./; |
| 34 |
} |
| 35 |
print "$error\n"; |
| 36 |
} |
| 37 |
else { |
| 38 |
if ($verbose) { |
| 39 |
print "'$file' is valid JSON.\n"; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
sub usage |
| 45 |
{ |
| 46 |
print <<EOF; |
| 47 |
$0: validate JSON. Default behaviour is to produce nothing except errors. |
| 48 |
|
| 49 |
Options: |
| 50 |
|
| 51 |
--verbose Get confirmation that the files are valid. |
| 52 |
--help View this message. |
| 53 |
|
| 54 |
This script requires JSON::Parse to be installed. |
| 55 |
EOF |
| 56 |
|
| 57 |
exit; |
| 58 |
} |