File: validjson

package info (click to toggle)
libjson-parse-perl 0.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 572 kB
  • sloc: ansic: 3,614; perl: 475; makefile: 12
file content (58 lines) | stat: -rwxr-xr-x 1,153 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use lib '/home/ben/projects/json-parse/blib/lib';
use lib '/home/ben/projects/json-parse/blib/arch';
use JSON::Parse 'assert_valid_json';
use Getopt::Long;
my $ok = GetOptions (
    "verbose" => \my $verbose,
    "help" => \my $help,
);
if (! $ok || $help) {
    usage ();
    exit;
}
for my $file (@ARGV) {
    eval {
	open my $in, "<:raw", $file or die "Can't open '$file': $!";
	my $text = '';
	while (my $line = <$in>) {
	    $text .= $line;
	}
	close $in or die $!;
	assert_valid_json ($text);
    };
    if ($@) {
	my $error = $@;
	$error =~ s/\n+$//;
	if ($error !~ /\Q$file/) {
	    $error = "$file: $error";
	}
	if ($error =~ /validjson line [0-9]+\.$/) {
	    $error =~ s/\sat\s\S+\sline.*$/\./;
	}
	print "$error\n";
    }
    else {
	if ($verbose) {
	    print "'$file' is valid JSON.\n";
	}
    }
}

sub usage
{
    print <<EOF;
$0: validate JSON. Default behaviour is to produce nothing except errors.

Options:

--verbose       Get confirmation that the files are valid.
--help          View this message.

This script requires JSON::Parse to be installed.
EOF

    exit;
}