Debian Codegraph @

bin/jsonld-expand

raw
1 #!/usr/bin/env perl
2
3 use v5.18;
4 use autodie;
5 use utf8;
6 use File::Spec;
7 use JSON;
8 use JSONLD;
9 use open ':std', ':encoding(UTF-8)';
10
11 my $data;
12 my $base;
13 my $j = JSON->new->pretty(1)->canonical(1)->allow_nonref(1);
14 my $context = {};
15 while (scalar(@ARGV) and $ARGV[0] =~ /^-(\w+)$/) {
16 my $flag = $1;
17 shift;
18 if ($flag eq 'v') {
19 $JSONLD::debug = 1;
20 } elsif ($flag eq 'c') {
21 my $file = shift;
22 open(my $fh, '<:utf8', $file);
23 my $j = JSON->new();
24 $context = $j->decode(do { local($/); <$fh> });
25 }
26 }
27
28 my %args;
29 if (scalar(@{ [keys %$context] })) {
30 $args{expandContext} = $context;
31 }
32
33 if (scalar(@ARGV)) {
34 my $file = shift;
35 open(my $fh, '<:utf8', $file);
36 $data = $j->decode(do { local($/); <$fh> });
37 $base = 'file://' . File::Spec->rel2abs($file);
38 } else {
39 $data = $j->decode(do { local($/); <> });
40 $base = 'http://base.example.org/';
41 }
42
43 my $jld = JSONLD->new(base_iri => IRI->new($base));
44 my $expanded = eval { $jld->expand($data, %args) };
45 if ($@) {
46 die $@;
47 }
48 print $j->encode($expanded);