AtteanX/Serializer/SPARQLJSON.pm

filter: Statementstmt 97.1% 67 / 69 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 100% 5 / 5 totaltotal 94.1% 96 / 102 CC 9 SCAR 14.7
CC
9
Cov
94%
serialize_iter_to_io
9.1
canonical_iri
2.0
BEGIN
1.0
CRAP 4.3
« Attean/API/Plan.pm ↑ Summary Attean/Expression.pm »
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.041
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 =item C<< canonical_iri >>
34  
35 Returns the canonical IRI of this format.
36  
37 =cut
38  
39 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 2 package AtteanX::Serializer::SPARQLJSON 0.041 {
43 3 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶ use Types::Standard qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 0 ▶ sub canonical_iri { return 'http://www.w3.org/ns/formats/SPARQL_Results_JSON' }
Subroutine canonical_iri: not called CC=1 SCAR=6.9
Pod documented
52 2 has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'application/sparql-results+json');
53  
54 =item C<< media_types >>
55  
56 Returns a list of media types that identify the format produced by this serializer.
57  
58 =cut
59  
60 14 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
61 14 return [qw(application/sparql-results+json)];
62 }
63  
64 =item C<< file_extensions >>
65  
66 Returns a list of file extensions associated with the serialized format.
67  
68 =cut
69  
70 7 ▶ sub file_extensions { return [qw(srj json)] };
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
71  
72 =item C<< serialize_iter_to_io( $fh, $iterator ) >>
73  
74 Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the
75 L<IO::Handle> object C<< $fh >>.
76  
77 =cut
78  
79 4 ▶ sub serialize_iter_to_io {
Subroutine serialize_iter_to_io: called CC=9 SCAR=22.0
Pod documented
80 4 my $self = shift;
81 4 my $fh = shift;
82 4 my $iter = shift;
83  
84 4 my $data = {};
85 4 ▶ if ($iter->does('Attean::API::TermIterator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->does('Attean::API::TermIterator')) { } 2 2 4
86 2 my $b = $iter->get_boolean;
87 2 $data->{head} = {};
88 2 ▶ $data->{boolean} = ($b) ? $JSON::true : $JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$b ? : 2 0 2
89 } else {
90 2 my @vars = sort @{ $iter->variables };
91 2 $data->{head}{vars} = \@vars;
92 2 $data->{results} = { bindings => [] };
93 2 ▶ while (my $t = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 6 2 8
94 6 my %binding;
95 6 foreach my $name ($t->variables) {
96 14 my $term = $t->value($name);
97 14 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 14 0 14
98 14 my $type;
99 14 ▶ if ($term->does('Attean::API::IRI')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($term->does('Attean::API::IRI')) { } 6 8 14
elsif ($term->does('Attean::API::Literal')) { } 4 4 8
elsif ($term->does('Attean::API::Blank')) { } 4 0 4
100 6 $type = 'uri';
101 } elsif ($term->does('Attean::API::Literal')) {
102 4 $type = 'literal';
103 } elsif ($term->does('Attean::API::Blank')) {
104 4 $type = 'bnode';
105 } else {
106 0 die 'Term object has an unrecognized type: ' . ref($term);
107 }
108 14 $binding{$name} = { type => $type, value => $term->value };
109 }
110 }
111 6 push(@{ $data->{results}{bindings} }, { %binding });
112 }
113 }
114  
115 4 print {$fh} JSON->new->canonical(1)->encode($data);
116 4 return;
117  
118 }
119  
120 =item C<< serialize_iter_to_bytes( $iterator ) >>
121  
122 Serializes the L<Attean::API::Binding> objects from C<< $iterator >>
123 and returns the serialization as a UTF-8 encoded byte string.
124  
125 =cut
126  
127 2 ▶ sub serialize_iter_to_bytes {
Subroutine serialize_iter_to_bytes: called CC=1 SCAR=0.0
Pod documented
128  
129 2 my $self = shift;
130 2 my $iter = shift;
131 2 my $data = encode('UTF-8', '');
132  
133 2 open(my $fh, '>:encoding(UTF-8)', \$data);
134 2 $self->serialize_iter_to_io($fh, $iter);
135 2 close($fh);
136 2 return $data;
137  
138 }
139  
140 2 with 'Attean::API::ResultSerializer', 'Attean::API::AppendableSerializer';
141 }
142  
143 2 1;
144  
145 __END__
« Attean/API/Plan.pm ↑ Summary Attean/Expression.pm »