File: jsonld-compact

package info (click to toggle)
libjsonld-perl 0.006-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 368 kB
  • sloc: perl: 5,090; sh: 32; makefile: 8
file content (43 lines) | stat: -rwxr-xr-x 926 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

use v5.18;
use autodie;
use utf8;
use File::Spec;
use JSON;
use JSONLD;
use open ':std', ':encoding(UTF-8)';

my $data;
my $base;
my $j		= JSON->new->pretty(1)->canonical(1)->allow_nonref(1);
my $context	= {};
while (scalar(@ARGV) and $ARGV[0] =~ /^-(\w+)$/) {
	my $flag	= $1;
	shift;
	if ($flag eq 'v') {
		$JSONLD::debug	= 1;
	} elsif ($flag eq 'c') {
		my $file	= shift;
		open(my $fh, '<:utf8', $file);
		my $j	= JSON->new();
		$context	= $j->decode(do { local($/); <$fh> });
	}
}

if (scalar(@ARGV)) {
	my $file	= shift;
	open(my $fh, '<:utf8', $file);
	$data	= $j->decode(do { local($/); <$fh> });
	$base	= 'file://' . File::Spec->rel2abs($file);
} else {
	$data	= $j->decode(do { local($/); <> });
	$base	= 'http://base.example.org/';
}

my $jld			= JSONLD->new(base_iri => IRI->new($base));
my $expanded	= eval { $jld->compact($data, $context) };
if ($@) {
	die $@;
}
print $j->encode($expanded);