Parser/JSON.pm

filter: Statementstmt 100% 35 / 35 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 90.0% 54 / 60 CC 9 SCAR 12.2
CC
9
Cov
94%
_encode
6.0
finalize
3.0
add
2.0
CRAP 3.4
« Parser/MultiPart.pm ↑ Summary Parser/UrlEncoded.pm »
1 package HTTP::Entity::Parser::JSON;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use JSON::MaybeXS qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Encode qw/encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 1 bless [''], $_[0];
10 }
11  
12 7 ▶sub add {
Subroutine add: called CC=2 SCAR=7.1
Pod undocumented
13 7 my $self = shift;
14 7 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 7 0 7
15 7 $self->[0] .= $_[0];
16 }
17 }
18  
19 1 ▶sub finalize {
Subroutine finalize: called CC=3 SCAR=11.0
Pod undocumented
20 1 my $self = shift;
21  
22 1 my $p = decode_json($self->[0]);
23 1 my @params;
24 1 ▶ if (ref $p eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq "HASH") 1 0 1
25 1 ▶ while (my ($k, $v) = each %$p) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$p) 5 1 6
26 5 push @params, _encode($k), _encode($v);
27 }
28 }
29 1 return (\@params, []);
30 }
31  
32 20 ▶sub _encode {
Subroutine _encode: called CC=6 SCAR=17.9
33 20 my ($data) = @_;
34  
35 20 ▶ if (ref $data eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 2 18 20
elsif (ref $data eq 'HASH') { } 3 15 18
36 2 my @result;
37 2 for my $d (@$data) {
38 4 push @result, _encode($d);
39 }
40 2 return \@result;
41 }
42 elsif (ref $data eq "HASH") {
43 3 my %result;
44 3 ▶ while (my ($k, $v) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 3 3 6
45 3 $result{_encode($k)} = _encode($v);
46 }
47 3 return \%result;
48 }
49  
50 15 ▶ return defined $data ? encode_utf8($data) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 15 0 15
51 }
52  
53 5 1;
54  
55 __END__
« Parser/MultiPart.pm ↑ Summary Parser/UrlEncoded.pm »