JSON/backportPP.pm

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