Make json-pretty support URLs too
This commit is contained in:
parent
4f028a5680
commit
e9e5328a0d
|
@ -2,22 +2,28 @@
|
||||||
|
|
||||||
# json-pretty: Take a JSON file and pretty-print it.
|
# json-pretty: Take a JSON file and pretty-print it.
|
||||||
#
|
#
|
||||||
# Usage: json-pretty <file.json>
|
# Usage: json-pretty <file.json or url>
|
||||||
#
|
#
|
||||||
# --Kirsle
|
# --Kirsle
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use LWP::Simple;
|
||||||
use JSON;
|
use JSON;
|
||||||
|
|
||||||
my $file = shift(@ARGV) or die "Usage: $0 <file.json>\n";
|
my $file = shift(@ARGV) or die "Usage: $0 <file.json or url>\n";
|
||||||
|
|
||||||
my $json = JSON->new->utf8->pretty();
|
my $json = JSON->new->utf8->pretty();
|
||||||
|
|
||||||
|
my $text = "";
|
||||||
|
if ($file =~ /^https?:/i) {
|
||||||
|
$text = get $file;
|
||||||
|
} else {
|
||||||
local $/ = undef;
|
local $/ = undef;
|
||||||
open(my $fh, "<", $file);
|
open(my $fh, "<", $file);
|
||||||
my $text = <$fh>;
|
$text = <$fh>;
|
||||||
close($fh);
|
close($fh);
|
||||||
|
}
|
||||||
|
|
||||||
my $data = $json->decode($text);
|
my $data = $json->decode($text);
|
||||||
print $json->encode($data);
|
print $json->encode($data);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user