line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
AtteanX::Serializer::SPARQLJSON - SPARQL Results JSON Serializer |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 VERSION |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
This document describes AtteanX::Serializer::SPARQLJSON version 0.033 |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Attean; |
12
|
|
|
|
|
|
|
my $s = Attean->get_serializer('SPARQLJSON')->new(); |
13
|
|
|
|
|
|
|
$s->serialize_iter_to_io( $fh, $iter ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
... |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=over 4 |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=item C<< canonical_media_type >> |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=item C<< file_extensions >> |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=back |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 METHODS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=over 4 |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
3
|
|
9711
|
use v5.14; |
|
3
|
|
|
|
|
16
|
|
36
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
145
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use Moo; |
39
|
3
|
|
|
3
|
|
19
|
use Types::Standard qw(Str); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
25
|
|
40
|
3
|
|
|
3
|
|
1184
|
use Encode qw(encode); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
63
|
|
41
|
3
|
|
|
3
|
|
1893
|
use Scalar::Util qw(blessed); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
162
|
|
42
|
3
|
|
|
3
|
|
21
|
use Attean::ListIterator; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
116
|
|
43
|
3
|
|
|
3
|
|
17
|
use JSON; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
80
|
|
44
|
3
|
|
|
3
|
|
747
|
use namespace::clean; |
|
3
|
|
|
|
|
8011
|
|
|
3
|
|
|
|
|
28
|
|
45
|
3
|
|
|
3
|
|
447
|
|
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
28
|
|
46
|
|
|
|
|
|
|
has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'application/sparql-results+json'); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item C<< media_types >> |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Returns a list of media types that identify the format produced by this serializer. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
return [qw(application/sparql-results+json)]; |
55
|
|
|
|
|
|
|
} |
56
|
11
|
|
|
11
|
1
|
4190
|
|
57
|
|
|
|
|
|
|
=item C<< file_extensions >> |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Returns a list of file extensions associated with the serialized format. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item C<< serialize_iter_to_io( $fh, $iterator ) >> |
65
|
8
|
|
|
8
|
1
|
30
|
|
66
|
|
|
|
|
|
|
Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the |
67
|
|
|
|
|
|
|
L<IO::Handle> object C<< $fh >>. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $self = shift; |
72
|
|
|
|
|
|
|
my $fh = shift; |
73
|
|
|
|
|
|
|
my $iter = shift; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my @vars = sort @{ $iter->variables }; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $data = { |
78
|
|
|
|
|
|
|
head => { vars => \@vars }, |
79
|
|
|
|
|
|
|
results => { bindings => [] }, |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
while (my $t = $iter->next()) { |
83
|
|
|
|
|
|
|
my %binding; |
84
|
|
|
|
|
|
|
foreach my $name ($t->variables) { |
85
|
|
|
|
|
|
|
my $term = $t->value($name); |
86
|
|
|
|
|
|
|
if (blessed($term)) { |
87
|
|
|
|
|
|
|
my $type; |
88
|
|
|
|
|
|
|
if ($term->does('Attean::API::IRI')) { |
89
|
|
|
|
|
|
|
$type = 'uri'; |
90
|
|
|
|
|
|
|
} elsif ($term->does('Attean::API::Literal')) { |
91
|
|
|
|
|
|
|
$type = 'literal'; |
92
|
|
|
|
|
|
|
} elsif ($term->does('Attean::API::Blank')) { |
93
|
|
|
|
|
|
|
$type = 'bnode'; |
94
|
|
|
|
|
|
|
} else { |
95
|
|
|
|
|
|
|
die 'Term object has an unrecognized type: ' . ref($term); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
$binding{$name} = { type => $type, value => $term->value }; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
push(@{ $data->{results}{bindings} }, { %binding }); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
print {$fh} JSON->new->canonical(1)->encode($data); |
104
|
|
|
|
|
|
|
return; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item C<< serialize_iter_to_bytes( $iterator ) >> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Serializes the L<Attean::API::Binding> objects from C<< $iterator >> |
111
|
|
|
|
|
|
|
and returns the serialization as a UTF-8 encoded byte string. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
my $self = shift; |
117
|
|
|
|
|
|
|
my $iter = shift; |
118
|
|
|
|
|
|
|
my $data = encode('UTF-8', ''); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
open(my $fh, '>:encoding(UTF-8)', \$data); |
121
|
|
|
|
|
|
|
$self->serialize_iter_to_io($fh, $iter); |
122
|
|
|
|
|
|
|
close($fh); |
123
|
|
|
|
|
|
|
return $data; |
124
|
|
|
|
|
|
|
|
125
|
1
|
|
|
1
|
|
12
|
} |
|
1
|
|
|
1
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
1117
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
with 'Attean::API::ResultSerializer', 'Attean::API::AppendableSerializer'; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=back |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 BUGS |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Please report any bugs or feature requests to through the GitHub web interface |
138
|
|
|
|
|
|
|
at L<https://github.com/kasei/attean/issues>. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 SEE ALSO |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L<http://www.w3.org/TR/sparql11-results-json/> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 AUTHOR |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Gregory Todd Williams C<< <gwilliams@cpan.org> >> |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 COPYRIGHT |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Copyright (c) 2014--2022 Gregory Todd Williams. This |
151
|
|
|
|
|
|
|
program is free software; you can redistribute it and/or modify it under |
152
|
|
|
|
|
|
|
the same terms as Perl itself. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |