AtteanX/Serializer/SPARQLJSON.pm

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