blib/lib/Google/Data/JSON.pm

filter: Statementstmt 76.3% 142 / 186 Branchbran 50.0% 35 / 70 Conditioncond 54.2% 19 / 35 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 75.9% 41 / 54 Podpod 100% 27 / 27 totaltotal 68.2% 264 / 387 CC 50 SCAR 23.2
CC
50
Cov
67%
new
36.0
error
20.0
as_atom
13.8
CRAP 10.2
↑ Summary
1 package Google::Data::JSON;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use version; our $VERSION = qv('0.1.10');
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::MoreUtils qw( any uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Perl6::Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ## XML::Simple
17 3 my $CONFIG = {
18 XMLin => {
19 KeepRoot => 1,
20 ContentKey => '$t',
21 KeyAttr => [],
22 ForceArray => 0,
23 ForceContent => 1,
24 },
25 XMLout => {
26 KeepRoot => 1,
27 ContentKey => '$t',
28 KeyAttr => [],
29 XMLDecl => '<?xml version="1.0" encoding="utf-8"?>',
30 NoSort => 1,
31 }
32 };
33  
34 3 ▶use vars qw( $ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 0 ▶sub error {
Subroutine error: not called CC=4 SCAR=30.0
Pod documented
37 0 ▶ my $msg = $_[1] || '';
Condition0%0/2
$_[1] || ''
l!l
00
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
38 0 ▶ $msg .= "\n" unless $msg =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 0 0 0
39 0 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
40 0 $_[0]->{_errstr} = $msg;
41 } else {
42 0 $ERROR = $msg;
43 }
44 0 return;
45 }
46  
47 0 ▶sub errstr { ref($_[0]) ? $_[0]->{_errstr} : $ERROR }
Subroutine errstr: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
48  
49 6 ▶sub new {
Subroutine new: called CC=17 SCAR=35.8
Pod documented
50 6 my $class = shift;
51 6 my ($type, $stream);
52 6 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 6 0 6
53 6 ($type, $stream) = @_;
54 6 ▶ if ($type eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "file") 2 4 6
55 2 $stream = read_file $stream;
56 2 $type = get_type_as_dwim($stream);
57 }
58 }
59 else {
60 0 warn 'DWIM-style constructor is DEPRECATED';
61 0 ($stream) = @_;
62 0 ▶ $stream = read_file $stream if $stream !~ /[\r\n]/ && -f $stream;
Branch: 1 branch
BranchTrueFalseTotal
if not $stream =~ /[\r\n]/ and -f $stream 0 0 0
Condition0%0/3
not $stream =~ /[\r\n]/ and -f $stream
!ll&&!rl&&r
000
MC/DC0%0/2
not $stream =~ /[\r\n]/ and -f $stream
not $stream =~ /[\r\n]/ -f $stream
Truth table0%0/3
not(A) and B
A= not $stream =~ /[\r\n]/B= -f $stream
ABresult
0X0
100
111
63 0 $type = get_type_as_dwim($stream);
64 }
65 6 ▶ return __PACKAGE__->error("Bad type: $type")
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "xml" or $type eq "json" or $type eq "hash" or $type eq "atom" 0 6 6
Condition88%8/9
$type eq "xml" or $type eq "json" or $type eq "hash" or $type eq "atom"
l!l&&r!l&&!r
510
$type eq "xml" or $type eq "json" or $type eq "hash"
l!l&&r!l&&!r
411
$type eq "xml" or $type eq "json"
l!l&&r!l&&!r
222
MC/DC0%0/4
$type eq "xml" or $type eq "json" or $type eq "hash" or $type eq "atom"
$type eq "xml" $type eq "json" $type eq "hash" $type eq "atom"
Truth table80%4/5
A or B or C or D
A= $type eq "xml"B= $type eq "json"C= $type eq "hash"D= $type eq "atom"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
66 unless $type eq 'xml' || $type eq 'json' || $type eq 'hash' || $type eq 'atom';
67 6 ▶ return __PACKAGE__->error("Bad stream: $type => $stream")
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "xml" and not $stream =~ /^</ or $type eq "json" and not $stream =~ /^\{/ or $type eq "hash" and not UNIVERSAL::isa($stream, "HASH") or $type eq "atom" and not UNIVERSAL::isa($stream, "XML::Atom::Base") 0 6 6
Condition52%11/21
$type eq "xml" and not $stream =~ /^</ or $type eq "json" and not $stream =~ /^\{/ or $type eq "hash" and not UNIVERSAL::isa($stream, "HASH") or $type eq "atom" and not UNIVERSAL::isa($stream, "XML::Atom::Base")
l!l&&r!l&&!r
006
$type eq "xml" and not $stream =~ /^</ or $type eq "json" and not $stream =~ /^\{/ or $type eq "hash" and not UNIVERSAL::isa($stream, "HASH")
l!l&&r!l&&!r
006
$type eq "xml" and not $stream =~ /^</ or $type eq "json" and not $stream =~ /^\{/
l!l&&r!l&&!r
006
$type eq "xml" and not $stream =~ /^</
!ll&&!rl&&r
420
$type eq "json" and not $stream =~ /^\{/
!ll&&!rl&&r
420
$type eq "hash" and not UNIVERSAL::isa($stream, "HASH")
!ll&&!rl&&r
510
$type eq "atom" and not UNIVERSAL::isa($stream, "XML::Atom::Base")
!ll&&!rl&&r
510
MC/DC0%0/8
$type eq "xml" and not $stream =~ /^</ or $type eq "json" and not $stream =~ /^\{/ or $type eq "hash" and not UNIVERSAL::isa($stream, "HASH") or $type eq "atom" and not UNIVERSAL::isa($stream, "XML::Atom::Base")
$type eq "xml" not $stream =~ /^</ $type eq "json" not $stream =~ /^\{/ $type eq "hash" not UNIVERSAL::isa($stream, "HASH") $type eq "atom" not UNIVERSAL::isa($stream, "XML::Atom::Base")
Truth table12%4/31
A and not(B) or C and not(D) or E and not(F) or G and not(H)
A= $type eq "xml"B= not $stream =~ /^</C= $type eq "json"D= not $stream =~ /^\{/E= $type eq "hash"F= not UNIVERSAL::isa($stream, "HASH")G= $type eq "atom"H= not UNIVERSAL::isa($stream, "XML::Atom::Base")
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X1011XX1
100X11XX1
101011XX1
0X0X0X111
0X0X10111
0X100X111
0X1010111
100X0X111
100X10111
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
68 if ($type eq 'xml' && $stream !~ /^</)
69 || ($type eq 'json' && $stream !~ /^\{/)
70 || ($type eq 'hash' && !UNIVERSAL::isa($stream, 'HASH'))
71 || ($type eq 'atom' && !UNIVERSAL::isa($stream, 'XML::Atom::Base'));
72 6 bless { $type => $stream }, $class;
73 }
74  
75 2 ▶sub get_type_as_dwim {
Subroutine get_type_as_dwim: called CC=5 SCAR=17.4
Pod documented
76 2 my ($stream) = @_;
77 2 ▶ return UNIVERSAL::isa($stream, 'XML::Atom::Base') ? 'atom'
Branch: 4 branches
BranchTrueFalseTotal
UNIVERSAL::isa($stream, 'XML::Atom::Base') ? : 0 2 2
UNIVERSAL::isa($stream, 'HASH') ? : 0 2 2
$stream =~ /^\{/ ? : 1 1 2
$stream =~ /^</ ? : 1 0 1
78 : UNIVERSAL::isa($stream, 'HASH') ? 'hash'
79 : $stream =~ /^\{/ ? 'json'
80 : $stream =~ /^</ ? 'xml'
81 : __PACKAGE__->error("Bad stream: $stream");
82 }
83  
84 0 ▶sub gdata :Export { __PACKAGE__->new(@_) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine gdata: not called CC=1 SCAR=0.2
Pod documented
85  
86 3 ▶sub as_xml {
Subroutine as_xml: called CC=5 SCAR=20.2
Pod documented
87 3 my $self = shift;
88 3 ▶ if ( $self->{xml} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'xml'}) { } 0 3 3
elsif ($self->{'atom'}) { } 1 2 3
elsif ($self->{'hash'}) { } 2 0 2
elsif ($self->{'json'}) { } 0 0 0
89 0 return $self->{xml};
90 }
91 elsif ( $self->{atom} ) {
92 1 return $self->{xml} = atom_to_xml( $self->{atom} );
93 }
94 elsif ( $self->{hash} ) {
95 2 return $self->{xml} = hash_to_xml( $self->{hash} );
96 }
97 elsif ( $self->{json} ) {
98 0 $self->{hash} = json_to_hash( $self->{json} );
99 0 return $self->{xml} = hash_to_xml( $self->{hash} );
100 }
101 }
102  
103 3 ▶sub as_atom {
Subroutine as_atom: called CC=5 SCAR=26.2
Pod documented
104 3 my $self = shift;
105 3 ▶ if ( $self->{atom} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'atom'}) { } 0 3 3
elsif ($self->{'xml'}) { } 3 0 3
elsif ($self->{'hash'}) { } 0 0 0
elsif ($self->{'json'}) { } 0 0 0
106 0 return $self->{atom};
107 }
108 elsif ( $self->{xml} ) {
109 3 return $self->{atom} = xml_to_atom( $self->{xml} );
110 }
111 elsif ( $self->{hash} ) {
112 0 $self->{xml} = hash_to_xml( $self->{hash} );
113 0 return $self->{atom} = xml_to_atom( $self->{xml} );
114 }
115 elsif ( $self->{json} ) {
116 0 $self->{hash} = json_to_hash( $self->{json} );
117 0 $self->{xml} = hash_to_xml( $self->{hash} );
118 0 return $self->{atom} = xml_to_atom( $self->{xml} );
119 }
120 }
121  
122 3 ▶sub as_hash {
Subroutine as_hash: called CC=5 SCAR=20.2
Pod documented
123 3 my $self = shift;
124 3 ▶ if ( $self->{hash} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'hash'}) { } 0 3 3
elsif ($self->{'json'}) { } 1 2 3
elsif ($self->{'xml'}) { } 2 0 2
elsif ($self->{'atom'}) { } 0 0 0
125 0 return $self->{hash};
126 }
127 elsif ( $self->{json} ) {
128 1 return $self->{hash} = json_to_hash( $self->{json} );
129 }
130 elsif ( $self->{xml} ) {
131 2 return $self->{hash} = xml_to_hash( $self->{xml} );
132 }
133 elsif ( $self->{atom} ) {
134 0 $self->{xml} = atom_to_xml( $self->{atom} );
135 0 return $self->{hash} = xml_to_hash( $self->{xml} );
136 }
137 }
138  
139 3 ▶sub as_json {
Subroutine as_json: called CC=5 SCAR=26.2
Pod documented
140 3 my $self = shift;
141 3 ▶ if ( $self->{json} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'json'}) { } 0 3 3
elsif ($self->{'hash'}) { } 3 0 3
elsif ($self->{'xml'}) { } 0 0 0
elsif ($self->{'atom'}) { } 0 0 0
142 0 return $self->{json};
143 }
144 elsif ( $self->{hash} ) {
145 3 return $self->{json} = hash_to_json( $self->{hash} );
146 }
147 elsif ( $self->{xml} ) {
148 0 $self->{hash} = xml_to_hash( $self->{xml} );
149 0 return $self->{json} = hash_to_json( $self->{hash} );
150 }
151 elsif ( $self->{atom} ) {
152 0 $self->{xml} = atom_to_xml( $self->{atom} );
153 0 $self->{hash} = xml_to_hash( $self->{xml} );
154 0 return $self->{json} = hash_to_json( $self->{hash} );
155 }
156 }
157  
158 3 ▶sub xml_to_atom :Export {
Subroutine xml_to_atom: called CC=2 SCAR=7.0
Pod documented
159 3 my ($xml) = shift;
160 3 my ($root) = $xml =~ /<\?xml[^>]+?\?>\s*<(?:\w+:)?(\w+)/ms;
161 3 my $module = 'XML::Atom::' . ucfirst($root);
162 3 ▶ "$module"->require or return __PACKAGE__->error($@);
Branch: 1 branch
BranchTrueFalseTotal
"$module"->require or return __PACKAGE__->error($@) 3 0 3
163 3 return $module->new(\$xml);
164 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166 3 ▶sub xml_to_hash :Export { fix_ns( XMLin( $_[0], %{ $CONFIG->{XMLin} } ) ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine xml_to_hash: called CC=1 SCAR=0.0
Pod documented
167  
168 0 ▶sub xml_to_json :Export { hash_to_json( xml_to_hash(@_) ) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine xml_to_json: not called CC=1 SCAR=0.2
Pod documented
169  
170 3 ▶sub atom_to_xml :Export { $_[0]->as_xml }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine atom_to_xml: called CC=1 SCAR=0.0
Pod documented
171  
172 0 ▶sub atom_to_hash :Export { xml_to_hash( atom_to_xml(@_) ) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine atom_to_hash: not called CC=1 SCAR=0.2
Pod documented
173  
174 0 ▶sub atom_to_json :Export { xml_to_json( atom_to_xml(@_) ) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine atom_to_json: not called CC=1 SCAR=0.2
Pod documented
175  
176 3 ▶sub hash_to_xml :Export { XMLout( fix_ns2(dclone $_[0]), %{ $CONFIG->{XMLout} } ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine hash_to_xml: called CC=1 SCAR=0.0
Pod documented
177  
178 0 ▶sub hash_to_atom :Export { xml_to_atom( hash_to_xml(@_) ) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine hash_to_atom: not called CC=1 SCAR=0.2
Pod documented
179  
180 3 ▶sub hash_to_json :Export { JSON::Any->objToJson( $_[0] ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine hash_to_json: called CC=1 SCAR=0.0
Pod documented
181  
182 0 ▶sub json_to_xml :Export { hash_to_xml( json_to_hash(@_) ) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine json_to_xml: not called CC=1 SCAR=0.2
Pod documented
183  
184 0 ▶sub json_to_atom :Export { hash_to_atom( json_to_hash(@_) ) }
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine json_to_atom: not called CC=1 SCAR=0.2
Pod documented
185  
186 3 ▶sub json_to_hash :Export { JSON::Any->jsonToObj( $_[0] ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine json_to_hash: called CC=1 SCAR=0.0
Pod documented
187  
188 0 ▶sub as_hashref {
Subroutine as_hashref: not called CC=1 SCAR=6.9
Pod documented
189 0 warn 'as_hashref is DEPRECATED and renamed to as_hash';
190 0 $_[0]->as_hash;
191 }
192  
193 0 ▶sub xml_to_hashref :Export {
Subroutine xml_to_hashref: not called CC=1 SCAR=6.9
Pod documented
194 0 warn 'xml_to_hashref is DEPRECATED and renamed to xml_to_hash';
195 0 xml_to_hash(@_);
196 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198 0 ▶sub atom_to_hashref :Export {
Subroutine atom_to_hashref: not called CC=1 SCAR=6.9
Pod documented
199 0 warn 'xml_to_hashref is DEPRECATED and renamed to atom_to_hash';
200 0 atom_to_hash(@_);
201 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
202  
203 0 ▶sub json_to_hashref :Export {
Subroutine json_to_hashref: not called CC=1 SCAR=6.9
Pod documented
204 0 warn 'xml_to_hashref is DEPRECATED and renamed to json_to_hash';
205 0 json_to_hash(@_);
206 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
207  
208 66 ▶sub fix_ns {
Subroutine fix_ns: called CC=5 SCAR=16.1
Pod documented
209 66 my ($h) = shift;
210 66 for my $k (keys %$h) {
211 126 ▶ if (UNIVERSAL::isa($h->{$k}, 'HASH')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($h->{$k}, 'HASH')) { } 52 74 126
elsif (UNIVERSAL::isa($h->{$k}, 'ARRAY')) { } 6 68 74
212 52 $h->{$k} = fix_ns($h->{$k});
213 }
214 elsif (UNIVERSAL::isa($h->{$k}, 'ARRAY')) {
215 6 $h->{$k} = [ map fix_ns($_), @{ $h->{$k} } ];
216 }
217 126 ▶ if ($k =~ /(.+):(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /(.+):(.+)/) 4 122 126
218 4 $h->{"$1\$$2"} = $h->{$k};
219 4 delete $h->{$k};
220 }
221 }
222 66 return $h;
223 }
224  
225 66 ▶sub fix_ns2 {
Subroutine fix_ns2: called CC=5 SCAR=16.1
Pod documented
226 66 my ($h) = shift;
227 66 for my $k (keys %$h) {
228 126 ▶ if (UNIVERSAL::isa($h->{$k}, 'HASH')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($h->{$k}, 'HASH')) { } 52 74 126
elsif (UNIVERSAL::isa($h->{$k}, 'ARRAY')) { } 6 68 74
229 52 $h->{$k} = fix_ns2($h->{$k});
230 }
231 elsif (UNIVERSAL::isa($h->{$k}, 'ARRAY')) {
232 6 $h->{$k} = [ map fix_ns2($_), @{ $h->{$k} } ];
233 }
234 126 ▶ if ($k =~ /(.+)\$(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /(.+)\$(.+)/) 4 122 126
235 4 $h->{"$1:$2"} = $h->{$k};
236 4 delete $h->{$k};
237 }
238 }
239 66 return $h;
240 }
241  
242 3 1; # Magic true value required at end of module
243 __END__
↑ Summary