PP.pm

filter: Statementstmt 86.6% 871 / 1005 Branchbran 74.4% 509 / 684 Conditioncond 67.9% 193 / 284 MC/DCmcdc 49.4% 85 / 172 Subroutinesub 77.0% 124 / 161 Podpod 48.9% 45 / 92 totaltotal 76.1% 1827 / 2398 CC 445 SCAR 29.3
CC
445
Cov
80%
white
112.6
_incr_parse
61.1
number
32.1
CRAP 18.7
↑ Summary PP/Boolean.pm »
1 package JSON::PP;
2  
3 # JSON-2.0
4  
5 47 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 47 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶BEGIN { @JSON::PP::ISA = ('Exporter') }
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 47 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 47 ▶use JSON::PP::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 47 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 #use Devel::Peek;
16  
17 47 $JSON::PP::VERSION = '3.99_01';
18  
19 47 @JSON::PP::EXPORT = qw(encode_json decode_json from_json to_json);
20  
21 # instead of hash-access, i tried index-access for speed.
22 # but this method is not faster than what i expected. so it will be changed.
23  
24 47 ▶use constant P_ASCII => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 47 ▶use constant P_LATIN1 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 47 ▶use constant P_UTF8 => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 47 ▶use constant P_INDENT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 47 ▶use constant P_CANONICAL => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 47 ▶use constant P_SPACE_BEFORE => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 47 ▶use constant P_SPACE_AFTER => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 47 ▶use constant P_ALLOW_NONREF => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 47 ▶use constant P_SHRINK => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 47 ▶use constant P_ALLOW_BLESSED => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 47 ▶use constant P_CONVERT_BLESSED => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 47 ▶use constant P_RELAXED => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 47 ▶use constant P_LOOSE => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 47 ▶use constant P_ALLOW_BIGNUM => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 47 ▶use constant P_ALLOW_BAREKEY => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 47 ▶use constant P_ALLOW_SINGLEQUOTE => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 47 ▶use constant P_ESCAPE_SLASH => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 47 ▶use constant P_AS_NONBLESSED => 17;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 47 ▶use constant P_ALLOW_UNKNOWN => 18;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 47 ▶use constant P_ALLOW_TAGS => 19;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 47 ▶use constant OLD_PERL => $] < 5.008 ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.008 ? : 0 47 47
48 47 ▶use constant USE_B => $ENV{PERL_JSON_PP_USE_B} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'PERL_JSON_PP_USE_B'} || 0
l!l
047
MC/DC0%0/1
$ENV{'PERL_JSON_PP_USE_B'} || 0
$ENV{'PERL_JSON_PP_USE_B'}
Truth table50%1/2
A
A= $ENV{'PERL_JSON_PP_USE_B'}
Aresult
11
00
49  
50 BEGIN {
51 47 ▶ if (USE_B) {
Subroutine BEGIN: called CC=1 SCAR=0.0
52 require B;
53 }
54 }
55  
56 BEGIN {
57 47 ▶ my @xs_compati_bit_properties = qw(
Subroutine BEGIN: called CC=2 SCAR=8.9
58 latin1 ascii utf8 indent canonical space_before space_after allow_nonref shrink
59 allow_blessed convert_blessed relaxed allow_unknown
60 allow_tags
61 );
62 47 my @pp_bit_properties = qw(
63 allow_singlequote allow_bignum loose
64 allow_barekey escape_slash as_nonblessed
65 );
66  
67 # Perl version check, Unicode handling is enabled?
68 # Helper module sets @JSON::PP::_properties.
69 47 if ( OLD_PERL ) {
70 my $helper = $] >= 5.006 ? 'JSON::PP::Compat5006' : 'JSON::PP::Compat5005';
71 eval qq| require $helper |;
72 if ($@) { Carp::croak $@; }
73 }
74  
75 47 for my $name (@xs_compati_bit_properties, @pp_bit_properties) {
76 940 my $property_id = 'P_' . uc($name);
77  
78 940 ▶ eval qq/
Subroutine allow_barekey: called CC=3 SCAR=13.9
Subroutine allow_bignum: called CC=3 SCAR=13.9
Subroutine allow_blessed: called CC=3 SCAR=13.9
Subroutine allow_nonref: called CC=3 SCAR=13.9
Subroutine allow_singlequote: called CC=3 SCAR=13.9
Subroutine allow_tags: called CC=3 SCAR=13.9
Subroutine allow_unknown: called CC=3 SCAR=13.9
Subroutine as_nonblessed: not called CC=3 SCAR=13.9
Subroutine ascii: called CC=3 SCAR=13.9
Subroutine canonical: called CC=3 SCAR=13.9
Subroutine convert_blessed: called CC=3 SCAR=13.9
Subroutine escape_slash: called CC=3 SCAR=13.9
Subroutine get_allow_barekey: not called CC=2 SCAR=9.0
Subroutine get_allow_bignum: not called CC=2 SCAR=9.0
Subroutine get_allow_blessed: not called CC=2 SCAR=9.0
Subroutine get_allow_nonref: not called CC=2 SCAR=9.0
Subroutine get_allow_singlequote: not called CC=2 SCAR=9.0
Subroutine get_allow_tags: not called CC=2 SCAR=9.0
Subroutine get_allow_unknown: not called CC=2 SCAR=9.0
Subroutine get_as_nonblessed: not called CC=2 SCAR=9.0
Subroutine get_ascii: not called CC=2 SCAR=9.0
Subroutine get_canonical: not called CC=2 SCAR=9.0
Subroutine get_convert_blessed: not called CC=2 SCAR=9.0
Subroutine get_escape_slash: not called CC=2 SCAR=9.0
Subroutine get_indent: not called CC=2 SCAR=9.0
Subroutine get_latin1: not called CC=2 SCAR=9.0
Subroutine get_loose: not called CC=2 SCAR=9.0
Subroutine get_relaxed: not called CC=2 SCAR=9.0
Subroutine get_shrink: not called CC=2 SCAR=9.0
Subroutine get_space_after: not called CC=2 SCAR=9.0
Subroutine get_space_before: not called CC=2 SCAR=9.0
Subroutine get_utf8: not called CC=2 SCAR=9.0
Subroutine indent: called CC=3 SCAR=13.9
Subroutine latin1: called CC=3 SCAR=13.9
Subroutine loose: not called CC=3 SCAR=13.9
Subroutine relaxed: called CC=3 SCAR=13.9
Subroutine shrink: called CC=3 SCAR=13.9
Subroutine space_after: called CC=3 SCAR=13.9
Subroutine space_before: called CC=3 SCAR=13.9
Subroutine utf8: called CC=3 SCAR=13.9
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod undocumented
Pod documented
Pod documented
Pod documented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 60 branches
BranchTrueFalseTotal
defined $_[1] ? : 0 2 2
if ($enable) { } 2 0 2
defined $_[1] ? : 1 0 1
if ($enable) { } 1 0 1
defined $_[1] ? : 0 2 2
if ($enable) { } 2 0 2
defined $_[1] ? : 24625 20 24645
if ($enable) { } 9279 15366 24645
defined $_[1] ? : 0 1 1
if ($enable) { } 1 0 1
defined $_[1] ? : 0 1 1
if ($enable) { } 1 0 1
defined $_[1] ? : 2 1 3
if ($enable) { } 2 1 3
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 2 12290 12292
if ($enable) { } 12292 0 12292
defined $_[1] ? : 3 7 10
if ($enable) { } 10 0 10
defined $_[1] ? : 0 4 4
if ($enable) { } 4 0 4
defined $_[1] ? : 0 1 1
if ($enable) { } 1 0 1
$_[0]{'PROPS'}[14] ? : 0 0 0
$_[0]{'PROPS'}[13] ? : 0 0 0
$_[0]{'PROPS'}[9] ? : 0 0 0
$_[0]{'PROPS'}[7] ? : 0 0 0
$_[0]{'PROPS'}[15] ? : 0 0 0
$_[0]{'PROPS'}[19] ? : 0 0 0
$_[0]{'PROPS'}[18] ? : 0 0 0
$_[0]{'PROPS'}[17] ? : 0 0 0
$_[0]{'PROPS'}[0] ? : 0 0 0
$_[0]{'PROPS'}[4] ? : 0 0 0
$_[0]{'PROPS'}[10] ? : 0 0 0
$_[0]{'PROPS'}[16] ? : 0 0 0
$_[0]{'PROPS'}[3] ? : 0 0 0
$_[0]{'PROPS'}[1] ? : 0 0 0
$_[0]{'PROPS'}[12] ? : 0 0 0
$_[0]{'PROPS'}[11] ? : 0 0 0
$_[0]{'PROPS'}[8] ? : 0 0 0
$_[0]{'PROPS'}[6] ? : 0 0 0
$_[0]{'PROPS'}[5] ? : 0 0 0
$_[0]{'PROPS'}[2] ? : 0 0 0
defined $_[1] ? : 6 0 6
if ($enable) { } 4 2 6
defined $_[1] ? : 0 2 2
if ($enable) { } 2 0 2
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 14 14
if ($enable) { } 14 0 14
defined $_[1] ? : 0 21504 21504
if ($enable) { } 21504 0 21504
defined $_[1] ? : 7 0 7
if ($enable) { } 4 3 7
defined $_[1] ? : 6 0 6
if ($enable) { } 4 2 6
defined $_[1] ? : 4 18483 18487
if ($enable) { } 18487 0 18487
79 sub $name {
80 my \$enable = defined \$_[1] ? \$_[1] : 1;
81  
82 if (\$enable) {
83 \$_[0]->{PROPS}->[$property_id] = 1;
84 }
85 else {
86 \$_[0]->{PROPS}->[$property_id] = 0;
87 }
88  
89 \$_[0];
90 }
91  
92 sub get_$name {
93 \$_[0]->{PROPS}->[$property_id] ? 1 : '';
94 }
95 /;
96 }
97  
98 }
99  
100  
101  
102 # Functions
103  
104 47 my $JSON; # cache
105  
106 sub encode_json ($) { # encode
107 186 ▶ ($JSON ||= __PACKAGE__->new->utf8)->encode(@_);
Subroutine encode_json: called CC=2 SCAR=9.2
Pod documented
Condition33%1/3
$JSON ||= __PACKAGE__->new->utf8
l!l&&r!l&&!r
18600
MC/DC0%0/2
$JSON ||= __PACKAGE__->new->utf8
$JSON __PACKAGE__->new->utf8
Truth table33%1/3
A ||= B
A= $JSONB= __PACKAGE__->new->utf8
ABresult
1X1
011
000
108 }
109  
110  
111 sub decode_json { # decode
112 6253 ▶ ($JSON ||= __PACKAGE__->new->utf8)->decode(@_);
Subroutine decode_json: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$JSON ||= __PACKAGE__->new->utf8
l!l&&r!l&&!r
6206470
MC/DC0%0/2
$JSON ||= __PACKAGE__->new->utf8
$JSON __PACKAGE__->new->utf8
Truth table66%2/3
A ||= B
A= $JSONB= __PACKAGE__->new->utf8
ABresult
1X1
011
000
113 }
114  
115 # Obsoleted
116  
117 sub to_json($) {
118 0 ▶ Carp::croak ("JSON::PP::to_json has been renamed to encode_json.");
Subroutine to_json: not called CC=1 SCAR=6.9
Pod undocumented
119 }
120  
121  
122 sub from_json($) {
123 0 ▶ Carp::croak ("JSON::PP::from_json has been renamed to decode_json.");
Subroutine from_json: not called CC=1 SCAR=6.9
Pod undocumented
124 }
125  
126  
127 # Methods
128  
129 sub new {
130 43169 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
131 43169 my $self = {
132 max_depth => 512,
133 max_size => 0,
134 indent_length => 3,
135 };
136  
137 43169 $self->{PROPS}[P_ALLOW_NONREF] = 1;
138  
139 43169 bless $self, $class;
140 }
141  
142  
143 sub encode {
144 25141 ▶ return $_[0]->PP_encode_json($_[1]);
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
145 }
146  
147  
148 sub decode {
149 24996 ▶ return $_[0]->PP_decode_json($_[1], 0x00000000);
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
150 }
151  
152  
153 sub decode_prefix {
154 8 ▶ return $_[0]->PP_decode_json($_[1], 0x00000001);
Subroutine decode_prefix: called CC=1 SCAR=0.0
Pod documented
155 }
156  
157  
158 # accessor
159  
160  
161 # pretty printing
162  
163 sub pretty {
164 5 ▶ my ($self, $v) = @_;
Subroutine pretty: called CC=3 SCAR=11.0
Pod documented
165 5 ▶ my $enable = defined $v ? $v : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 5 0 5
166  
167 5 ▶ if ($enable) { # indent_length(3) for JSON::XS compatibility
Branch: 1 branch
BranchTrueFalseTotal
if ($enable) { } 3 2 5
168 3 $self->indent(1)->space_before(1)->space_after(1);
169 }
170 else {
171 2 $self->indent(0)->space_before(0)->space_after(0);
172 }
173  
174 5 $self;
175 }
176  
177 # etc
178  
179 sub max_depth {
180 5 ▶ my $max = defined $_[1] ? $_[1] : 0x80000000;
Subroutine max_depth: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 5 0 5
181 5 $_[0]->{max_depth} = $max;
182 5 $_[0];
183 }
184  
185  
186 357 ▶sub get_max_depth { $_[0]->{max_depth}; }
Subroutine get_max_depth: called CC=1 SCAR=0.0
Pod undocumented
187  
188  
189 sub max_size {
190 3 ▶ my $max = defined $_[1] ? $_[1] : 0;
Subroutine max_size: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 3 0 3
191 3 $_[0]->{max_size} = $max;
192 3 $_[0];
193 }
194  
195  
196 353 ▶sub get_max_size { $_[0]->{max_size}; }
Subroutine get_max_size: called CC=1 SCAR=0.0
Pod undocumented
197  
198 sub boolean_values {
199 4 ▶ my $self = shift;
Subroutine boolean_values: called CC=2 SCAR=6.9
Pod documented
200 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 2 4
201 2 my ($false, $true) = @_;
202 2 $self->{false} = $false;
203 2 $self->{true} = $true;
204 2 return ($false, $true);
205 } else {
206 2 delete $self->{false};
207 2 delete $self->{true};
208 2 return;
209 }
210 }
211  
212 sub get_boolean_values {
213 4 ▶ my $self = shift;
Subroutine get_boolean_values: called CC=3 SCAR=11.0
Pod undocumented
214 4 ▶ if (exists $self->{true} and exists $self->{false}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"true"} and exists $self->{"false"}) 2 2 4
Condition66%2/3
exists $self->{"true"} and exists $self->{"false"}
!ll&&!rl&&r
202
MC/DC50%1/2
exists $self->{"true"} and exists $self->{"false"}
exists $self->{"true"} exists $self->{"false"}
Truth table66%2/3
A and B
A= exists $self->{"true"}B= exists $self->{"false"}
ABresult
0X0
100
111
215 2 return @$self{qw/false true/};
216 }
217 2 return;
218 }
219  
220 sub filter_json_object {
221 3 ▶ if (defined $_[1] and ref $_[1] eq 'CODE') {
Subroutine filter_json_object: called CC=5 SCAR=16.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and ref $_[1] eq 'CODE') { } 2 1 3
Condition66%2/3
defined $_[1] and ref $_[1] eq "CODE"
!ll&&!rl&&r
102
MC/DC50%1/2
defined $_[1] and ref $_[1] eq "CODE"
defined $_[1] ref $_[1] eq "CODE"
Truth table66%2/3
A and B
A= defined $_[1]B= ref $_[1] eq "CODE"
ABresult
0X0
100
111
222 2 $_[0]->{cb_object} = $_[1];
223 } else {
224 1 delete $_[0]->{cb_object};
225 }
226 3 ▶ $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'cb_object'} || $_[0]{'cb_sk_object'} ? : 3 0 3
Condition66%2/3
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
l!l&&r!l&&!r
210
MC/DC0%0/2
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
$_[0]{"cb_object"} $_[0]{"cb_sk_object"}
Truth table66%2/3
A or B
A= $_[0]{"cb_object"}B= $_[0]{"cb_sk_object"}
ABresult
1X1
011
000
227 3 $_[0];
228 }
229  
230 sub filter_json_single_key_object {
231 4 ▶ if (@_ == 1 or @_ > 3) {
Subroutine filter_json_single_key_object: called CC=9 SCAR=25.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 or @_ > 3) 0 4 4
Condition33%1/3
@_ == 1 or @_ > 3
l!l&&r!l&&!r
004
MC/DC0%0/2
@_ == 1 or @_ > 3
@_ == 1 @_ > 3
Truth table33%1/3
A or B
A= @_ == 1B= @_ > 3
ABresult
1X1
011
000
232 0 Carp::croak("Usage: JSON::PP::filter_json_single_key_object(self, key, callback = undef)");
233 }
234 4 ▶ if (defined $_[2] and ref $_[2] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2] and ref $_[2] eq 'CODE') { } 3 1 4
Condition66%2/3
defined $_[2] and ref $_[2] eq "CODE"
!ll&&!rl&&r
103
MC/DC50%1/2
defined $_[2] and ref $_[2] eq "CODE"
defined $_[2] ref $_[2] eq "CODE"
Truth table66%2/3
A and B
A= defined $_[2]B= ref $_[2] eq "CODE"
ABresult
0X0
100
111
235 3 $_[0]->{cb_sk_object}->{$_[1]} = $_[2];
236 } else {
237 1 delete $_[0]->{cb_sk_object}->{$_[1]};
238 1 ▶ delete $_[0]->{cb_sk_object} unless %{$_[0]->{cb_sk_object} || {}};
Branch: 2 branches
BranchTrueFalseTotal
unless %{$_[0]{"cb_sk_object"} or {};} 0 1 1
$_[0]{"cb_sk_object"} or {} 0 1 1
239 }
240 4 ▶ $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'cb_object'} || $_[0]{'cb_sk_object'} ? : 4 0 4
Condition33%1/3
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
$_[0]{"cb_object"} $_[0]{"cb_sk_object"}
Truth table33%1/3
A or B
A= $_[0]{"cb_object"}B= $_[0]{"cb_sk_object"}
ABresult
1X1
011
000
241 4 $_[0];
242 }
243  
244 sub indent_length {
245 0 ▶ if (!defined $_[1] or $_[1] > 15 or $_[1] < 0) {
Subroutine indent_length: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[1] or $_[1] > 15 or $_[1] < 0) { } 0 0 0
Condition0%0/6
not defined $_[1] or $_[1] > 15 or $_[1] < 0
l!l&&r!l&&!r
000
not defined $_[1] or $_[1] > 15
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $_[1] or $_[1] > 15 or $_[1] < 0
not defined $_[1] $_[1] > 15 $_[1] < 0
Truth table0%0/4
not(A) or B or C
A= not defined $_[1]B= $_[1] > 15C= $_[1] < 0
ABCresult
1XX1
01X1
0011
0000
246 0 Carp::carp "The acceptable range of indent_length() is 0 to 15.";
247 }
248 else {
249 0 $_[0]->{indent_length} = $_[1];
250 }
251 0 $_[0];
252 }
253  
254 sub get_indent_length {
255 0 ▶ $_[0]->{indent_length};
Subroutine get_indent_length: not called CC=1 SCAR=6.9
Pod undocumented
256 }
257  
258 sub sort_by {
259 3 ▶ $_[0]->{sort_by} = defined $_[1] ? $_[1] : 1;
Subroutine sort_by: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 3 0 3
260 3 $_[0];
261 }
262  
263 sub allow_bigint {
264 0 ▶ Carp::carp("allow_bigint() is obsoleted. use allow_bignum() instead.");
Subroutine allow_bigint: not called CC=1 SCAR=6.9
Pod undocumented
265 0 $_[0]->allow_bignum;
266 }
267  
268 ###############################
269  
270 ###
271 ### Perl => JSON
272 ###
273  
274  
275 { # Convert
276  
277 47 my $max_depth;
278 my $indent;
279 47 my $ascii;
280 47 my $latin1;
281 47 my $utf8;
282 47 my $space_before;
283 47 my $space_after;
284 47 my $canonical;
285 47 my $allow_blessed;
286 47 my $convert_blessed;
287  
288 47 my $indent_length;
289 47 my $escape_slash;
290 47 my $bignum;
291 47 my $as_nonblessed;
292 47 my $allow_tags;
293  
294 47 my $depth;
295 47 my $indent_count;
296 47 my $keysort;
297  
298  
299 sub PP_encode_json {
300 25141 ▶ my $self = shift;
Subroutine PP_encode_json: called CC=12 SCAR=24.9
Pod undocumented
301 25141 my $obj = shift;
302  
303 25141 $indent_count = 0;
304 25141 $depth = 0;
305  
306 25141 my $props = $self->{PROPS};
307  
308 ($ascii, $latin1, $utf8, $indent, $canonical, $space_before, $space_after, $allow_blessed,
309 $convert_blessed, $escape_slash, $bignum, $as_nonblessed, $allow_tags)
310 25141 = @{$props}[P_ASCII .. P_SPACE_AFTER, P_ALLOW_BLESSED, P_CONVERT_BLESSED,
311 P_ESCAPE_SLASH, P_ALLOW_BIGNUM, P_AS_NONBLESSED, P_ALLOW_TAGS];
312  
313 25141 ($max_depth, $indent_length) = @{$self}{qw/max_depth indent_length/};
314  
315 25141 ▶ $keysort = $canonical ? sub { $a cmp $b } : undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$canonical ? : 268 24873 25141
316  
317 25141 ▶ if ($self->{sort_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sort_by"}) 3 25138 25141
318 $keysort = ref($self->{sort_by}) eq 'CODE' ? $self->{sort_by}
319 : $self->{sort_by} =~ /\D+/ ? $self->{sort_by}
320 3 ▶ : sub { $a cmp $b };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
ref $self->{'sort_by'} eq 'CODE' ? : 1 2 3
$self->{'sort_by'} =~ /\D+/ ? : 1 1 2
321 }
322  
323 25141 ▶ encode_error("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj or $props->[7] 0 25141 25141
Condition66%2/3
ref $obj or $props->[7]
l!l&&r!l&&!r
25063780
MC/DC0%0/2
ref $obj or $props->[7]
ref $obj $props->[7]
Truth table66%2/3
A or B
A= ref $objB= $props->[7]
ABresult
1X1
011
000
324 if(!ref $obj and !$props->[ P_ALLOW_NONREF ]);
325  
326 25141 my $str = $self->object_to_json($obj);
327  
328 25123 ▶ $str .= "\n" if ( $indent ); # JSON::XS 2.26 compatible
Branch: 1 branch
BranchTrueFalseTotal
if $indent 7 25116 25123
329  
330 25123 ▶ unless ($ascii or $latin1 or $utf8) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ascii or $latin1 or $utf8) 6483 18640 25123
Condition100%6/6
$ascii or $latin1 or $utf8
l!l&&r!l&&!r
1229963416483
$ascii or $latin1
l!l&&r!l&&!r
12297212824
MC/DC100%3/3
$ascii or $latin1 or $utf8
$ascii $latin1 $utf8
Truth table100%4/4
A or B or C
A= $asciiB= $latin1C= $utf8
ABCresult
1XX1
01X1
0011
0000
331 6483 utf8::upgrade($str);
332 }
333  
334 25123 ▶ if ($props->[ P_SHRINK ]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->[8]) 9216 15907 25123
335 9216 utf8::downgrade($str, 1);
336 }
337  
338 25123 return $str;
339 }
340  
341  
342 sub object_to_json {
343 25491 ▶ my ($self, $obj) = @_;
Subroutine object_to_json: called CC=21 SCAR=31.3
Pod undocumented
344 25491 my $type = ref($obj);
345  
346 25491 ▶ if($type eq 'HASH'){
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 346 25145 25491
elsif ($type eq 'ARRAY') { } 25010 135 25145
elsif ($type) { } 54 81 135
347 346 return $self->hash_to_json($obj);
348 }
349 elsif($type eq 'ARRAY'){
350 25010 return $self->array_to_json($obj);
351 }
352 elsif ($type) { # blessed object?
353 54 ▶ if (blessed($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($obj)) { } 34 20 54
354  
355 34 ▶ return $self->value_to_json($obj) if ( $obj->isa('JSON::PP::Boolean') );
Branch: 1 branch
BranchTrueFalseTotal
if $obj->isa("JSON::PP::Boolean") 20 14 34
356  
357 14 ▶ if ( $allow_tags and $obj->can('FREEZE') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_tags and $obj->can("FREEZE")) 1 13 14
Condition100%3/3
$allow_tags and $obj->can("FREEZE")
!ll&&!rl&&r
1211
MC/DC100%2/2
$allow_tags and $obj->can("FREEZE")
$allow_tags $obj->can("FREEZE")
Truth table100%3/3
A and B
A= $allow_tagsB= $obj->can("FREEZE")
ABresult
0X0
100
111
358 1 ▶ my $obj_class = ref $obj || $obj;
Condition33%1/3
ref $obj || $obj
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table33%1/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
359 1 $obj = bless $obj, $obj_class;
360 1 my @results = $obj->FREEZE('JSON');
361 1 ▶ if ( @results and ref $results[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@results and ref $results[0]) 0 1 1
Condition33%1/3
@results and ref $results[0]
!ll&&!rl&&r
010
MC/DC0%0/2
@results and ref $results[0]
@results ref $results[0]
Truth table33%1/3
A and B
A= @resultsB= ref $results[0]
ABresult
0X0
100
111
362 0 ▶ if ( refaddr( $obj ) eq refaddr( $results[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&refaddr($obj) eq &refaddr($results[0])) 0 0 0
363 0 encode_error( sprintf(
364 "%s::FREEZE method returned same object as was passed instead of a new one",
365 ref $obj
366 ) );
367 }
368 }
369 1 return '("'.$obj_class.'")['.join(',', @results).']';
370 }
371  
372 13 ▶ if ( $convert_blessed and $obj->can('TO_JSON') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert_blessed and $obj->can("TO_JSON")) 5 8 13
Condition100%3/3
$convert_blessed and $obj->can("TO_JSON")
!ll&&!rl&&r
445
MC/DC100%2/2
$convert_blessed and $obj->can("TO_JSON")
$convert_blessed $obj->can("TO_JSON")
Truth table100%3/3
A and B
A= $convert_blessedB= $obj->can("TO_JSON")
ABresult
0X0
100
111
373 5 my $result = $obj->TO_JSON();
374 5 ▶ if ( defined $result and ref( $result ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and ref $result) 2 3 5
Condition66%2/3
defined $result and ref $result
!ll&&!rl&&r
032
MC/DC50%1/2
defined $result and ref $result
defined $result ref $result
Truth table66%2/3
A and B
A= defined $resultB= ref $result
ABresult
0X0
100
111
375 2 ▶ if ( refaddr( $obj ) eq refaddr( $result ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&refaddr($obj) eq &refaddr($result)) 1 1 2
376 1 encode_error( sprintf(
377 "%s::TO_JSON method returned same object as was passed instead of a new one",
378 ref $obj
379 ) );
380 }
381 }
382  
383 4 return $self->object_to_json( $result );
384 }
385  
386 8 ▶ return "$obj" if ( $bignum and _is_bignum($obj) );
Branch: 1 branch
BranchTrueFalseTotal
if $bignum and _is_bignum($obj) 3 5 8
Condition66%2/3
$bignum and _is_bignum($obj)
!ll&&!rl&&r
503
MC/DC50%1/2
$bignum and _is_bignum($obj)
$bignum _is_bignum($obj)
Truth table66%2/3
A and B
A= $bignumB= _is_bignum($obj)
ABresult
0X0
100
111
387  
388 5 ▶ if ($allow_blessed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_blessed) 3 2 5
389 3 ▶ return $self->blessed_to_json($obj) if ($as_nonblessed); # will be removed.
Branch: 1 branch
BranchTrueFalseTotal
if $as_nonblessed 0 3 3
390 3 return 'null';
391 }
392 2 encode_error( sprintf("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", $obj)
393 );
394 }
395 else {
396 20 return $self->value_to_json($obj);
397 }
398 }
399 else{
400 81 return $self->value_to_json($obj);
401 }
402 }
403  
404  
405 sub hash_to_json {
406 346 ▶ my ($self, $obj) = @_;
Subroutine hash_to_json: called CC=9 SCAR=22.0
Pod undocumented
407 346 my @res;
408  
409 346 ▶ encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)")
Branch: 1 branch
BranchTrueFalseTotal
if ++$depth > $max_depth 1 345 346
410 if (++$depth > $max_depth);
411  
412 345 ▶ my ($pre, $post) = $indent ? $self->_up_indent() : ('', '');
Branch: 1 branch
BranchTrueFalseTotal
$indent ? : 7 338 345
413 345 ▶ my $del = ($space_before ? ' ' : '') . ':' . ($space_after ? ' ' : '');
Branch: 2 branches
BranchTrueFalseTotal
$space_before ? : 5 340 345
$space_after ? : 5 340 345
414  
415 345 for my $k ( _sort( $obj ) ) {
416 744 if ( OLD_PERL ) { utf8::decode($k) } # key for Perl 5.6 / be optimized
417 push @res, $self->string_to_json( $k )
418 . $del
419 744 ▶ . ( ref $obj->{$k} ? $self->object_to_json( $obj->{$k} ) : $self->value_to_json( $obj->{$k} ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $obj->{$k} ? : 36 708 744
420 }
421  
422 343 --$depth;
423 343 ▶ $self->_down_indent() if ($indent);
Branch: 1 branch
BranchTrueFalseTotal
if $indent 7 336 343
424  
425 343 ▶ return '{}' unless @res;
Branch: 1 branch
BranchTrueFalseTotal
unless @res 10 333 343
426 333 return '{' . $pre . join( ",$pre", @res ) . $post . '}';
427 }
428  
429  
430 sub array_to_json {
431 25010 ▶ my ($self, $obj) = @_;
Subroutine array_to_json: called CC=7 SCAR=19.5
Pod undocumented
432 25010 my @res;
433  
434 25010 ▶ encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)")
Branch: 1 branch
BranchTrueFalseTotal
if ++$depth > $max_depth 1 25009 25010
435 if (++$depth > $max_depth);
436  
437 25009 ▶ my ($pre, $post) = $indent ? $self->_up_indent() : ('', '');
Branch: 1 branch
BranchTrueFalseTotal
$indent ? : 2 25007 25009
438  
439 25009 for my $v (@$obj){
440 25795 ▶ push @res, ref($v) ? $self->object_to_json($v) : $self->value_to_json($v);
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 310 25485 25795
441 }
442  
443 24995 --$depth;
444 24995 ▶ $self->_down_indent() if ($indent);
Branch: 1 branch
BranchTrueFalseTotal
if $indent 2 24993 24995
445  
446 24995 ▶ return '[]' unless @res;
Branch: 1 branch
BranchTrueFalseTotal
unless @res 10 24985 24995
447 24985 return '[' . $pre . join( ",$pre", @res ) . $post . ']';
448 }
449  
450 sub _looks_like_number {
451 26230 ▶ my $value = shift;
Subroutine _looks_like_number: called CC=5 SCAR=16.2
452 26230 if (USE_B) {
453 my $b_obj = B::svref_2object(\$value);
454 my $flags = $b_obj->FLAGS;
455 return 1 if $flags & ( B::SVp_IOK() | B::SVp_NOK() ) and !( $flags & B::SVp_POK() );
456 return;
457 } else {
458 47 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
459 # if the utf8 flag is on, it almost certainly started as a string
460 26230 ▶ return if utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($value) 12487 13743 26230
461 # detect numbers
462 # string & "" -> ""
463 # number & "" -> 0 (with warning)
464 # nan and inf can detect as numbers, so check with * 0
465 13743 ▶ return unless length((my $dummy = "") & $value);
Branch: 1 branch
BranchTrueFalseTotal
unless length((my $dummy = "") & $value) 12926 817 13743
466 817 ▶ return unless 0 + $value eq $value;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $value eq $value 1 816 817
467 816 ▶ return 1 if $value * 0 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value * 0 == 0 816 0 816
468 0 return -1; # inf/nan
469 }
470 }
471  
472 sub value_to_json {
473 26314 ▶ my ($self, $value) = @_;
Subroutine value_to_json: called CC=16 SCAR=27.8
Pod undocumented
474  
475 26314 ▶ return 'null' if(!defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 44 26270 26314
476  
477 26270 my $type = ref($value);
478  
479 26270 ▶ if (!$type) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 26230 40 26270
elsif (&blessed($value) and $value->isa('JSON::PP::Boolean')) { } 20 20 40
480 26230 ▶ if (_looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_looks_like_number($value)) 816 25414 26230
481 816 return $value;
482 }
483 25414 return $self->string_to_json($value);
484 }
485 ▶ elsif( blessed($value) and $value->isa('JSON::PP::Boolean') ){
Condition66%2/3
&blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
20020
MC/DC50%1/2
&blessed($value) and $value->isa("JSON::PP::Boolean")
&blessed($value) $value->isa("JSON::PP::Boolean")
Truth table66%2/3
A and B
A= &blessed($value)B= $value->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
486 20 ▶ return $$value == 1 ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$$value == 1 ? : 10 10 20
487 }
488 else {
489 20 ▶ if ((overload::StrVal($value) =~ /=(\w+)/)[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ((overload::StrVal($value) =~ /=(\w+)/)[0]) 0 20 20
490 0 return $self->value_to_json("$value");
491 }
492  
493 20 ▶ if ($type eq 'SCALAR' and defined $$value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" and defined $$value) 7 13 20
Condition100%3/3
$type eq "SCALAR" and defined $$value
!ll&&!rl&&r
1037
MC/DC100%2/2
$type eq "SCALAR" and defined $$value
$type eq "SCALAR" defined $$value
Truth table100%3/3
A and B
A= $type eq "SCALAR"B= defined $$value
ABresult
0X0
100
111
494 return $$value eq '1' ? 'true'
495 : $$value eq '0' ? 'false'
496 7 ▶ : $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ? 'null'
Branch: 3 branches
BranchTrueFalseTotal
$$value eq '1' ? : 1 6 7
$$value eq '0' ? : 1 5 6
$self->{'PROPS'}[18] ? : 1 4 5
497 : encode_error("cannot encode reference to scalar");
498 }
499  
500 13 ▶ if ( $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PROPS'}[18]) { } 4 9 13
501 4 return 'null';
502 }
503 else {
504 9 ▶ if ( $type eq 'SCALAR' or $type eq 'REF' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'SCALAR' or $type eq 'REF') { } 6 3 9
Condition100%3/3
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
243
MC/DC100%2/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
Truth table100%3/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
505 6 encode_error("cannot encode reference to scalar");
506 }
507 else {
508 3 encode_error("encountered $value, but JSON can only represent references to arrays or hashes");
509 }
510 }
511  
512 }
513 }
514  
515  
516 47 my %esc = (
517 "\n" => '\n',
518 "\r" => '\r',
519 "\t" => '\t',
520 "\f" => '\f',
521 "\b" => '\b',
522 "\"" => '\"',
523 "\\" => '\\\\',
524 "\'" => '\\\'',
525 );
526  
527  
528 sub string_to_json {
529 26158 ▶ my ($self, $arg) = @_;
Subroutine string_to_json: called CC=5 SCAR=16.1
Pod undocumented
530  
531 26158 $arg =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g;
532 26158 ▶ $arg =~ s/\//\\\//g if ($escape_slash);
Branch: 1 branch
BranchTrueFalseTotal
if $escape_slash 1 26157 26158
533 26158 $arg =~ s/([\x00-\x08\x0b\x0e-\x1f])/'\\u00' . unpack('H2', $1)/eg;
534  
535 26158 ▶ if ($ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ascii) 12297 13861 26158
536 12297 $arg = JSON_PP_encode_ascii($arg);
537 }
538  
539 26158 ▶ if ($latin1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($latin1) 2 26156 26158
540 2 $arg = JSON_PP_encode_latin1($arg);
541 }
542  
543 26158 ▶ if ($utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) 12551 13607 26158
544 12551 utf8::encode($arg);
545 }
546  
547 26158 return '"' . $arg . '"';
548 }
549  
550  
551 sub blessed_to_json {
552 0 ▶ my $reftype = reftype($_[1]) || '';
Subroutine blessed_to_json: not called CC=4 SCAR=30.0
Pod undocumented
Condition0%0/2
&reftype($_[1]) || ''
l!l
00
MC/DC0%0/1
&reftype($_[1]) || ''
&reftype($_[1])
Truth table0%0/2
A
A= &reftype($_[1])
Aresult
11
00
553 0 ▶ if ($reftype eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 0 0 0
elsif ($reftype eq 'ARRAY') { } 0 0 0
554 0 return $_[0]->hash_to_json($_[1]);
555 }
556 elsif ($reftype eq 'ARRAY') {
557 0 return $_[0]->array_to_json($_[1]);
558 }
559 else {
560 0 return 'null';
561 }
562 }
563  
564  
565 sub encode_error {
566 18 ▶ my $error = shift;
Subroutine encode_error: called CC=1 SCAR=0.0
Pod undocumented
567 18 Carp::croak "$error";
568 }
569  
570  
571 sub _sort {
572 345 ▶ defined $keysort ? (sort $keysort (keys %{$_[0]})) : keys %{$_[0]};
Subroutine _sort: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $keysort ? : 222 123 345
573 }
574  
575  
576 sub _up_indent {
577 9 ▶ my $self = shift;
Subroutine _up_indent: called CC=1 SCAR=0.0
578 9 my $space = ' ' x $indent_length;
579  
580 9 my ($pre,$post) = ('','');
581  
582 9 $post = "\n" . $space x $indent_count;
583  
584 9 $indent_count++;
585  
586 9 $pre = "\n" . $space x $indent_count;
587  
588 9 return ($pre,$post);
589 }
590  
591  
592 9 ▶ sub _down_indent { $indent_count--; }
Subroutine _down_indent: called CC=1 SCAR=0.0
593  
594  
595 sub PP_encode_box {
596 {
597 0 ▶ depth => $depth,
Subroutine PP_encode_box: not called CC=1 SCAR=6.9
Pod undocumented
598 indent_count => $indent_count,
599 };
600 }
601  
602 } # Convert
603  
604  
605 47 sub _encode_ascii {
606 join('',
607 map {
608 12297 ▶ $_ <= 127 ?
Subroutine _encode_ascii: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
$_ <= 127 ? : 2824908 3440033 6264941
$_ <= 65535 ? : 2312298 1127735 3440033
609 chr($_) :
610 $_ <= 65535 ?
611 sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_));
612 } unpack('U*', $_[0])
613 );
614 }
615  
616  
617 sub _encode_latin1 {
618 join('',
619 map {
620 2 ▶ $_ <= 255 ?
Subroutine _encode_latin1: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
$_ <= 255 ? : 21 1 22
$_ <= 65535 ? : 1 0 1
621 chr($_) :
622 $_ <= 65535 ?
623 sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_));
624 } unpack('U*', $_[0])
625 );
626 }
627  
628  
629 sub _encode_surrogates { # from perlunicode
630 1127735 ▶ my $uni = $_[0] - 0x10000;
Subroutine _encode_surrogates: called CC=1 SCAR=0.0
631 1127735 return ($uni / 0x400 + 0xD800, $uni % 0x400 + 0xDC00);
632 }
633  
634  
635 sub _is_bignum {
636 3 ▶ $_[0]->isa('Math::BigInt') or $_[0]->isa('Math::BigFloat');
Subroutine _is_bignum: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->isa("Math::BigInt") or $_[0]->isa("Math::BigFloat") 1 2 3
637 }
638  
639  
640  
641 #
642 # JSON => Perl
643 #
644  
645 47 my $max_intsize;
646  
647 BEGIN {
648 47 ▶ my $checkint = 1111;
Subroutine BEGIN: called CC=3 SCAR=11.0
649 47 for my $d (5..64) {
650 799 $checkint .= 1;
651 799 my $int = eval qq| $checkint |;
652 799 ▶ if ($int =~ /[eE]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int =~ /[eE]/) 47 752 799
653 47 $max_intsize = $d - 1;
654 47 last;
655 }
656 }
657 }
658  
659 { # PARSE
660  
661 47 my %escapes = ( # by Jeremy Muhlich <jmuhlich [at] bitflood.org>
662 b => "\x8",
663 t => "\x9",
664 n => "\xA",
665 f => "\xC",
666 r => "\xD",
667 '\\' => '\\',
668 '"' => '"',
669 '/' => '/',
670 );
671  
672 47 my $text; # json data
673 my $at; # offset
674 47 my $ch; # first character
675 47 my $len; # text length (changed according to UTF8 or NON UTF8)
676 # INTERNAL
677 47 my $depth; # nest counter
678 47 my $encoding; # json text encoding
679 47 my $is_valid_utf8; # temp variable
680 47 my $utf8_len; # utf8 byte length
681 # FLAGS
682 47 my $utf8; # must be utf8
683 47 my $max_depth; # max nest number of objects and arrays
684 47 my $max_size;
685 47 my $relaxed;
686 47 my $cb_object;
687 47 my $cb_sk_object;
688  
689 47 my $F_HOOK;
690  
691 47 my $allow_bignum; # using Math::BigInt/BigFloat
692 47 my $singlequote; # loosely quoting
693 47 my $loose; #
694 47 my $allow_barekey; # bareKey
695 47 my $allow_tags;
696  
697 47 my $alt_true;
698 47 my $alt_false;
699  
700 sub _detect_utf_encoding {
701 12449 ▶ my $text = shift;
Subroutine _detect_utf_encoding: called CC=10 SCAR=25.0
702 12449 my @octets = unpack('C4', $text);
703 12449 ▶ return 'unknown' unless defined $octets[3];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $octets[3] 19 12430 12449
704 12430 ▶ return ( $octets[0] and $octets[1]) ? 'UTF-8'
Branch: 5 branches
BranchTrueFalseTotal
$octets[0] && $octets[1] ? : 12427 3 12430
!$octets[0] && $octets[1] ? : 1 2 3
!($octets[0] || $octets[1]) ? : 0 2 2
$octets[2] ? : 2 0 2
!$octets[2] ? : 0 0 0
Condition66%6/9
$octets[0] and $octets[1]
!ll&&!rl&&r
1212427
not $octets[0] and $octets[1]
!ll&&!rl&&r
201
$octets[0] || $octets[1]
l!l&&r!l&&!r
200
MC/DC100%2/2
$octets[0] and $octets[1]
$octets[0] $octets[1]
MC/DC50%1/2
not $octets[0] and $octets[1]
not $octets[0] $octets[1]
MC/DC0%0/2
$octets[0] || $octets[1]
$octets[0] $octets[1]
Truth table100%3/3
A and B
A= $octets[0]B= $octets[1]
ABresult
0X0
100
111
Truth table66%2/3
not(A) and B
A= not $octets[0]B= $octets[1]
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= $octets[0]B= $octets[1]
ABresult
1X1
011
000
705 : (!$octets[0] and $octets[1]) ? 'UTF-16BE'
706 : (!$octets[0] and !$octets[1]) ? 'UTF-32BE'
707 : ( $octets[2] ) ? 'UTF-16LE'
708 : (!$octets[2] ) ? 'UTF-32LE'
709 : 'unknown';
710 }
711  
712 sub PP_decode_json {
713 25290 ▶ my ($self, $want_offset);
Subroutine PP_decode_json: called CC=16 SCAR=27.7
Pod undocumented
714  
715 25290 ($self, $text, $want_offset) = @_;
716  
717 25290 ($at, $ch, $depth) = (0, '', 0);
718  
719 25290 ▶ if ( !defined $text or ref $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $text or ref $text) 4 25286 25290
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
1325286
MC/DC100%2/2
not defined $text or ref $text
not defined $text ref $text
Truth table100%3/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
720 4 decode_error("malformed JSON string, neither array, object, number, string or atom");
721 }
722  
723 25286 my $props = $self->{PROPS};
724  
725 ($utf8, $relaxed, $loose, $allow_bignum, $allow_barekey, $singlequote, $allow_tags)
726 25286 = @{$props}[P_UTF8, P_RELAXED, P_LOOSE .. P_ALLOW_SINGLEQUOTE, P_ALLOW_TAGS];
727  
728 25286 ($alt_true, $alt_false) = @$self{qw/true false/};
729  
730 25286 ▶ if ( $utf8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) { } 12449 12837 25286
731 12449 $encoding = _detect_utf_encoding($text);
732 12449 ▶ if ($encoding ne 'UTF-8' and $encoding ne 'unknown') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding ne 'UTF-8' and $encoding ne 'unknown') { } 3 12446 12449
Condition100%3/3
$encoding ne "UTF-8" and $encoding ne "unknown"
!ll&&!rl&&r
12427193
MC/DC100%2/2
$encoding ne "UTF-8" and $encoding ne "unknown"
$encoding ne "UTF-8" $encoding ne "unknown"
Truth table100%3/3
A and B
A= $encoding ne "UTF-8"B= $encoding ne "unknown"
ABresult
0X0
100
111
733 3 require Encode;
734 3 Encode::from_to($text, $encoding, 'utf-8');
735 } else {
736 12446 ▶ utf8::downgrade( $text, 1 ) or Carp::croak("Wide character in subroutine entry");
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($text, 1) or Carp::croak("Wide character in subroutine entry") 0 12446 12446
737 }
738 }
739 else {
740 12837 utf8::upgrade( $text );
741 12837 utf8::encode( $text );
742 }
743  
744 25286 $len = length $text;
745  
746 ($max_depth, $max_size, $cb_object, $cb_sk_object, $F_HOOK)
747 25286 = @{$self}{qw/max_depth max_size cb_object cb_sk_object F_HOOK/};
748  
749 25286 ▶ if ($max_size > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size > 1) 2 25284 25286
750 47 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
751 2 my $bytes = length $text;
752 2 ▶ decode_error(
Branch: 1 branch
BranchTrueFalseTotal
if $bytes > $max_size 1 1 2
753 sprintf("attempted decode of JSON text of %s bytes size, but max_size is set to %s"
754 , $bytes, $max_size), 1
755 ) if ($bytes > $max_size);
756 }
757  
758 25285 white(); # remove head white space
759  
760 25285 ▶ decode_error("malformed JSON string, neither array, object, number, string or atom") unless defined $ch; # Is there a first character for JSON structure?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ch 2 25283 25285
761  
762 25283 my $result = value();
763  
764 25212 ▶ if ( !$props->[ P_ALLOW_NONREF ] and !ref $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($props->[7] or ref $result) 4 25208 25212
Condition100%3/3
$props->[7] or ref $result
l!l&&r!l&&!r
250101984
MC/DC100%2/2
$props->[7] or ref $result
$props->[7] ref $result
Truth table100%3/3
A or B
A= $props->[7]B= ref $result
ABresult
1X1
011
000
765 4 decode_error(
766 'JSON text must be an object or array (but found number, string, true, false or null,'
767 . ' use allow_nonref to allow this)', 1);
768 }
769  
770 25208 ▶ Carp::croak('something wrong.') if $len < $at; # we won't arrive here.
Branch: 1 branch
BranchTrueFalseTotal
if $len < $at 0 25208 25208
771  
772 25208 ▶ my $consumed = defined $ch ? $at - 1 : $at; # consumed JSON text length
Branch: 1 branch
BranchTrueFalseTotal
defined $ch ? : 311 24897 25208
773  
774 25208 white(); # remove tail white space
775  
776 25208 ▶ return ( $result, $consumed ) if $want_offset; # all right if decode_prefix
Branch: 1 branch
BranchTrueFalseTotal
if $want_offset 288 24920 25208
777  
778 24920 ▶ decode_error("garbage after JSON object") if defined $ch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ch 12 24908 24920
779  
780 24908 $result;
781 }
782  
783  
784 sub next_chr {
785 36783337 ▶ return $ch = undef if($at >= $len);
Subroutine next_chr: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $at >= $len 25104 36758233 36783337
786 36758233 $ch = substr($text, $at++, 1);
787 }
788  
789  
790 sub value {
791 55440 ▶ white();
Subroutine value: called CC=10 SCAR=23.1
Pod undocumented
792 55440 ▶ return if(!defined $ch);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ch 0 55440 55440
793 55440 ▶ return object() if($ch eq '{');
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "{" 1988 53452 55440
794 53452 ▶ return array() if($ch eq '[');
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "[" 26256 27196 53452
795 27196 ▶ return tag() if($ch eq '(');
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "(" 1 27195 27196
796 27195 ▶ return string() if($ch eq '"' or ($singlequote and $ch eq "'"));
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "\"" or $singlequote and $ch eq "'" 26206 989 27195
Condition83%5/6
$ch eq "\"" or $singlequote and $ch eq "'"
l!l&&r!l&&!r
262044987
$singlequote and $ch eq "'"
!ll&&!rl&&r
98902
MC/DC0%0/3
$ch eq "\"" or $singlequote and $ch eq "'"
$ch eq "\"" $singlequote $ch eq "'"
Truth table50%2/4
A or B and C
A= $ch eq "\""B= $singlequoteC= $ch eq "'"
ABCresult
1XX1
0111
00X0
0100
797 989 ▶ return number() if($ch =~ /[0-9]/ or $ch eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $ch =~ /[0-9]/ or $ch eq "-" 901 88 989
Condition100%3/3
$ch =~ /[0-9]/ or $ch eq "-"
l!l&&r!l&&!r
8604188
MC/DC100%2/2
$ch =~ /[0-9]/ or $ch eq "-"
$ch =~ /[0-9]/ $ch eq "-"
Truth table100%3/3
A or B
A= $ch =~ /[0-9]/B= $ch eq "-"
ABresult
1X1
011
000
798 88 return word();
799 }
800  
801 sub string {
802 29333 ▶ my $utf16;
Subroutine string: called CC=27 SCAR=33.2
Pod documented
803 my $is_utf8;
804  
805 29333 ($is_valid_utf8, $utf8_len) = ('', 0);
806  
807 29333 my $s = ''; # basically UTF8 flag on
808  
809 29333 ▶ if($ch eq '"' or ($singlequote and $ch eq "'")){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" or $singlequote and $ch eq "'") 29328 5 29333
Condition83%5/6
$ch eq "\"" or $singlequote and $ch eq "'"
l!l&&r!l&&!r
2932454
$singlequote and $ch eq "'"
!ll&&!rl&&r
504
MC/DC0%0/3
$ch eq "\"" or $singlequote and $ch eq "'"
$ch eq "\"" $singlequote $ch eq "'"
Truth table50%2/4
A or B and C
A= $ch eq "\""B= $singlequoteC= $ch eq "'"
ABCresult
1XX1
0111
00X0
0100
810 29328 my $boundChar = $ch;
811  
812 29328 ▶ OUTER: while( defined(next_chr()) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined next_chr()) 10748827 1 10748828
813  
814 10748827 ▶ if($ch eq $boundChar){
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq $boundChar) { } 29313 10719514 10748827
elsif ($ch eq '\\') { } 5302021 5417493 10719514
815 29313 next_chr();
816  
817 29313 ▶ if ($utf16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf16) 1 29312 29313
818 1 decode_error("missing low surrogate character in surrogate pair");
819 }
820  
821 29312 ▶ utf8::decode($s) if($is_utf8);
Branch: 1 branch
BranchTrueFalseTotal
if $is_utf8 23775 5537 29312
822  
823 29312 return $s;
824 }
825 elsif($ch eq '\\'){
826 5302021 next_chr();
827 5302021 ▶ if(exists $escapes{$ch}){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $escapes{$ch}) { } 153373 5148648 5302021
elsif ($ch eq 'u') { } 5148644 4 5148648
828 153373 $s .= $escapes{$ch};
829 }
830 elsif($ch eq 'u'){ # UNICODE handling
831 5148644 my $u = '';
832  
833 5148644 for(1..4){
834 20594576 $ch = next_chr();
835 20594576 ▶ last OUTER if($ch !~ /[0-9a-fA-F]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $ch =~ /[0-9a-fA-F]/ 0 20594576 20594576
836 20594576 $u .= $ch;
837 }
838  
839 # U+D800 - U+DBFF
840 5148644 ▶ if ($u =~ /^[dD][89abAB][0-9a-fA-F]{2}/) { # UTF-16 high surrogate?
Branch: 2 branches
BranchTrueFalseTotal
if ($u =~ /^[dD][89abAB][0-9a-fA-F]{2}/) { } 1127737 4020907 5148644
elsif ($u =~ /^[dD][c-fC-F][0-9a-fA-F]{2}/) { } 1127736 2893171 4020907
841 1127737 $utf16 = $u;
842 }
843 # U+DC00 - U+DFFF
844 elsif ($u =~ /^[dD][c-fC-F][0-9a-fA-F]{2}/) { # UTF-16 low surrogate?
845 1127736 ▶ unless (defined $utf16) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $utf16) 1 1127735 1127736
846 1 decode_error("missing high surrogate character in surrogate pair");
847 }
848 1127735 $is_utf8 = 1;
849 1127735 ▶ $s .= JSON_PP_decode_surrogates($utf16, $u) || next;
Condition50%1/2
JSON_PP_decode_surrogates($utf16, $u) || (next)
l!l
11277350
MC/DC0%0/1
JSON_PP_decode_surrogates($utf16, $u) || (next)
JSON_PP_decode_surrogates($utf16, $u)
Truth table50%1/2
A
A= JSON_PP_decode_surrogates($utf16, $u)
Aresult
11
00
850 1127735 $utf16 = undef;
851 }
852 else {
853 2893171 ▶ if (defined $utf16) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $utf16) 1 2893170 2893171
854 1 decode_error("surrogate pair expected");
855 }
856  
857 2893170 ▶ if ( ( my $hex = hex( $u ) ) > 127 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $hex = hex $u) > 127) { } 2312509 580661 2893170
858 2312509 $is_utf8 = 1;
859 2312509 ▶ $s .= JSON_PP_decode_unicode($u) || next;
Condition50%1/2
JSON_PP_decode_unicode($u) || (next)
l!l
23125090
MC/DC0%0/1
JSON_PP_decode_unicode($u) || (next)
JSON_PP_decode_unicode($u)
Truth table50%1/2
A
A= JSON_PP_decode_unicode($u)
Aresult
11
00
860 }
861 else {
862 580661 $s .= chr $hex;
863 }
864 }
865  
866 }
867 else{
868 4 ▶ unless ($loose) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loose) 4 0 4
869 4 $at -= 2;
870 4 decode_error('illegal backslash escape sequence in string');
871 }
872 0 $s .= $ch;
873 }
874 }
875 else{
876  
877 5417493 ▶ if ( ord $ch > 127 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $ch > 127) 3440092 1977401 5417493
878 3440092 ▶ unless( $ch = is_valid_utf8($ch) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch = is_valid_utf8($ch)) { } 3440088 4 3440092
879 4 $at -= 1;
880 4 decode_error("malformed UTF-8 character in JSON string");
881 }
882 else {
883 3440088 $at += $utf8_len - 1;
884 }
885  
886 3440088 $is_utf8 = 1;
887 }
888  
889 5417489 ▶ if (!$loose) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loose) 5417489 0 5417489
890 5417489 ▶ if ($ch =~ /[\x00-\x1f\x22\x5c]/) { # '/' ok
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[\x00-\x1f\x22\x5c]/) 4 5417485 5417489
891 4 ▶ if (!$relaxed or $ch ne "\t") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $relaxed or $ch ne "\t") 4 0 4
Condition33%1/3
not $relaxed or $ch ne "\t"
l!l&&r!l&&!r
400
MC/DC0%0/2
not $relaxed or $ch ne "\t"
not $relaxed $ch ne "\t"
Truth table33%1/3
not(A) or B
A= not $relaxedB= $ch ne "\t"
ABresult
1X1
011
000
892 4 $at--;
893 4 decode_error('invalid character encountered while parsing JSON string');
894 }
895 }
896 }
897  
898 5417485 $s .= $ch;
899 }
900 }
901 }
902  
903 6 decode_error("unexpected end of string while parsing JSON string");
904 }
905  
906  
907 sub white {
908 168330 ▶ while( defined $ch ){
Subroutine white: called CC=19 SCAR=47.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) 189782 25096 214878
909 189782 ▶ if($ch eq '' or $ch =~ /\A[ \t\r\n]\z/){
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq '' or $ch =~ /\A[ \t\r\n]\z/) { } 46539 143243 189782
elsif ($relaxed and $ch eq '/') { } 0 143243 143243
Condition100%3/3
$ch eq "" or $ch =~ /\A[ \t\r\n]\z/
l!l&&r!l&&!r
2528521254143243
MC/DC100%2/2
$ch eq "" or $ch =~ /\A[ \t\r\n]\z/
$ch eq "" $ch =~ /\A[ \t\r\n]\z/
Truth table100%3/3
A or B
A= $ch eq ""B= $ch =~ /\A[ \t\r\n]\z/
ABresult
1X1
011
000
910 46539 next_chr();
911 }
912 ▶ elsif($relaxed and $ch eq '/'){
Condition66%2/3
$relaxed and $ch eq "/"
!ll&&!rl&&r
1431401030
MC/DC0%0/2
$relaxed and $ch eq "/"
$relaxed $ch eq "/"
Truth table66%2/3
A and B
A= $relaxedB= $ch eq "/"
ABresult
0X0
100
111
913 0 next_chr();
914 0 ▶ if(defined $ch and $ch eq '/'){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ch and $ch eq '/') { } 0 0 0
elsif (defined $ch and $ch eq '*') { } 0 0 0
Condition0%0/3
defined $ch and $ch eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ch and $ch eq "/"
defined $ch $ch eq "/"
Truth table0%0/3
A and B
A= defined $chB= $ch eq "/"
ABresult
0X0
100
111
915 0 ▶ 1 while(defined(next_chr()) and $ch ne "\n" and $ch ne "\r");
Branch: 1 branch
BranchTrueFalseTotal
defined next_chr() and $ch ne "\n" and $ch ne "\r" and do { '???' } 0 0 0
Condition0%0/6
defined next_chr() and $ch ne "\n" and $ch ne "\r"
!ll&&!rl&&r
000
defined next_chr() and $ch ne "\n"
!ll&&!rl&&r
000
MC/DC0%0/3
defined next_chr() and $ch ne "\n" and $ch ne "\r"
defined next_chr() $ch ne "\n" $ch ne "\r"
Truth table0%0/4
A and B and C
A= defined next_chr()B= $ch ne "\n"C= $ch ne "\r"
ABCresult
0XX0
10X0
1100
1111
916 }
917 ▶ elsif(defined $ch and $ch eq '*'){
Condition0%0/3
defined $ch and $ch eq "*"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ch and $ch eq "*"
defined $ch $ch eq "*"
Truth table0%0/3
A and B
A= defined $chB= $ch eq "*"
ABresult
0X0
100
111
918 0 next_chr();
919 0 while(1){
920 0 ▶ if(defined $ch){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) { } 0 0 0
921 0 ▶ if($ch eq '*'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq '*') { } 0 0 0
922 0 ▶ if(defined(next_chr()) and $ch eq '/'){
Branch: 1 branch
BranchTrueFalseTotal
if (defined next_chr() and $ch eq "/") 0 0 0
Condition0%0/3
defined next_chr() and $ch eq "/"
!ll&&!rl&&r
000
MC/DC0%0/2
defined next_chr() and $ch eq "/"
defined next_chr() $ch eq "/"
Truth table0%0/3
A and B
A= defined next_chr()B= $ch eq "/"
ABresult
0X0
100
111
923 0 next_chr();
924 0 last;
925 }
926 }
927 else{
928 0 next_chr();
929 }
930 }
931 else{
932 0 decode_error("Unterminated comment");
933 }
934 }
935 0 next;
936 }
937 else{
938 0 $at--;
939 0 decode_error("malformed JSON string, neither array, object, number, string or atom");
940 }
941 }
942 else{
943 143243 ▶ if ($relaxed and $ch eq '#') { # correctly?
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed and $ch eq "#") 9 143234 143243
Condition100%3/3
$relaxed and $ch eq "#"
!ll&&!rl&&r
143140949
MC/DC100%2/2
$relaxed and $ch eq "#"
$relaxed $ch eq "#"
Truth table100%3/3
A and B
A= $relaxedB= $ch eq "#"
ABresult
0X0
100
111
944 9 pos($text) = $at;
945 9 $text =~ /\G([^\n]*(?:\r\n|\r|\n|$))/g;
946 9 $at = pos($text);
947 9 next_chr;
948 9 next;
949 }
950  
951 143234 last;
952 }
953 }
954 }
955  
956  
957 sub array {
958 26256 ▶ my $a = $_[0] || []; # you can use this code to use another array ref object.
Subroutine array: called CC=13 SCAR=25.7
Pod documented
Condition50%1/2
$_[0] || []
l!l
026256
MC/DC0%0/1
$_[0] || []
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
959  
960 26256 ▶ decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)')
Branch: 1 branch
BranchTrueFalseTotal
if ++$depth > $max_depth 3 26253 26256
961 if (++$depth > $max_depth);
962  
963 26253 next_chr();
964 26253 white();
965  
966 26253 ▶ if(defined $ch and $ch eq ']'){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch eq ']') { } 23 26230 26253
Condition66%2/3
defined $ch and $ch eq "]"
!ll&&!rl&&r
02623023
MC/DC50%1/2
defined $ch and $ch eq "]"
defined $ch $ch eq "]"
Truth table66%2/3
A and B
A= defined $chB= $ch eq "]"
ABresult
0X0
100
111
967 23 --$depth;
968 23 next_chr();
969 23 return $a;
970 }
971 else {
972 26230 ▶ while(defined($ch)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) 27034 0 27034
973 27034 push @$a, value();
974  
975 26467 white();
976  
977 26467 ▶ if (!defined $ch) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ch) 3 26464 26467
978 3 last;
979 }
980  
981 26464 ▶ if($ch eq ']'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "]") 25655 809 26464
982 25655 --$depth;
983 25655 next_chr();
984 25655 return $a;
985 }
986  
987 809 ▶ if($ch ne ','){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch ne ",") 3 806 809
988 3 last;
989 }
990  
991 806 next_chr();
992 806 white();
993  
994 806 ▶ if ($relaxed and $ch eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed and $ch eq "]") 2 804 806
Condition100%3/3
$relaxed and $ch eq "]"
!ll&&!rl&&r
79592
MC/DC100%2/2
$relaxed and $ch eq "]"
$relaxed $ch eq "]"
Truth table100%3/3
A and B
A= $relaxedB= $ch eq "]"
ABresult
0X0
100
111
995 2 --$depth;
996 2 next_chr();
997 2 return $a;
998 }
999  
1000 }
1001 }
1002  
1003 6 ▶ $at-- if defined $ch and $ch ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ch and $ch ne "" 3 3 6
Condition66%2/3
defined $ch and $ch ne ""
!ll&&!rl&&r
303
MC/DC50%1/2
defined $ch and $ch ne ""
defined $ch $ch ne ""
Truth table66%2/3
A and B
A= defined $chB= $ch ne ""
ABresult
0X0
100
111
1004 6 decode_error(", or ] expected while parsing array");
1005 }
1006  
1007 sub tag {
1008 1 ▶ decode_error('malformed JSON string, neither array, object, number, string or atom') unless $allow_tags;
Subroutine tag: called CC=10 SCAR=27.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $allow_tags 0 1 1
1009  
1010 1 next_chr();
1011 1 white();
1012  
1013 1 my $tag = value();
1014 1 ▶ return unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 1 1
1015 1 ▶ decode_error('malformed JSON string, (tag) must be a string') if ref $tag;
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag 0 1 1
1016  
1017 1 white();
1018  
1019 1 ▶ if (!defined $ch or $ch ne ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ch or $ch ne ")") 0 1 1
Condition33%1/3
not defined $ch or $ch ne ")"
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $ch or $ch ne ")"
not defined $ch $ch ne ")"
Truth table33%1/3
not(A) or B
A= not defined $chB= $ch ne ")"
ABresult
1X1
011
000
1020 0 decode_error(') expected after tag');
1021 }
1022  
1023 1 next_chr();
1024 1 white();
1025  
1026 1 my $val = value();
1027 1 ▶ return unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 1 1
1028 1 ▶ decode_error('malformed JSON string, tag value must be an array') unless ref $val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "ARRAY" 0 1 1
1029  
1030 1 ▶ if (!eval { $tag->can('THAW') }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $tag->can("THAW") } }) 0 1 1
1031 0 ▶ decode_error('cannot decode perl-object (package does not exist)') if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1032 0 decode_error('cannot decode perl-object (package does not have a THAW method)');
1033 }
1034 1 $tag->THAW('JSON', @$val);
1035 }
1036  
1037 sub object {
1038 1988 ▶ my $o = $_[0] || {}; # you can use this code to use another hash ref object.
Subroutine object: called CC=21 SCAR=30.7
Pod documented
Condition50%1/2
$_[0] || {}
l!l
01988
MC/DC0%0/1
$_[0] || {}
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
1039 1988 my $k;
1040  
1041 1988 ▶ decode_error('json text or perl structure exceeds maximum nesting level (max_depth set too low?)')
Branch: 1 branch
BranchTrueFalseTotal
if ++$depth > $max_depth 0 1988 1988
1042 if (++$depth > $max_depth);
1043 1988 next_chr();
1044 1988 white();
1045  
1046 1988 ▶ if(defined $ch and $ch eq '}'){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch eq '}') { } 9 1979 1988
Condition66%2/3
defined $ch and $ch eq "}"
!ll&&!rl&&r
019799
MC/DC50%1/2
defined $ch and $ch eq "}"
defined $ch $ch eq "}"
Truth table66%2/3
A and B
A= defined $chB= $ch eq "}"
ABresult
0X0
100
111
1047 9 --$depth;
1048 9 next_chr();
1049 9 ▶ if ($F_HOOK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_HOOK) 1 8 9
1050 1 return _json_object_hook($o);
1051 }
1052 8 return $o;
1053 }
1054 else {
1055 1979 ▶ while (defined $ch) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) 3129 1 3130
1056 3129 ▶ $k = ($allow_barekey and $ch ne '"' and $ch ne "'") ? bareKey() : string();
Branch: 1 branch
BranchTrueFalseTotal
$allow_barekey && $ch ne '"' && $ch ne q['] ? : 2 3127 3129
Condition66%4/6
$allow_barekey and $ch ne "\"" and $ch ne "'"
!ll&&!rl&&r
312702
$allow_barekey and $ch ne "\""
!ll&&!rl&&r
312702
MC/DC33%1/3
$allow_barekey and $ch ne "\"" and $ch ne "'"
$allow_barekey $ch ne "\"" $ch ne "'"
Truth table50%2/4
A and B and C
A= $allow_barekeyB= $ch ne "\""C= $ch ne "'"
ABCresult
0XX0
10X0
1100
1111
1057 3124 white();
1058  
1059 3124 ▶ if(!defined $ch or $ch ne ':'){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ch or $ch ne ":") 3 3121 3124
Condition100%3/3
not defined $ch or $ch ne ":"
l!l&&r!l&&!r
123121
MC/DC100%2/2
not defined $ch or $ch ne ":"
not defined $ch $ch ne ":"
Truth table100%3/3
not(A) or B
A= not defined $chB= $ch ne ":"
ABresult
1X1
011
000
1060 3 $at--;
1061 3 decode_error("':' expected");
1062 }
1063  
1064 3121 next_chr();
1065 3121 $o->{$k} = value();
1066 2604 white();
1067  
1068 2604 ▶ last if (!defined $ch);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ch 2 2602 2604
1069  
1070 2602 ▶ if($ch eq '}'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "}") 1447 1155 2602
1071 1447 --$depth;
1072 1447 next_chr();
1073 1447 ▶ if ($F_HOOK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_HOOK) 8 1439 1447
1074 8 return _json_object_hook($o);
1075 }
1076 1439 return $o;
1077 }
1078  
1079 1155 ▶ if($ch ne ','){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch ne ",") 3 1152 1155
1080 3 last;
1081 }
1082  
1083 1152 next_chr();
1084 1152 white();
1085  
1086 1152 ▶ if ($relaxed and $ch eq '}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed and $ch eq "}") 1 1151 1152
Condition66%2/3
$relaxed and $ch eq "}"
!ll&&!rl&&r
115101
MC/DC50%1/2
$relaxed and $ch eq "}"
$relaxed $ch eq "}"
Truth table66%2/3
A and B
A= $relaxedB= $ch eq "}"
ABresult
0X0
100
111
1087 1 --$depth;
1088 1 next_chr();
1089 1 ▶ if ($F_HOOK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_HOOK) 0 1 1
1090 0 return _json_object_hook($o);
1091 }
1092 1 return $o;
1093 }
1094  
1095 }
1096  
1097 }
1098  
1099 6 ▶ $at-- if defined $ch and $ch ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ch and $ch ne "" 3 3 6
Condition66%2/3
defined $ch and $ch ne ""
!ll&&!rl&&r
303
MC/DC50%1/2
defined $ch and $ch ne ""
defined $ch $ch ne ""
Truth table66%2/3
A and B
A= defined $chB= $ch ne ""
ABresult
0X0
100
111
1100 6 decode_error(", or } expected while parsing object/hash");
1101 }
1102  
1103  
1104 sub bareKey { # doesn't strictly follow Standard ECMA-262 3rd Edition
1105 2 ▶ my $key;
Subroutine bareKey: called CC=2 SCAR=6.9
Pod undocumented
1106 2 ▶ while($ch =~ /[^\x00-\x23\x25-\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\x7F]/){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[^\x00-\x23\x25-\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\x7F]/) 6 2 8
1107 6 $key .= $ch;
1108 6 next_chr();
1109 }
1110 2 return $key;
1111 }
1112  
1113  
1114 sub word {
1115 88 ▶ my $word = substr($text,$at-1,4);
Subroutine word: called CC=10 SCAR=23.0
Pod undocumented
1116  
1117 88 ▶ if($word eq 'true'){
Branch: 3 branches
BranchTrueFalseTotal
if ($word eq 'true') { } 13 75 88
elsif ($word eq 'null') { } 44 31 75
elsif ($word eq 'fals') { } 10 21 31
1118 13 $at += 3;
1119 13 next_chr;
1120 13 ▶ return defined $alt_true ? $alt_true : $JSON::PP::true;
Branch: 1 branch
BranchTrueFalseTotal
defined $alt_true ? : 2 11 13
1121 }
1122 elsif($word eq 'null'){
1123 44 $at += 3;
1124 44 next_chr;
1125 44 return undef;
1126 }
1127 elsif($word eq 'fals'){
1128 10 $at += 3;
1129 10 ▶ if(substr($text,$at,1) eq 'e'){
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $at, 1) eq "e") 10 0 10
1130 10 $at++;
1131 10 next_chr;
1132 10 ▶ return defined $alt_false ? $alt_false : $JSON::PP::false;
Branch: 1 branch
BranchTrueFalseTotal
defined $alt_false ? : 2 8 10
1133 }
1134 }
1135  
1136 21 $at--; # for decode_error report
1137  
1138 21 ▶ decode_error("'null' expected") if ($word =~ /^n/);
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^n/ 1 20 21
1139 20 ▶ decode_error("'true' expected") if ($word =~ /^t/);
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^t/ 1 19 20
1140 19 ▶ decode_error("'false' expected") if ($word =~ /^f/);
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^f/ 0 19 19
1141 19 decode_error("malformed JSON string, neither array, object, number, string or atom");
1142 }
1143  
1144  
1145 sub number {
1146 901 ▶ my $n = '';
Subroutine number: called CC=32 SCAR=34.7
Pod documented
1147 901 my $v;
1148 my $is_dec;
1149 901 my $is_exp;
1150  
1151 901 ▶ if($ch eq '-'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "-") 41 860 901
1152 41 $n = '-';
1153 41 next_chr;
1154 41 ▶ if (!defined $ch or $ch !~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ch and $ch =~ /\d/) 1 40 41
Condition66%2/3
defined $ch and $ch =~ /\d/
!ll&&!rl&&r
0140
MC/DC50%1/2
defined $ch and $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table66%2/3
A and B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1155 1 decode_error("malformed number (no digits after initial minus)");
1156 }
1157 }
1158  
1159 # According to RFC4627, hex or oct digits are invalid.
1160 900 ▶ if($ch eq '0'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq 0) 93 807 900
1161 93 my $peek = substr($text,$at,1);
1162 93 ▶ if($peek =~ /^[0-9a-dfA-DF]/){ # e may be valid (exponential)
Branch: 1 branch
BranchTrueFalseTotal
if ($peek =~ /^[0-9a-dfA-DF]/) 5 88 93
1163 5 decode_error("malformed number (leading zero must not be followed by another digit)");
1164 }
1165 88 $n .= $ch;
1166 88 next_chr;
1167 }
1168  
1169 895 ▶ while(defined $ch and $ch =~ /\d/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch =~ /\d/) 1013 895 1908
Condition100%3/3
defined $ch and $ch =~ /\d/
!ll&&!rl&&r
68891013
MC/DC100%2/2
defined $ch and $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table100%3/3
A and B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1170 1013 $n .= $ch;
1171 1013 next_chr;
1172 }
1173  
1174 895 ▶ if(defined $ch and $ch eq '.'){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch eq ".") 49 846 895
Condition100%3/3
defined $ch and $ch eq "."
!ll&&!rl&&r
684049
MC/DC100%2/2
defined $ch and $ch eq "."
defined $ch $ch eq "."
Truth table100%3/3
A and B
A= defined $chB= $ch eq "."
ABresult
0X0
100
111
1175 49 $n .= '.';
1176 49 $is_dec = 1;
1177  
1178 49 next_chr;
1179 49 ▶ if (!defined $ch or $ch !~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ch && $ch =~ /\d/) { } 1 48 49
Condition66%2/3
defined $ch && $ch =~ /\d/
!ll&&!rl&&r
1048
MC/DC50%1/2
defined $ch && $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table66%2/3
A && B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1180 1 decode_error("malformed number (no digits after decimal point)");
1181 }
1182 else {
1183 48 $n .= $ch;
1184 }
1185  
1186 48 ▶ while(defined(next_chr) and $ch =~ /\d/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined next_chr() and $ch =~ /\d/) 97 48 145
Condition100%3/3
defined next_chr() and $ch =~ /\d/
!ll&&!rl&&r
24697
MC/DC100%2/2
defined next_chr() and $ch =~ /\d/
defined next_chr() $ch =~ /\d/
Truth table100%3/3
A and B
A= defined next_chr()B= $ch =~ /\d/
ABresult
0X0
100
111
1187 97 $n .= $ch;
1188 }
1189 }
1190  
1191 894 ▶ if(defined $ch and ($ch eq 'e' or $ch eq 'E')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch eq "e" || $ch eq "E") 58 836 894
Condition100%6/6
defined $ch and $ch eq "e" || $ch eq "E"
!ll&&!rl&&r
884937
$ch eq "e" or $ch eq "E"
l!l&&r!l&&!r
3325828
MC/DC66%2/3
defined $ch and $ch eq "e" || $ch eq "E"
defined $ch $ch eq "e" $ch eq "E"
Truth table75%3/4
A and B or C
A= defined $chB= $ch eq "e"C= $ch eq "E"
ABCresult
0XX0
1000
11X1
1011
1192 58 $n .= $ch;
1193 58 $is_exp = 1;
1194 58 next_chr;
1195  
1196 58 ▶ if(defined($ch) and ($ch eq '+' or $ch eq '-')){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ch and $ch eq '+' || $ch eq '-') { } 38 20 58
elsif (defined $ch and $ch =~ /\d/) { } 18 2 20
Condition100%6/6
defined $ch and $ch eq "+" || $ch eq "-"
!ll&&!rl&&r
11938
$ch eq "+" or $ch eq "-"
l!l&&r!l&&!r
32619
MC/DC100%3/3
defined $ch and $ch eq "+" || $ch eq "-"
defined $ch $ch eq "+" $ch eq "-"
Truth table100%4/4
A and B or C
A= defined $chB= $ch eq "+"C= $ch eq "-"
ABCresult
0XX0
1000
11X1
1011
1197 38 $n .= $ch;
1198 38 next_chr;
1199 38 ▶ if (!defined $ch or $ch =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ch or $ch =~ /\D/) 2 36 38
Condition66%2/3
not defined $ch or $ch =~ /\D/
l!l&&r!l&&!r
0236
MC/DC50%1/2
not defined $ch or $ch =~ /\D/
not defined $ch $ch =~ /\D/
Truth table66%2/3
not(A) or B
A= not defined $chB= $ch =~ /\D/
ABresult
1X1
011
000
1200 2 decode_error("malformed number (no digits after exp sign)");
1201 }
1202 36 $n .= $ch;
1203 }
1204 ▶ elsif(defined($ch) and $ch =~ /\d/){
Condition100%3/3
defined $ch and $ch =~ /\d/
!ll&&!rl&&r
1118
MC/DC100%2/2
defined $ch and $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table100%3/3
A and B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1205 18 $n .= $ch;
1206 }
1207 else {
1208 2 decode_error("malformed number (no digits after exp sign)");
1209 }
1210  
1211 54 ▶ while(defined(next_chr) and $ch =~ /\d/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined next_chr() and $ch =~ /\d/) 33 54 87
Condition100%3/3
defined next_chr() and $ch =~ /\d/
!ll&&!rl&&r
163833
MC/DC100%2/2
defined next_chr() and $ch =~ /\d/
defined next_chr() $ch =~ /\d/
Truth table100%3/3
A and B
A= defined next_chr()B= $ch =~ /\d/
ABresult
0X0
100
111
1212 33 $n .= $ch;
1213 }
1214  
1215 }
1216  
1217 890 $v .= $n;
1218  
1219 890 ▶ if ($is_dec or $is_exp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dec or $is_exp) { } 70 820 890
Condition100%3/3
$is_dec or $is_exp
l!l&&r!l&&!r
4822820
MC/DC100%2/2
$is_dec or $is_exp
$is_dec $is_exp
Truth table100%3/3
A or B
A= $is_decB= $is_exp
ABresult
1X1
011
000
1220 70 ▶ if ($allow_bignum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_bignum) 1 69 70
1221 1 require Math::BigFloat;
1222 1 return Math::BigFloat->new($v);
1223 }
1224 } else {
1225 820 ▶ if (length $v > $max_intsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v > $max_intsize) 1 819 820
1226 1 ▶ if ($allow_bignum) { # from Adam Sussman
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_bignum) { } 1 0 1
1227 1 require Math::BigInt;
1228 1 return Math::BigInt->new($v);
1229 }
1230 else {
1231 0 return "$v";
1232 }
1233 }
1234 }
1235  
1236 888 ▶ return $is_dec ? $v/1.0 : 0+$v;
Branch: 1 branch
BranchTrueFalseTotal
$is_dec ? : 47 841 888
1237 }
1238  
1239  
1240 sub is_valid_utf8 {
1241  
1242 3440092 ▶ $utf8_len = $_[0] =~ /[\x00-\x7F]/ ? 1
Subroutine is_valid_utf8: called CC=7 SCAR=19.5
Pod undocumented
Branch: 4 branches
BranchTrueFalseTotal
$_[0] =~ /[\x00-\x7F]/ ? : 0 3440092 3440092
$_[0] =~ /[\xC2-\xDF]/ ? : 1194793 2245299 3440092
$_[0] =~ /[\xE0-\xEF]/ ? : 1117561 1127738 2245299
$_[0] =~ /[\xF0-\xF4]/ ? : 1127735 3 1127738
1243 : $_[0] =~ /[\xC2-\xDF]/ ? 2
1244 : $_[0] =~ /[\xE0-\xEF]/ ? 3
1245 : $_[0] =~ /[\xF0-\xF4]/ ? 4
1246 : 0
1247 ;
1248  
1249 3440092 ▶ return unless $utf8_len;
Branch: 1 branch
BranchTrueFalseTotal
unless $utf8_len 3 3440089 3440092
1250  
1251 3440089 my $is_valid_utf8 = substr($text, $at - 1, $utf8_len);
1252  
1253 3440089 ▶ return ( $is_valid_utf8 =~ /^(?:
Branch: 1 branch
BranchTrueFalseTotal
$is_valid_utf8 =~ /^(?: [\x00-\x7F] |[\xC2-\xDF][\x80-\xBF] |[\xE0][\xA0-\xBF][\x80-\xBF] |[\xE1-\xEC][\x80-\xBF][\x80-\xBF] |[\xED][\x80-\x9F][\x80-\xBF] |[\xEE-\xEF][\x80-\xBF][\x80-\xBF] |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF] |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF] |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF] )$/x ? : 3440088 1 3440089
1254 [\x00-\x7F]
1255 |[\xC2-\xDF][\x80-\xBF]
1256 |[\xE0][\xA0-\xBF][\x80-\xBF]
1257 |[\xE1-\xEC][\x80-\xBF][\x80-\xBF]
1258 |[\xED][\x80-\x9F][\x80-\xBF]
1259 |[\xEE-\xEF][\x80-\xBF][\x80-\xBF]
1260 |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF]
1261 |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]
1262 |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF]
1263 )$/x ) ? $is_valid_utf8 : '';
1264 }
1265  
1266  
1267 sub decode_error {
1268 94 ▶ my $error = shift;
Subroutine decode_error: called CC=14 SCAR=26.5
Pod undocumented
1269 94 my $no_rep = shift;
1270 94 ▶ my $str = defined $text ? substr($text, $at) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 93 1 94
1271 94 my $mess = '';
1272 94 my $type = 'U*';
1273  
1274 94 if ( OLD_PERL ) {
1275 my $type = $] < 5.006 ? 'C*'
1276 : utf8::is_utf8( $str ) ? 'U*' # 5.6
1277 : 'C*'
1278 ;
1279 }
1280  
1281 94 for my $c ( unpack( $type, $str ) ) { # emulate pv_uni_display() ?
1282 450 ▶ $mess .= $c == 0x07 ? '\a'
Branch: 8 branches
BranchTrueFalseTotal
$c == 7 ? : 0 450 450
$c == 9 ? : 3 447 450
$c == 10 ? : 3 444 447
$c == 13 ? : 0 444 444
$c == 12 ? : 0 444 444
$c < 32 ? : 2 442 444
$c == 92 ? : 6 436 442
$c < 128 ? : 428 8 436
1283 : $c == 0x09 ? '\t'
1284 : $c == 0x0a ? '\n'
1285 : $c == 0x0d ? '\r'
1286 : $c == 0x0c ? '\f'
1287 : $c < 0x20 ? sprintf('\x{%x}', $c)
1288 : $c == 0x5c ? '\\\\'
1289 : $c < 0x80 ? chr($c)
1290 : sprintf('\x{%x}', $c)
1291 ;
1292 450 ▶ if ( length $mess >= 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mess >= 20) 12 438 450
1293 12 $mess .= '...';
1294 12 last;
1295 }
1296 }
1297  
1298 94 ▶ unless ( length $mess ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $mess) 29 65 94
1299 29 $mess = '(end of string)';
1300 }
1301  
1302 Carp::croak (
1303 94 ▶ $no_rep ? "$error" : "$error, at character offset $at (before \"$mess\")"
Branch: 1 branch
BranchTrueFalseTotal
$no_rep ? : 5 89 94
1304 );
1305  
1306 }
1307  
1308  
1309 sub _json_object_hook {
1310 9 ▶ my $o = $_[0];
Subroutine _json_object_hook: called CC=10 SCAR=23.5
1311 9 my @ks = keys %{$o};
1312  
1313 9 ▶ if ( $cb_sk_object and @ks == 1 and exists $cb_sk_object->{ $ks[0] } and ref $cb_sk_object->{ $ks[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb_sk_object and @ks == 1 and exists $$cb_sk_object{$ks[0]} and ref $$cb_sk_object{$ks[0]}) 4 5 9
Condition77%7/9
$cb_sk_object and @ks == 1 and exists $$cb_sk_object{$ks[0]} and ref $$cb_sk_object{$ks[0]}
!ll&&!rl&&r
504
$cb_sk_object and @ks == 1 and exists $$cb_sk_object{$ks[0]}
!ll&&!rl&&r
414
$cb_sk_object and @ks == 1
!ll&&!rl&&r
045
MC/DC50%2/4
$cb_sk_object and @ks == 1 and exists $$cb_sk_object{$ks[0]} and ref $$cb_sk_object{$ks[0]}
$cb_sk_object @ks == 1 exists $$cb_sk_object{$ks[0]} ref $$cb_sk_object{$ks[0]}
Truth table60%3/5
A and B and C and D
A= $cb_sk_objectB= @ks == 1C= exists $$cb_sk_object{$ks[0]}D= ref $$cb_sk_object{$ks[0]}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1314 4 my @val = $cb_sk_object->{ $ks[0] }->( $o->{$ks[0]} );
1315 4 ▶ if (@val == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@val == 0) { } 1 3 4
elsif (@val == 1) { } 3 0 3
1316 1 return $o;
1317 }
1318 elsif (@val == 1) {
1319 3 return $val[0];
1320 }
1321 else {
1322 0 Carp::croak("filter_json_single_key_object callbacks must not return more than one scalar");
1323 }
1324 }
1325  
1326 5 ▶ my @val = $cb_object->($o) if ($cb_object);
Branch: 1 branch
BranchTrueFalseTotal
if $cb_object 4 1 5
1327 5 ▶ if (@val == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@val == 0) { } 3 2 5
elsif (@val == 1) { } 2 0 2
1328 3 return $o;
1329 }
1330 elsif (@val == 1) {
1331 2 return $val[0];
1332 }
1333 else {
1334 0 Carp::croak("filter_json_object callbacks must not return more than one scalar");
1335 }
1336 }
1337  
1338  
1339 sub PP_decode_box {
1340 {
1341 0 ▶ text => $text,
Subroutine PP_decode_box: not called CC=1 SCAR=6.9
Pod undocumented
1342 at => $at,
1343 ch => $ch,
1344 len => $len,
1345 depth => $depth,
1346 encoding => $encoding,
1347 is_valid_utf8 => $is_valid_utf8,
1348 };
1349 }
1350  
1351 } # PARSE
1352  
1353  
1354 47 sub _decode_surrogates { # from perlunicode
1355 1127735 ▶ my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00);
Subroutine _decode_surrogates: called CC=1 SCAR=0.0
1356 1127735 my $un = pack('U*', $uni);
1357 1127735 utf8::encode( $un );
1358 1127735 return $un;
1359 }
1360  
1361  
1362 sub _decode_unicode {
1363 2312509 ▶ my $un = pack('U', hex shift);
Subroutine _decode_unicode: called CC=1 SCAR=0.0
1364 2312509 utf8::encode( $un );
1365 2312509 return $un;
1366 }
1367  
1368 #
1369 # Setup for various Perl versions (the code from JSON::PP58)
1370 #
1371  
1372 BEGIN {
1373  
1374 47 ▶ unless ( defined &utf8::is_utf8 ) {
Subroutine BEGIN: called CC=4 SCAR=17.2
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &utf8::is_utf8) 0 47 47
1375 0 require Encode;
1376 0 *utf8::is_utf8 = *Encode::is_utf8;
1377 }
1378  
1379 47 if ( !OLD_PERL ) {
1380 47 *JSON::PP::JSON_PP_encode_ascii = \&_encode_ascii;
1381 47 *JSON::PP::JSON_PP_encode_latin1 = \&_encode_latin1;
1382 47 *JSON::PP::JSON_PP_decode_surrogates = \&_decode_surrogates;
1383 47 *JSON::PP::JSON_PP_decode_unicode = \&_decode_unicode;
1384  
1385 47 ▶ if ($] < 5.008003) { # join() in 5.8.0 - 5.8.2 is broken.
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.008003") 0 47 47
1386 package JSON::PP;
1387 0 require subs;
1388 0 subs->import('join');
1389 0 eval q|
1390 sub join {
1391 return '' if (@_ < 2);
1392 my $j = shift;
1393 my $str = shift;
1394 for (@_) { $str .= $j . $_; }
1395 return $str;
1396 }
1397 |;
1398 }
1399 }
1400  
1401  
1402 sub JSON::PP::incr_parse {
1403 714 ▶ local $Carp::CarpLevel = 1;
Subroutine incr_parse: called CC=2 SCAR=7.1
Pod documented
1404 714 ▶ ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_parse( @_ );
Condition66%2/3
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
l!l&&r!l&&!r
686280
MC/DC0%0/2
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
$_[0]{"_incr_parser"} "JSON::PP::IncrParser"->new
Truth table66%2/3
A ||= B
A= $_[0]{"_incr_parser"}B= "JSON::PP::IncrParser"->new
ABresult
1X1
011
000
1405 }
1406  
1407  
1408 sub JSON::PP::incr_skip {
1409 2 ▶ ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_skip;
Subroutine incr_skip: called CC=2 SCAR=9.2
Pod documented
Condition33%1/3
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
l!l&&r!l&&!r
200
MC/DC0%0/2
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
$_[0]{"_incr_parser"} "JSON::PP::IncrParser"->new
Truth table33%1/3
A ||= B
A= $_[0]{"_incr_parser"}B= "JSON::PP::IncrParser"->new
ABresult
1X1
011
000
1410 }
1411  
1412  
1413 sub JSON::PP::incr_reset {
1414 0 ▶ ( $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new )->incr_reset;
Subroutine incr_reset: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
$_[0]{"_incr_parser"} "JSON::PP::IncrParser"->new
Truth table0%0/3
A ||= B
A= $_[0]{"_incr_parser"}B= "JSON::PP::IncrParser"->new
ABresult
1X1
011
000
1415 }
1416  
1417 47 ▶ eval q{
Subroutine incr_text: called CC=3 SCAR=13.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $] >= "5.006" 47 0 47
if ($_[0]{"_incr_parser"}{"incr_pos"}) 0 304 304
Condition33%1/3
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
l!l&&r!l&&!r
30400
MC/DC0%0/2
$_[0]{"_incr_parser"} ||= "JSON::PP::IncrParser"->new
$_[0]{"_incr_parser"} "JSON::PP::IncrParser"->new
Truth table33%1/3
A ||= B
A= $_[0]{"_incr_parser"}B= "JSON::PP::IncrParser"->new
ABresult
1X1
011
000
1418 sub JSON::PP::incr_text : lvalue {
1419 $_[0]->{_incr_parser} ||= JSON::PP::IncrParser->new;
1420  
1421 if ( $_[0]->{_incr_parser}->{incr_pos} ) {
1422 Carp::croak("incr_text cannot be called when the incremental parser already started parsing");
1423 }
1424 $_[0]->{_incr_parser}->{incr_text};
1425 }
1426 } if ( $] >= 5.006 );
1427  
1428 } # Setup for various Perl versions (the code from JSON::PP58)
1429  
1430  
1431 ###############################
1432 # Utilities
1433 #
1434  
1435 BEGIN {
1436 47 ▶ eval 'require Scalar::Util';
Subroutine BEGIN: called CC=2 SCAR=15.0
1437 47 ▶ unless($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 47 47
1438 47 *JSON::PP::blessed = \&Scalar::Util::blessed;
1439 47 *JSON::PP::reftype = \&Scalar::Util::reftype;
1440 47 *JSON::PP::refaddr = \&Scalar::Util::refaddr;
1441 }
1442 else{ # This code is from Scalar::Util.
1443 # warn $@;
1444 0 eval 'sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }';
1445 *JSON::PP::blessed = sub {
1446 0 ▶ local($@, $SIG{__DIE__}, $SIG{__WARN__});
Subroutine __ANON__: not called CC=2 SCAR=17.9
1447 0 ▶ ref($_[0]) ? eval { $_[0]->a_sub_not_likely_to_be_here } : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
1448 0 };
1449 0 require B;
1450 0 my %tmap = qw(
1451 B::NULL SCALAR
1452 B::HV HASH
1453 B::AV ARRAY
1454 B::CV CODE
1455 B::IO IO
1456 B::GV GLOB
1457 B::REGEXP REGEXP
1458 );
1459 *JSON::PP::reftype = sub {
1460 0 ▶ my $r = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
1461  
1462 0 ▶ return undef unless length(ref($r));
Branch: 1 branch
BranchTrueFalseTotal
unless length ref $r 0 0 0
1463  
1464 0 my $t = ref(B::svref_2object($r));
1465  
1466 return
1467 0 ▶ exists $tmap{$t} ? $tmap{$t}
Branch: 2 branches
BranchTrueFalseTotal
exists $tmap{$t} ? : 0 0 0
length ref $$r ? : 0 0 0
1468 : length(ref($$r)) ? 'REF'
1469 : 'SCALAR';
1470 0 };
1471 *JSON::PP::refaddr = sub {
1472 0 ▶ return undef unless length(ref($_[0]));
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless length ref $_[0] 0 0 0
1473  
1474 0 my $addr;
1475 0 ▶ if(defined(my $pkg = blessed($_[0]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pkg = &blessed($_[0]))) { } 0 0 0
1476 0 $addr .= bless $_[0], 'Scalar::Util::Fake';
1477 0 bless $_[0], $pkg;
1478 }
1479 else {
1480 0 $addr .= $_[0]
1481 }
1482  
1483 0 $addr =~ /0x(\w+)/;
1484 0 local $^W;
1485 #no warnings 'portable';
1486 0 hex($1);
1487 }
1488 0 }
1489 }
1490  
1491  
1492 # shamelessly copied and modified from JSON::XS code.
1493  
1494 47 $JSON::PP::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" };
1495 47 $JSON::PP::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" };
1496  
1497 5 ▶sub is_bool { blessed $_[0] and ( $_[0]->isa("JSON::PP::Boolean") or $_[0]->isa("Types::Serialiser::BooleanBase") or $_[0]->isa("JSON::XS::Boolean") ); }
Subroutine is_bool: called CC=4 SCAR=16.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("JSON::PP::Boolean") || $_[0]->isa("Types::Serialiser::BooleanBase") || $_[0]->isa("JSON::XS::Boolean") 2 3 5
Condition44%4/9
builtin::blessed($_[0]) && ($_[0]->isa('JSON::PP::Boolean') || $_[0]->isa('Types::Serialiser::BooleanBase') || $_[0]->isa('JSON::XS::Boolean'))
!ll&&!rl&&r
302
$_[0]->isa("JSON::PP::Boolean") or $_[0]->isa("Types::Serialiser::BooleanBase") or $_[0]->isa("JSON::XS::Boolean")
l!l&&r!l&&!r
200
$_[0]->isa("JSON::PP::Boolean") or $_[0]->isa("Types::Serialiser::BooleanBase")
l!l&&r!l&&!r
200
MC/DC25%1/4
builtin::blessed($_[0]) && ($_[0]->isa('JSON::PP::Boolean') || $_[0]->isa('Types::Serialiser::BooleanBase') || $_[0]->isa('JSON::XS::Boolean'))
builtin::blessed($_[0]) $_[0]->isa("JSON::PP::Boolean") $_[0]->isa("Types::Serialiser::BooleanBase") $_[0]->isa("JSON::XS::Boolean")
Truth table40%2/5
A && B or C or D
A= builtin::blessed($_[0])B= $_[0]->isa("JSON::PP::Boolean")C= $_[0]->isa("Types::Serialiser::BooleanBase")D= $_[0]->isa("JSON::XS::Boolean")
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1498  
1499 3 ▶sub true { $JSON::PP::true }
Subroutine true: called CC=1 SCAR=0.0
Pod documented
1500 3 ▶sub false { $JSON::PP::false }
Subroutine false: called CC=1 SCAR=0.0
Pod documented
1501 0 ▶sub null { undef; }
Subroutine null: not called CC=1 SCAR=6.9
Pod documented
1502  
1503 ###############################
1504  
1505 package JSON::PP::IncrParser;
1506  
1507 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1508  
1509 47 ▶use constant INCR_M_WS => 0; # initial whitespace skipping
Subroutine BEGIN: called CC=1 SCAR=0.0
1510 47 ▶use constant INCR_M_STR => 1; # inside string
Subroutine BEGIN: called CC=1 SCAR=0.0
1511 47 ▶use constant INCR_M_BS => 2; # inside backslash
Subroutine BEGIN: called CC=1 SCAR=0.0
1512 47 ▶use constant INCR_M_JSON => 3; # outside anything, count nesting
Subroutine BEGIN: called CC=1 SCAR=0.0
1513 47 ▶use constant INCR_M_C0 => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1514 47 ▶use constant INCR_M_C1 => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1515 47 ▶use constant INCR_M_TFN => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
1516 47 ▶use constant INCR_M_NUM => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
1517  
1518 47 $JSON::PP::IncrParser::VERSION = '1.01';
1519  
1520 sub new {
1521 28 ▶ my ( $class ) = @_;
Subroutine new: called CC=1 SCAR=0.0
1522  
1523 28 bless {
1524 incr_nest => 0,
1525 incr_text => undef,
1526 incr_pos => 0,
1527 incr_mode => 0,
1528 }, $class;
1529 }
1530  
1531  
1532 sub incr_parse {
1533 714 ▶ my ( $self, $coder, $text ) = @_;
Subroutine incr_parse: called CC=19 SCAR=29.5
1534  
1535 714 ▶ $self->{incr_text} = '' unless ( defined $self->{incr_text} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"incr_text"} 28 686 714
1536  
1537 714 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 372 342 714
1538 372 ▶ if ( utf8::is_utf8( $text ) and !utf8::is_utf8( $self->{incr_text} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($text) and not utf8::is_utf8($self->{"incr_text"})) 1 371 372
Condition66%2/3
utf8::is_utf8($text) and not utf8::is_utf8($self->{"incr_text"})
!ll&&!rl&&r
37101
MC/DC50%1/2
utf8::is_utf8($text) and not utf8::is_utf8($self->{"incr_text"})
utf8::is_utf8($text) not utf8::is_utf8($self->{"incr_text"})
Truth table66%2/3
A and not(B)
A= utf8::is_utf8($text)B= not utf8::is_utf8($self->{"incr_text"})
ABresult
0X0
100
111
1539 1 utf8::upgrade( $self->{incr_text} ) ;
1540 1 utf8::decode( $self->{incr_text} ) ;
1541 }
1542 372 $self->{incr_text} .= $text;
1543 }
1544  
1545 714 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 353 361 714
1546 353 my $max_size = $coder->get_max_size;
1547 353 my $p = $self->{incr_pos};
1548 353 my @ret;
1549 {
1550 353 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
wantarray and do { do { unless ($self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3) { $self->_incr_parse($coder); if ($max_size and $self->{"incr_pos"} > $max_size) { Carp::croak(qq[attempted decode of JSON text of $self->{"incr_pos"} bytes size, but max_size is set to $max_size]); } ; unless ($self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3) { if ($self->{"incr_mode"} == 0 and $self->{"incr_pos"}) { $self->{"incr_pos"} = 0; $self->{"incr_text"} = ""; } ; last; } ; } ; () = $coder->PP_decode_json($self->{"incr_text"}, 1); push @ret, $obj; use bytes; $self->{"incr_text"} = substr($self->{"incr_text"}, $offset || 0); $self->{"incr_pos"} = 0; $self->{"incr_nest"} = 0; $self->{"incr_mode"} = 0; last unless wantarray } } 2 0 2
1551 355 ▶ unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3) 354 1 355
Condition100%3/3
$self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3
!ll&&!rl&&r
223321
MC/DC100%2/2
$self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3
$self->{"incr_nest"} <= 0 $self->{"incr_mode"} == 3
Truth table100%3/3
A and B
A= $self->{"incr_nest"} <= 0B= $self->{"incr_mode"} == 3
ABresult
0X0
100
111
1552 354 $self->_incr_parse( $coder );
1553  
1554 353 ▶ if ( $max_size and $self->{incr_pos} > $max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size and $self->{"incr_pos"} > $max_size) 1 352 353
Condition100%3/3
$max_size and $self->{"incr_pos"} > $max_size
!ll&&!rl&&r
35111
MC/DC100%2/2
$max_size and $self->{"incr_pos"} > $max_size
$max_size $self->{"incr_pos"} > $max_size
Truth table100%3/3
A and B
A= $max_sizeB= $self->{"incr_pos"} > $max_size
ABresult
0X0
100
111
1555 1 Carp::croak("attempted decode of JSON text of $self->{incr_pos} bytes size, but max_size is set to $max_size");
1556 }
1557 352 ▶ unless ( $self->{incr_nest} <= 0 and $self->{incr_mode} == INCR_M_JSON ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3) 67 285 352
Condition100%3/3
$self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3
!ll&&!rl&&r
2245285
MC/DC100%2/2
$self->{"incr_nest"} <= 0 and $self->{"incr_mode"} == 3
$self->{"incr_nest"} <= 0 $self->{"incr_mode"} == 3
Truth table100%3/3
A and B
A= $self->{"incr_nest"} <= 0B= $self->{"incr_mode"} == 3
ABresult
0X0
100
111
1558 # as an optimisation, do not accumulate white space in the incr buffer
1559 67 ▶ if ( $self->{incr_mode} == INCR_M_WS and $self->{incr_pos} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"incr_mode"} == 0 and $self->{"incr_pos"}) 4 63 67
Condition100%3/3
$self->{"incr_mode"} == 0 and $self->{"incr_pos"}
!ll&&!rl&&r
24394
MC/DC100%2/2
$self->{"incr_mode"} == 0 and $self->{"incr_pos"}
$self->{"incr_mode"} == 0 $self->{"incr_pos"}
Truth table100%3/3
A and B
A= $self->{"incr_mode"} == 0B= $self->{"incr_pos"}
ABresult
0X0
100
111
1560 4 $self->{incr_pos} = 0;
1561 4 $self->{incr_text} = '';
1562 }
1563 67 last;
1564 }
1565 }
1566  
1567 286 my ($obj, $offset) = $coder->PP_decode_json( $self->{incr_text}, 0x00000001 );
1568 282 push @ret, $obj;
1569 47 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
1570 282 ▶ $self->{incr_text} = substr( $self->{incr_text}, $offset || 0 );
Condition50%1/2
$offset || 0
l!l
2820
MC/DC0%0/1
$offset || 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
1571 282 $self->{incr_pos} = 0;
1572 282 $self->{incr_nest} = 0;
1573 282 $self->{incr_mode} = 0;
1574 282 ▶ last unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 280 2 282
1575 } while ( wantarray );
1576 }
1577  
1578 347 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 343 347
1579 4 return @ret;
1580 }
1581 else { # in scalar context
1582 343 ▶ return defined $ret[0] ? $ret[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ret[0] ? : 280 63 343
1583 }
1584 }
1585 }
1586  
1587  
1588 sub _incr_parse {
1589 354 ▶ my ($self, $coder) = @_;
Subroutine _incr_parse: called CC=53 SCAR=41.1
1590 354 my $text = $self->{incr_text};
1591 354 my $len = length $text;
1592 354 my $p = $self->{incr_pos};
1593  
1594 INCR_PARSE:
1595 354 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 324 67 391
1596 2927 my $s = substr( $text, $p, 1 );
1597 2927 ▶ last INCR_PARSE unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 2927 2927
1598 2927 my $mode = $self->{incr_mode};
1599  
1600 2927 ▶ if ( $mode == INCR_M_WS ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($mode == 0) { } 297 2630 2927
elsif ($mode == 2) { } 0 2630 2630
elsif ($mode == 4 or $mode == 5) { } 9 2621 2630
elsif ($mode == 6) { } 35 2586 2621
elsif ($mode == 7) { } 391 2195 2586
elsif ($mode == 1) { } 765 1430 2195
elsif ($mode == 3) { } 1430 0 1430
1601 297 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 550 4 554
1602 550 $s = substr( $text, $p, 1 );
1603 550 ▶ last INCR_PARSE unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 550 550
1604 550 ▶ if ( ord($s) > 0x20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $s > 32) 293 257 550
1605 293 ▶ if ( $s eq '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq '#') { } 6 287 293
1606 6 $self->{incr_mode} = INCR_M_C0;
1607 6 redo INCR_PARSE;
1608 } else {
1609 287 $self->{incr_mode} = INCR_M_JSON;
1610 287 redo INCR_PARSE;
1611 }
1612 }
1613 257 $p++;
1614 }
1615 } elsif ( $mode == INCR_M_BS ) {
1616 0 $p++;
1617 0 $self->{incr_mode} = INCR_M_STR;
1618 0 redo INCR_PARSE;
1619 ▶ } elsif ( $mode == INCR_M_C0 or $mode == INCR_M_C1 ) {
Condition100%3/3
$mode == 4 or $mode == 5
l!l&&r!l&&!r
632621
MC/DC100%2/2
$mode == 4 or $mode == 5
$mode == 4 $mode == 5
Truth table100%3/3
A or B
A= $mode == 4B= $mode == 5
ABresult
1X1
011
000
1620 9 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 45 0 45
1621 45 $s = substr( $text, $p, 1 );
1622 45 ▶ last INCR_PARSE unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 45 45
1623 45 ▶ if ( $s eq "\n" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq "\n") 9 36 45
1624 9 ▶ $self->{incr_mode} = $self->{incr_mode} == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'incr_mode'} == 4 ? : 6 3 9
1625 9 last;
1626 }
1627 36 $p++;
1628 }
1629 9 next;
1630 } elsif ( $mode == INCR_M_TFN ) {
1631 35 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 140 0 140
1632 140 $s = substr( $text, $p++, 1 );
1633 140 ▶ next if defined $s and $s =~ /[rueals]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s and $s =~ /[rueals]/ 105 35 140
Condition66%2/3
defined $s and $s =~ /[rueals]/
!ll&&!rl&&r
035105
MC/DC50%1/2
defined $s and $s =~ /[rueals]/
defined $s $s =~ /[rueals]/
Truth table66%2/3
A and B
A= defined $sB= $s =~ /[rueals]/
ABresult
0X0
100
111
1634 35 last;
1635 }
1636 35 $p--;
1637 35 $self->{incr_mode} = INCR_M_JSON;
1638  
1639 35 ▶ last INCR_PARSE unless $self->{incr_nest};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"incr_nest"} 0 35 35
1640 35 redo INCR_PARSE;
1641 } elsif ( $mode == INCR_M_NUM ) {
1642 391 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 479 5 484
1643 479 $s = substr( $text, $p++, 1 );
1644 479 ▶ next if defined $s and $s =~ /[0-9eE.+\-]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s and $s =~ /[0-9eE.+\-]/ 93 386 479
Condition66%2/3
defined $s and $s =~ /[0-9eE.+\-]/
!ll&&!rl&&r
038693
MC/DC50%1/2
defined $s and $s =~ /[0-9eE.+\-]/
defined $s $s =~ /[0-9eE.+\-]/
Truth table66%2/3
A and B
A= defined $sB= $s =~ /[0-9eE.+\-]/
ABresult
0X0
100
111
1645 386 last;
1646 }
1647 391 $p--;
1648 391 $self->{incr_mode} = INCR_M_JSON;
1649  
1650 391 ▶ last INCR_PARSE unless $self->{incr_nest};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"incr_nest"} 16 375 391
1651 375 redo INCR_PARSE;
1652 } elsif ( $mode == INCR_M_STR ) {
1653 765 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 84285 22 84307
1654 84285 $s = substr( $text, $p, 1 );
1655 84285 ▶ last INCR_PARSE unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 84285 84285
1656 84285 ▶ if ( $s eq '"' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s eq '"') { } 743 83542 84285
elsif ($s eq '\\') { } 498 83044 83542
1657 743 $p++;
1658 743 $self->{incr_mode} = INCR_M_JSON;
1659  
1660 743 ▶ last INCR_PARSE unless $self->{incr_nest};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"incr_nest"} 15 728 743
1661 728 redo INCR_PARSE;
1662 }
1663 elsif ( $s eq '\\' ) {
1664 498 $p++;
1665 498 ▶ if ( !defined substr($text, $p, 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined substr($text, $p, 1)) 0 498 498
1666 0 $self->{incr_mode} = INCR_M_BS;
1667 0 last INCR_PARSE;
1668 }
1669 }
1670 83542 $p++;
1671 }
1672 } elsif ( $mode == INCR_M_JSON ) {
1673 1430 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 3459 2 3461
1674 3459 $s = substr( $text, $p++, 1 );
1675 3459 ▶ if ( $s eq "\x00" ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($s eq "\000") { } 0 3459 3459
elsif ($s eq "\t" or $s eq "\n" or $s eq "\r" or $s eq ' ') { } 682 2777 3459
elsif ($s eq 't' or $s eq 'f' or $s eq 'n') { } 35 2742 2777
elsif ($s =~ /^[0-9\-]$/) { } 391 2351 2742
elsif ($s eq '"') { } 743 1608 2351
elsif ($s eq '[' or $s eq '{') { } 357 1251 1608
elsif ($s eq ']' or $s eq '}') { } 354 897 1251
elsif ($s eq '#') { } 3 894 897
1676 0 $p--;
1677 0 last INCR_PARSE;
1678 ▶ } elsif ( $s eq "\x09" or $s eq "\x0a" or $s eq "\x0d" or $s eq "\x20" ) {
Condition77%7/9
$s eq "\t" or $s eq "\n" or $s eq "\r" or $s eq " "
l!l&&r!l&&!r
36792777
$s eq "\t" or $s eq "\n" or $s eq "\r"
l!l&&r!l&&!r
303456
$s eq "\t" or $s eq "\n"
l!l&&r!l&&!r
033456
MC/DC50%2/4
$s eq "\t" or $s eq "\n" or $s eq "\r" or $s eq " "
$s eq "\t" $s eq "\n" $s eq "\r" $s eq " "
Truth table60%3/5
A or B or C or D
A= $s eq "\t"B= $s eq "\n"C= $s eq "\r"D= $s eq " "
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1679 682 ▶ if ( !$self->{incr_nest} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"incr_nest"}) 0 682 682
1680 0 $p--; # do not eat the whitespace, let the next round do it
1681 0 last INCR_PARSE;
1682 }
1683 682 next;
1684 ▶ } elsif ( $s eq 't' or $s eq 'f' or $s eq 'n' ) {
Condition50%3/6
$s eq "t" or $s eq "f" or $s eq "n"
l!l&&r!l&&!r
0352742
$s eq "t" or $s eq "f"
l!l&&r!l&&!r
002777
MC/DC33%1/3
$s eq "t" or $s eq "f" or $s eq "n"
$s eq "t" $s eq "f" $s eq "n"
Truth table50%2/4
A or B or C
A= $s eq "t"B= $s eq "f"C= $s eq "n"
ABCresult
1XX1
01X1
0011
0000
1685 35 $self->{incr_mode} = INCR_M_TFN;
1686 35 redo INCR_PARSE;
1687 } elsif ( $s =~ /^[0-9\-]$/ ) {
1688 391 $self->{incr_mode} = INCR_M_NUM;
1689 391 redo INCR_PARSE;
1690 } elsif ( $s eq '"' ) {
1691 743 $self->{incr_mode} = INCR_M_STR;
1692 743 redo INCR_PARSE;
1693 ▶ } elsif ( $s eq '[' or $s eq '{' ) {
Condition100%3/3
$s eq "[" or $s eq "{"
l!l&&r!l&&!r
1861711251
MC/DC100%2/2
$s eq "[" or $s eq "{"
$s eq "[" $s eq "{"
Truth table100%3/3
A or B
A= $s eq "["B= $s eq "{"
ABresult
1X1
011
000
1694 357 ▶ if ( ++$self->{incr_nest} > $coder->get_max_depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"incr_nest"} > $coder->get_max_depth) 1 356 357
1695 1 Carp::croak('json text or perl structure exceeds maximum nesting level (max_depth set too low?)');
1696 }
1697 356 next;
1698 ▶ } elsif ( $s eq ']' or $s eq '}' ) {
Condition100%3/3
$s eq "]" or $s eq "}"
l!l&&r!l&&!r
183171897
MC/DC100%2/2
$s eq "]" or $s eq "}"
$s eq "]" $s eq "}"
Truth table100%3/3
A or B
A= $s eq "]"B= $s eq "}"
ABresult
1X1
011
000
1699 354 ▶ if ( --$self->{incr_nest} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"incr_nest"} <= 0) 255 99 354
1700 255 last INCR_PARSE;
1701 }
1702 } elsif ( $s eq '#' ) {
1703 3 $self->{incr_mode} = INCR_M_C1;
1704 3 redo INCR_PARSE;
1705 }
1706 }
1707 }
1708 }
1709  
1710 353 $self->{incr_pos} = $p;
1711 353 ▶ $self->{incr_parsing} = $p ? 1 : 0; # for backward compatibility
Branch: 1 branch
BranchTrueFalseTotal
$p ? : 314 39 353
1712 }
1713  
1714  
1715 sub incr_text {
1716 0 ▶ if ( $_[0]->{incr_pos} ) {
Subroutine incr_text: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"incr_pos"}) 0 0 0
1717 0 Carp::croak("incr_text cannot be called when the incremental parser already started parsing");
1718 }
1719 0 $_[0]->{incr_text};
1720 }
1721  
1722  
1723 sub incr_skip {
1724 2 ▶ my $self = shift;
Subroutine incr_skip: called CC=1 SCAR=0.0
1725 2 $self->{incr_text} = substr( $self->{incr_text}, $self->{incr_pos} );
1726 2 $self->{incr_pos} = 0;
1727 2 $self->{incr_mode} = 0;
1728 2 $self->{incr_nest} = 0;
1729 }
1730  
1731  
1732 sub incr_reset {
1733 0 ▶ my $self = shift;
Subroutine incr_reset: not called CC=1 SCAR=6.9
1734 0 $self->{incr_text} = undef;
1735 0 $self->{incr_pos} = 0;
1736 0 $self->{incr_mode} = 0;
1737 0 $self->{incr_nest} = 0;
1738 }
1739  
1740 ###############################
1741  
1742  
1743 47 1;
1744 __END__
↑ Summary PP/Boolean.pm »