JSON/PP.pm

filter: Statementstmt 20.8% 176 / 843 Branchbran 1.2% 7 / 578 Conditioncond 0.0% 0 / 218 MC/DCmcdc 0.0% 0 / 133 Subroutinesub 26.2% 38 / 145 Podpod 43.3% 36 / 83 totaltotal 12.8% 257 / 2000 CC 368 SCAR 55.9
CC
368
Cov
11%
number
1406.0
string
702.0
PP_decode_json
589.6
CRAP 266.6
↑ Summary JSON.pm »
1 package JSON::PP;
2  
3 # JSON-2.0
4  
5 3 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 #use Devel::Peek;
13  
14 3 $JSON::PP::VERSION = '2.27008';
15  
16 3 @JSON::PP::EXPORT = qw(encode_json decode_json from_json to_json);
17  
18 # instead of hash-access, i tried index-access for speed.
19 # but this method is not faster than what i expected. so it will be changed.
20  
21 3 ▶use constant P_ASCII => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use constant P_LATIN1 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use constant P_UTF8 => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use constant P_INDENT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use constant P_CANONICAL => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use constant P_SPACE_BEFORE => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use constant P_SPACE_AFTER => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use constant P_ALLOW_NONREF => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use constant P_SHRINK => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use constant P_ALLOW_BLESSED => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use constant P_CONVERT_BLESSED => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use constant P_RELAXED => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 3 ▶use constant P_LOOSE => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use constant P_ALLOW_BIGNUM => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use constant P_ALLOW_BAREKEY => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use constant P_ALLOW_SINGLEQUOTE => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use constant P_ESCAPE_SLASH => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use constant P_AS_NONBLESSED => 17;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 3 ▶use constant P_ALLOW_UNKNOWN => 18;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 3 ▶use constant OLD_PERL => $] < 5.008 ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.008 ? : 0 3 3
44  
45 BEGIN {
46 3 ▶ my @xs_compati_bit_properties = qw(
Subroutine BEGIN: called CC=5 SCAR=33.0
47 latin1 ascii utf8 indent canonical space_before space_after allow_nonref shrink
48 allow_blessed convert_blessed relaxed allow_unknown
49 );
50 3 my @pp_bit_properties = qw(
51 allow_singlequote allow_bignum loose
52 allow_barekey escape_slash as_nonblessed
53 );
54  
55 # Perl version check, Unicode handling is enable?
56 # Helper module sets @JSON::PP::_properties.
57  
58 3 ▶ my $helper = $] >= 5.008 ? 'JSON::PP58'
Branch: 2 branches
BranchTrueFalseTotal
$] >= 5.008 ? : 3 0 3
$] >= 5.006 ? : 0 0 0
59 : $] >= 5.006 ? 'JSON::PP56'
60 : 'JSON::PP5005'
61 ;
62  
63 3 eval qq| require $helper |;
64 3 ▶ if ($@) { Carp::croak $@; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
65  
66 3 for my $name (@xs_compati_bit_properties, @pp_bit_properties) {
67 57 my $flag_name = 'P_' . uc($name);
68  
69 57 ▶ eval qq/
Subroutine allow_barekey: not called CC=3 SCAR=24.8
Subroutine allow_bignum: not called CC=3 SCAR=24.8
Subroutine allow_blessed: not called CC=3 SCAR=24.8
Subroutine allow_nonref: not called CC=3 SCAR=24.8
Subroutine allow_singlequote: not called CC=3 SCAR=24.8
Subroutine allow_unknown: not called CC=3 SCAR=24.8
Subroutine as_nonblessed: not called CC=3 SCAR=24.8
Subroutine ascii: not called CC=3 SCAR=24.8
Subroutine canonical: not called CC=3 SCAR=24.8
Subroutine convert_blessed: not called CC=3 SCAR=24.8
Subroutine escape_slash: not called CC=3 SCAR=24.8
Subroutine get_allow_barekey: not called CC=2 SCAR=17.8
Subroutine get_allow_bignum: not called CC=2 SCAR=17.8
Subroutine get_allow_blessed: not called CC=2 SCAR=17.8
Subroutine get_allow_nonref: not called CC=2 SCAR=17.8
Subroutine get_allow_singlequote: not called CC=2 SCAR=17.8
Subroutine get_allow_unknown: not called CC=2 SCAR=17.8
Subroutine get_as_nonblessed: not called CC=2 SCAR=17.8
Subroutine get_ascii: not called CC=2 SCAR=17.8
Subroutine get_canonical: not called CC=2 SCAR=17.8
Subroutine get_convert_blessed: not called CC=2 SCAR=17.8
Subroutine get_escape_slash: not called CC=2 SCAR=17.8
Subroutine get_indent: not called CC=2 SCAR=17.8
Subroutine get_latin1: not called CC=2 SCAR=17.8
Subroutine get_loose: not called CC=2 SCAR=17.8
Subroutine get_relaxed: not called CC=2 SCAR=17.8
Subroutine get_shrink: not called CC=2 SCAR=17.8
Subroutine get_space_after: not called CC=2 SCAR=17.8
Subroutine get_space_before: not called CC=2 SCAR=17.8
Subroutine get_utf8: not called CC=2 SCAR=17.8
Subroutine indent: not called CC=3 SCAR=24.8
Subroutine latin1: not called CC=3 SCAR=24.8
Subroutine loose: not called CC=3 SCAR=24.8
Subroutine relaxed: not called CC=3 SCAR=24.8
Subroutine shrink: not called CC=3 SCAR=24.8
Subroutine space_after: not called CC=3 SCAR=24.8
Subroutine space_before: not called CC=3 SCAR=24.8
Subroutine utf8: not called CC=3 SCAR=24.8
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 documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 57 branches
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
$_[0]{'PROPS'}[14] ? : 0 0 0
$_[0]{'PROPS'}[13] ? : 0 0 0
$_[0]{'PROPS'}[9] ? : 0 0 0
$_[0]{'PROPS'}[7] ? : 0 0 0
$_[0]{'PROPS'}[15] ? : 0 0 0
$_[0]{'PROPS'}[18] ? : 0 0 0
$_[0]{'PROPS'}[17] ? : 0 0 0
$_[0]{'PROPS'}[0] ? : 0 0 0
$_[0]{'PROPS'}[4] ? : 0 0 0
$_[0]{'PROPS'}[10] ? : 0 0 0
$_[0]{'PROPS'}[16] ? : 0 0 0
$_[0]{'PROPS'}[3] ? : 0 0 0
$_[0]{'PROPS'}[1] ? : 0 0 0
$_[0]{'PROPS'}[12] ? : 0 0 0
$_[0]{'PROPS'}[11] ? : 0 0 0
$_[0]{'PROPS'}[8] ? : 0 0 0
$_[0]{'PROPS'}[6] ? : 0 0 0
$_[0]{'PROPS'}[5] ? : 0 0 0
$_[0]{'PROPS'}[2] ? : 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
defined $_[1] ? : 0 0 0
if ($enable) { } 0 0 0
70 sub $name {
71 my \$enable = defined \$_[1] ? \$_[1] : 1;
72  
73 if (\$enable) {
74 \$_[0]->{PROPS}->[$flag_name] = 1;
75 }
76 else {
77 \$_[0]->{PROPS}->[$flag_name] = 0;
78 }
79  
80 \$_[0];
81 }
82  
83 sub get_$name {
84 \$_[0]->{PROPS}->[$flag_name] ? 1 : '';
85 }
86 /;
87 }
88  
89 }
90  
91  
92  
93 # Functions
94  
95 my %encode_allow_method
96 3 = map {($_ => 1)} qw/utf8 pretty allow_nonref latin1 self_encode escape_slash
97 allow_blessed convert_blessed indent indent_length allow_bignum
98 as_nonblessed
99 /;
100 my %decode_allow_method
101 3 = map {($_ => 1)} qw/utf8 allow_nonref loose allow_singlequote allow_bignum
102 allow_barekey max_size relaxed/;
103  
104  
105 3 my $JSON; # cache
106  
107 sub encode_json ($) { # encode
108 0 ▶ ($JSON ||= __PACKAGE__->new->utf8)->encode(@_);
Subroutine encode_json: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$JSON ||= __PACKAGE__->new->utf8
l!l&&r!l&&!r
000
MC/DC0%0/2
$JSON ||= __PACKAGE__->new->utf8
$JSON __PACKAGE__->new->utf8
Truth table0%0/3
A ||= B
A= $JSONB= __PACKAGE__->new->utf8
ABresult
1X1
011
000
109 }
110  
111  
112 sub decode_json { # decode
113 0 ▶ ($JSON ||= __PACKAGE__->new->utf8)->decode(@_);
Subroutine decode_json: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$JSON ||= __PACKAGE__->new->utf8
l!l&&r!l&&!r
000
MC/DC0%0/2
$JSON ||= __PACKAGE__->new->utf8
$JSON __PACKAGE__->new->utf8
Truth table0%0/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 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
132 my $self = {
133 max_depth => 512,
134 max_size => 0,
135 indent => 0,
136 FLAGS => 0,
137 0 ▶ fallback => sub { encode_error('Invalid value. JSON can only reference.') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 indent_length => 3,
139 };
140  
141 0 bless $self, $class;
142 }
143  
144  
145 sub encode {
146 0 ▶ return $_[0]->PP_encode_json($_[1]);
Subroutine encode: not called CC=1 SCAR=6.9
Pod documented
147 }
148  
149  
150 sub decode {
151 0 ▶ return $_[0]->PP_decode_json($_[1], 0x00000000);
Subroutine decode: not called CC=1 SCAR=6.9
Pod documented
152 }
153  
154  
155 sub decode_prefix {
156 0 ▶ return $_[0]->PP_decode_json($_[1], 0x00000001);
Subroutine decode_prefix: not called CC=1 SCAR=6.9
Pod documented
157 }
158  
159  
160 # accessor
161  
162  
163 # pretty printing
164  
165 sub pretty {
166 0 ▶ my ($self, $v) = @_;
Subroutine pretty: not called CC=3 SCAR=24.8
Pod documented
167 0 ▶ my $enable = defined $v ? $v : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
168  
169 0 ▶ if ($enable) { # indent_length(3) for JSON::XS compatibility
Branch: 1 branch
BranchTrueFalseTotal
if ($enable) { } 0 0 0
170 0 $self->indent(1)->indent_length(3)->space_before(1)->space_after(1);
171 }
172 else {
173 0 $self->indent(0)->space_before(0)->space_after(0);
174 }
175  
176 0 $self;
177 }
178  
179 # etc
180  
181 sub max_depth {
182 0 ▶ my $max = defined $_[1] ? $_[1] : 0x80000000;
Subroutine max_depth: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
183 0 $_[0]->{max_depth} = $max;
184 0 $_[0];
185 }
186  
187  
188 0 ▶sub get_max_depth { $_[0]->{max_depth}; }
Subroutine get_max_depth: not called CC=1 SCAR=6.9
Pod undocumented
189  
190  
191 sub max_size {
192 0 ▶ my $max = defined $_[1] ? $_[1] : 0;
Subroutine max_size: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
193 0 $_[0]->{max_size} = $max;
194 0 $_[0];
195 }
196  
197  
198 0 ▶sub get_max_size { $_[0]->{max_size}; }
Subroutine get_max_size: not called CC=1 SCAR=6.9
Pod undocumented
199  
200  
201 sub filter_json_object {
202 0 ▶ $_[0]->{cb_object} = defined $_[1] ? $_[1] : 0;
Subroutine filter_json_object: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
203 0 ▶ $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'cb_object'} || $_[0]{'cb_sk_object'} ? : 0 0 0
Condition0%0/3
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
$_[0]{"cb_object"} $_[0]{"cb_sk_object"}
Truth table0%0/3
A or B
A= $_[0]{"cb_object"}B= $_[0]{"cb_sk_object"}
ABresult
1X1
011
000
204 0 $_[0];
205 }
206  
207 sub filter_json_single_key_object {
208 0 ▶ if (@_ > 1) {
Subroutine filter_json_single_key_object: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
209 0 $_[0]->{cb_sk_object}->{$_[1]} = $_[2];
210 }
211 0 ▶ $_[0]->{F_HOOK} = ($_[0]->{cb_object} or $_[0]->{cb_sk_object}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'cb_object'} || $_[0]{'cb_sk_object'} ? : 0 0 0
Condition0%0/3
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"cb_object"} or $_[0]{"cb_sk_object"}
$_[0]{"cb_object"} $_[0]{"cb_sk_object"}
Truth table0%0/3
A or B
A= $_[0]{"cb_object"}B= $_[0]{"cb_sk_object"}
ABresult
1X1
011
000
212 0 $_[0];
213 }
214  
215 sub indent_length {
216 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
217 0 Carp::carp "The acceptable range of indent_length() is 0 to 15.";
218 }
219 else {
220 0 $_[0]->{indent_length} = $_[1];
221 }
222 0 $_[0];
223 }
224  
225 sub get_indent_length {
226 0 ▶ $_[0]->{indent_length};
Subroutine get_indent_length: not called CC=1 SCAR=6.9
Pod undocumented
227 }
228  
229 sub sort_by {
230 0 ▶ $_[0]->{sort_by} = defined $_[1] ? $_[1] : 1;
Subroutine sort_by: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
231 0 $_[0];
232 }
233  
234 sub allow_bigint {
235 0 ▶ Carp::carp("allow_bigint() is obsoleted. use allow_bignum() insted.");
Subroutine allow_bigint: not called CC=1 SCAR=6.9
Pod undocumented
236 }
237  
238 ###############################
239  
240 ###
241 ### Perl => JSON
242 ###
243  
244  
245 { # Convert
246  
247 3 my $max_depth;
248 my $indent;
249 3 my $ascii;
250 3 my $latin1;
251 3 my $utf8;
252 3 my $space_before;
253 3 my $space_after;
254 3 my $canonical;
255 3 my $allow_blessed;
256 3 my $convert_blessed;
257  
258 3 my $indent_length;
259 3 my $escape_slash;
260 3 my $bignum;
261 3 my $as_nonblessed;
262  
263 3 my $depth;
264 3 my $indent_count;
265 3 my $keysort;
266  
267  
268 sub PP_encode_json {
269 0 ▶ my $self = shift;
Subroutine PP_encode_json: not called CC=12 SCAR=50.5
Pod undocumented
270 0 my $obj = shift;
271  
272 0 $indent_count = 0;
273 0 $depth = 0;
274  
275 0 my $idx = $self->{PROPS};
276  
277 ($ascii, $latin1, $utf8, $indent, $canonical, $space_before, $space_after, $allow_blessed,
278 $convert_blessed, $escape_slash, $bignum, $as_nonblessed)
279 0 = @{$idx}[P_ASCII .. P_SPACE_AFTER, P_ALLOW_BLESSED, P_CONVERT_BLESSED,
280 P_ESCAPE_SLASH, P_ALLOW_BIGNUM, P_AS_NONBLESSED];
281  
282 0 ($max_depth, $indent_length) = @{$self}{qw/max_depth indent_length/};
283  
284 0 ▶ $keysort = $canonical ? sub { $a cmp $b } : undef;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$canonical ? : 0 0 0
285  
286 0 ▶ if ($self->{sort_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sort_by"}) 0 0 0
287 $keysort = ref($self->{sort_by}) eq 'CODE' ? $self->{sort_by}
288 : $self->{sort_by} =~ /\D+/ ? $self->{sort_by}
289 0 ▶ : sub { $a cmp $b };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
ref $self->{'sort_by'} eq 'CODE' ? : 0 0 0
$self->{'sort_by'} =~ /\D+/ ? : 0 0 0
290 }
291  
292 0 ▶ encode_error("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj or $idx->[7] 0 0 0
Condition0%0/3
ref $obj or $idx->[7]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $obj or $idx->[7]
ref $obj $idx->[7]
Truth table0%0/3
A or B
A= ref $objB= $idx->[7]
ABresult
1X1
011
000
293 if(!ref $obj and !$idx->[ P_ALLOW_NONREF ]);
294  
295 0 my $str = $self->object_to_json($obj);
296  
297 0 ▶ $str .= "\n" if ( $indent ); # JSON::XS 2.26 compatible
Branch: 1 branch
BranchTrueFalseTotal
if $indent 0 0 0
298  
299 0 ▶ unless ($ascii or $latin1 or $utf8) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ascii or $latin1 or $utf8) 0 0 0
Condition0%0/6
$ascii or $latin1 or $utf8
l!l&&r!l&&!r
000
$ascii or $latin1
l!l&&r!l&&!r
000
MC/DC0%0/3
$ascii or $latin1 or $utf8
$ascii $latin1 $utf8
Truth table0%0/4
A or B or C
A= $asciiB= $latin1C= $utf8
ABCresult
1XX1
01X1
0011
0000
300 0 utf8::upgrade($str);
301 }
302  
303 0 ▶ if ($idx->[ P_SHRINK ]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx->[8]) 0 0 0
304 0 utf8::downgrade($str, 1);
305 }
306  
307 0 return $str;
308 }
309  
310  
311 sub object_to_json {
312 0 ▶ my ($self, $obj) = @_;
Subroutine object_to_json: not called CC=16 SCAR=56.1
Pod undocumented
313 0 my $type = ref($obj);
314  
315 0 ▶ if($type eq 'HASH'){
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type) { } 0 0 0
316 0 return $self->hash_to_json($obj);
317 }
318 elsif($type eq 'ARRAY'){
319 0 return $self->array_to_json($obj);
320 }
321 elsif ($type) { # blessed object?
322 0 ▶ if (blessed($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($obj)) { } 0 0 0
323  
324 0 ▶ return $self->value_to_json($obj) if ( $obj->isa('JSON::PP::Boolean') );
Branch: 1 branch
BranchTrueFalseTotal
if $obj->isa("JSON::PP::Boolean") 0 0 0
325  
326 0 ▶ if ( $convert_blessed and $obj->can('TO_JSON') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($convert_blessed and $obj->can("TO_JSON")) 0 0 0
Condition0%0/3
$convert_blessed and $obj->can("TO_JSON")
!ll&&!rl&&r
000
MC/DC0%0/2
$convert_blessed and $obj->can("TO_JSON")
$convert_blessed $obj->can("TO_JSON")
Truth table0%0/3
A and B
A= $convert_blessedB= $obj->can("TO_JSON")
ABresult
0X0
100
111
327 0 my $result = $obj->TO_JSON();
328 0 ▶ if ( defined $result and overload::Overloaded( $obj ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and overload::Overloaded($obj)) 0 0 0
Condition0%0/3
defined $result and overload::Overloaded($obj)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $result and overload::Overloaded($obj)
defined $result overload::Overloaded($obj)
Truth table0%0/3
A and B
A= defined $resultB= overload::Overloaded($obj)
ABresult
0X0
100
111
329 0 ▶ if ( overload::StrVal( $obj ) eq $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (overload::StrVal($obj) eq $result) 0 0 0
330 0 encode_error( sprintf(
331 "%s::TO_JSON method returned same object as was passed instead of a new one",
332 ref $obj
333 ) );
334 }
335 }
336  
337 0 return $self->object_to_json( $result );
338 }
339  
340 0 ▶ return "$obj" if ( $bignum and _is_bignum($obj) );
Branch: 1 branch
BranchTrueFalseTotal
if $bignum and _is_bignum($obj) 0 0 0
Condition0%0/3
$bignum and _is_bignum($obj)
!ll&&!rl&&r
000
MC/DC0%0/2
$bignum and _is_bignum($obj)
$bignum _is_bignum($obj)
Truth table0%0/3
A and B
A= $bignumB= _is_bignum($obj)
ABresult
0X0
100
111
341 0 ▶ return $self->blessed_to_json($obj) if ($allow_blessed and $as_nonblessed); # will be removed.
Branch: 1 branch
BranchTrueFalseTotal
if $allow_blessed and $as_nonblessed 0 0 0
Condition0%0/3
$allow_blessed and $as_nonblessed
!ll&&!rl&&r
000
MC/DC0%0/2
$allow_blessed and $as_nonblessed
$allow_blessed $as_nonblessed
Truth table0%0/3
A and B
A= $allow_blessedB= $as_nonblessed
ABresult
0X0
100
111
342  
343 0 ▶ encode_error( sprintf("encountered object '%s', but neither allow_blessed "
Branch: 1 branch
BranchTrueFalseTotal
unless $allow_blessed 0 0 0
344 . "nor convert_blessed settings are enabled", $obj)
345 ) unless ($allow_blessed);
346  
347 0 return 'null';
348 }
349 else {
350 0 return $self->value_to_json($obj);
351 }
352 }
353 else{
354 0 return $self->value_to_json($obj);
355 }
356 }
357  
358  
359 sub hash_to_json {
360 0 ▶ my ($self, $obj) = @_;
Subroutine hash_to_json: not called CC=10 SCAR=47.0
Pod undocumented
361 0 my @res;
362  
363 0 ▶ encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)")
Branch: 1 branch
BranchTrueFalseTotal
if ++$depth > $max_depth 0 0 0
364 if (++$depth > $max_depth);
365  
366 0 ▶ my ($pre, $post) = $indent ? $self->_up_indent() : ('', '');
Branch: 1 branch
BranchTrueFalseTotal
$indent ? : 0 0 0
367 0 ▶ my $del = ($space_before ? ' ' : '') . ':' . ($space_after ? ' ' : '');
Branch: 2 branches
BranchTrueFalseTotal
$space_before ? : 0 0 0
$space_after ? : 0 0 0
368  
369 0 for my $k ( _sort( $obj ) ) {
370 0 if ( OLD_PERL ) { utf8::decode($k) } # key for Perl 5.6 / be optimized
371 push @res, string_to_json( $self, $k )
372 . $del
373 0 ▶ . ( $self->object_to_json( $obj->{$k} ) || $self->value_to_json( $obj->{$k} ) );
Condition0%0/3
$self->object_to_json($obj->{$k}) || $self->value_to_json($obj->{$k})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->object_to_json($obj->{$k}) || $self->value_to_json($obj->{$k})
$self->object_to_json($obj->{$k}) $self->value_to_json($obj->{$k})
Truth table0%0/3
A || B
A= $self->object_to_json($obj->{$k})B= $self->value_to_json($obj->{$k})
ABresult
1X1
011
000
374 }
375  
376 0 --$depth;
377 0 ▶ $self->_down_indent() if ($indent);
Branch: 1 branch
BranchTrueFalseTotal
if $indent 0 0 0
378  
379 0 ▶ return '{' . ( @res ? $pre : '' ) . ( @res ? join( ",$pre", @res ) . $post : '' ) . '}';
Branch: 2 branches
BranchTrueFalseTotal
@res ? : 0 0 0
@res ? : 0 0 0
380 }
381  
382  
383 sub array_to_json {
384 0 ▶ my ($self, $obj) = @_;
Subroutine array_to_json: not called CC=8 SCAR=42.8
Pod undocumented
385 0 my @res;
386  
387 0 ▶ encode_error("json text or perl structure exceeds maximum nesting level (max_depth set too low?)")
Branch: 1 branch
BranchTrueFalseTotal
if ++$depth > $max_depth 0 0 0
388 if (++$depth > $max_depth);
389  
390 0 ▶ my ($pre, $post) = $indent ? $self->_up_indent() : ('', '');
Branch: 1 branch
BranchTrueFalseTotal
$indent ? : 0 0 0
391  
392 0 for my $v (@$obj){
393 0 ▶ push @res, $self->object_to_json($v) || $self->value_to_json($v);
Condition0%0/3
$self->object_to_json($v) || $self->value_to_json($v)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->object_to_json($v) || $self->value_to_json($v)
$self->object_to_json($v) $self->value_to_json($v)
Truth table0%0/3
A || B
A= $self->object_to_json($v)B= $self->value_to_json($v)
ABresult
1X1
011
000
394 }
395  
396 0 --$depth;
397 0 ▶ $self->_down_indent() if ($indent);
Branch: 1 branch
BranchTrueFalseTotal
if $indent 0 0 0
398  
399 0 ▶ return '[' . ( @res ? $pre : '' ) . ( @res ? join( ",$pre", @res ) . $post : '' ) . ']';
Branch: 2 branches
BranchTrueFalseTotal
@res ? : 0 0 0
@res ? : 0 0 0
400 }
401  
402  
403 sub value_to_json {
404 0 ▶ my ($self, $value) = @_;
Subroutine value_to_json: not called CC=20 SCAR=60.4
Pod undocumented
405  
406 0 ▶ return 'null' if(!defined $value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
407  
408 0 my $b_obj = B::svref_2object(\$value); # for round trip problem
409 0 my $flags = $b_obj->FLAGS;
410  
411 0 ▶ return $value # as is
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 12288 and not $flags & 16384 0 0 0
Condition0%0/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
000
MC/DC0%0/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table0%0/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
412 if $flags & ( B::SVp_IOK | B::SVp_NOK ) and !( $flags & B::SVp_POK ); # SvTYPE is IV or NV?
413  
414 0 my $type = ref($value);
415  
416 0 ▶ if(!$type){
Branch: 3 branches
BranchTrueFalseTotal
if (not $type) { } 0 0 0
elsif (&blessed($value) and $value->isa('JSON::PP::Boolean')) { } 0 0 0
elsif ($type) { } 0 0 0
417 0 return string_to_json($self, $value);
418 }
419 ▶ elsif( blessed($value) and $value->isa('JSON::PP::Boolean') ){
Condition0%0/3
&blessed($value) and $value->isa("JSON::PP::Boolean")
!ll&&!rl&&r
000
MC/DC0%0/2
&blessed($value) and $value->isa("JSON::PP::Boolean")
&blessed($value) $value->isa("JSON::PP::Boolean")
Truth table0%0/3
A and B
A= &blessed($value)B= $value->isa("JSON::PP::Boolean")
ABresult
0X0
100
111
420 0 ▶ return $$value == 1 ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$$value == 1 ? : 0 0 0
421 }
422 elsif ($type) {
423 0 ▶ if ((overload::StrVal($value) =~ /=(\w+)/)[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ((overload::StrVal($value) =~ /=(\w+)/)[0]) 0 0 0
424 0 return $self->value_to_json("$value");
425 }
426  
427 0 ▶ if ($type eq 'SCALAR' and defined $$value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" and defined $$value) 0 0 0
Condition0%0/3
$type eq "SCALAR" and defined $$value
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "SCALAR" and defined $$value
$type eq "SCALAR" defined $$value
Truth table0%0/3
A and B
A= $type eq "SCALAR"B= defined $$value
ABresult
0X0
100
111
428 return $$value eq '1' ? 'true'
429 : $$value eq '0' ? 'false'
430 0 ▶ : $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ? 'null'
Branch: 3 branches
BranchTrueFalseTotal
$$value eq '1' ? : 0 0 0
$$value eq '0' ? : 0 0 0
$self->{'PROPS'}[18] ? : 0 0 0
431 : encode_error("cannot encode reference to scalar");
432 }
433  
434 0 ▶ if ( $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'PROPS'}[18]) { } 0 0 0
435 0 return 'null';
436 }
437 else {
438 0 ▶ if ( $type eq 'SCALAR' or $type eq 'REF' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'SCALAR' or $type eq 'REF') { } 0 0 0
Condition0%0/3
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
Truth table0%0/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
439 0 encode_error("cannot encode reference to scalar");
440 }
441 else {
442 0 encode_error("encountered $value, but JSON can only represent references to arrays or hashes");
443 }
444 }
445  
446 }
447 else {
448 return $self->{fallback}->($value)
449 0 ▶ if ($self->{fallback} and ref($self->{fallback}) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fallback"} and ref $self->{"fallback"} eq "CODE" 0 0 0
Condition0%0/3
$self->{"fallback"} and ref $self->{"fallback"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"fallback"} and ref $self->{"fallback"} eq "CODE"
$self->{"fallback"} ref $self->{"fallback"} eq "CODE"
Truth table0%0/3
A and B
A= $self->{"fallback"}B= ref $self->{"fallback"} eq "CODE"
ABresult
0X0
100
111
450 0 return 'null';
451 }
452  
453 }
454  
455  
456 3 my %esc = (
457 "\n" => '\n',
458 "\r" => '\r',
459 "\t" => '\t',
460 "\f" => '\f',
461 "\b" => '\b',
462 "\"" => '\"',
463 "\\" => '\\\\',
464 "\'" => '\\\'',
465 );
466  
467  
468 sub string_to_json {
469 0 ▶ my ($self, $arg) = @_;
Subroutine string_to_json: not called CC=5 SCAR=34.0
Pod undocumented
470  
471 0 $arg =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g;
472 0 ▶ $arg =~ s/\//\\\//g if ($escape_slash);
Branch: 1 branch
BranchTrueFalseTotal
if $escape_slash 0 0 0
473 0 $arg =~ s/([\x00-\x08\x0b\x0e-\x1f])/'\\u00' . unpack('H2', $1)/eg;
474  
475 0 ▶ if ($ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ascii) 0 0 0
476 0 $arg = JSON_PP_encode_ascii($arg);
477 }
478  
479 0 ▶ if ($latin1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($latin1) 0 0 0
480 0 $arg = JSON_PP_encode_latin1($arg);
481 }
482  
483 0 ▶ if ($utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) 0 0 0
484 0 utf8::encode($arg);
485 }
486  
487 0 return '"' . $arg . '"';
488 }
489  
490  
491 sub blessed_to_json {
492 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
493 0 ▶ if ($reftype eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 0 0 0
elsif ($reftype eq 'ARRAY') { } 0 0 0
494 0 return $_[0]->hash_to_json($_[1]);
495 }
496 elsif ($reftype eq 'ARRAY') {
497 0 return $_[0]->array_to_json($_[1]);
498 }
499 else {
500 0 return 'null';
501 }
502 }
503  
504  
505 sub encode_error {
506 0 ▶ my $error = shift;
Subroutine encode_error: not called CC=1 SCAR=6.9
Pod undocumented
507 0 Carp::croak "$error";
508 }
509  
510  
511 sub _sort {
512 0 ▶ defined $keysort ? (sort $keysort (keys %{$_[0]})) : keys %{$_[0]};
Subroutine _sort: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $keysort ? : 0 0 0
513 }
514  
515  
516 sub _up_indent {
517 0 ▶ my $self = shift;
Subroutine _up_indent: not called CC=1 SCAR=6.9
518 0 my $space = ' ' x $indent_length;
519  
520 0 my ($pre,$post) = ('','');
521  
522 0 $post = "\n" . $space x $indent_count;
523  
524 0 $indent_count++;
525  
526 0 $pre = "\n" . $space x $indent_count;
527  
528 0 return ($pre,$post);
529 }
530  
531  
532 0 ▶ sub _down_indent { $indent_count--; }
Subroutine _down_indent: not called CC=1 SCAR=6.9
533  
534  
535 sub PP_encode_box {
536 {
537 0 ▶ depth => $depth,
Subroutine PP_encode_box: not called CC=1 SCAR=6.9
Pod documented
538 indent_count => $indent_count,
539 };
540 }
541  
542 } # Convert
543  
544  
545 3 sub _encode_ascii {
546 join('',
547 map {
548 0 ▶ $_ <= 127 ?
Subroutine _encode_ascii: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
$_ <= 127 ? : 0 0 0
$_ <= 65535 ? : 0 0 0
549 chr($_) :
550 $_ <= 65535 ?
551 sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_));
552 } unpack('U*', $_[0])
553 );
554 }
555  
556  
557 sub _encode_latin1 {
558 join('',
559 map {
560 0 ▶ $_ <= 255 ?
Subroutine _encode_latin1: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
$_ <= 255 ? : 0 0 0
$_ <= 65535 ? : 0 0 0
561 chr($_) :
562 $_ <= 65535 ?
563 sprintf('\u%04x', $_) : sprintf('\u%x\u%x', _encode_surrogates($_));
564 } unpack('U*', $_[0])
565 );
566 }
567  
568  
569 sub _encode_surrogates { # from perlunicode
570 0 ▶ my $uni = $_[0] - 0x10000;
Subroutine _encode_surrogates: not called CC=1 SCAR=6.9
571 0 return ($uni / 0x400 + 0xD800, $uni % 0x400 + 0xDC00);
572 }
573  
574  
575 sub _is_bignum {
576 0 ▶ $_[0]->isa('Math::BigInt') or $_[0]->isa('Math::BigFloat');
Subroutine _is_bignum: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->isa("Math::BigInt") or $_[0]->isa("Math::BigFloat") 0 0 0
577 }
578  
579  
580  
581 #
582 # JSON => Perl
583 #
584  
585 3 my $max_intsize;
586  
587 BEGIN {
588 3 ▶ my $checkint = 1111;
Subroutine BEGIN: called CC=3 SCAR=11.0
589 3 for my $d (5..64) {
590 51 $checkint .= 1;
591 51 my $int = eval qq| $checkint |;
592 51 ▶ if ($int =~ /[eE]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int =~ /[eE]/) 3 48 51
593 3 $max_intsize = $d - 1;
594 3 last;
595 }
596 }
597 }
598  
599 { # PARSE
600  
601 3 my %escapes = ( # by Jeremy Muhlich <jmuhlich [at] bitflood.org>
602 b => "\x8",
603 t => "\x9",
604 n => "\xA",
605 f => "\xC",
606 r => "\xD",
607 '\\' => '\\',
608 '"' => '"',
609 '/' => '/',
610 );
611  
612 3 my $text; # json data
613 my $at; # offset
614 3 my $ch; # 1chracter
615 3 my $len; # text length (changed according to UTF8 or NON UTF8)
616 # INTERNAL
617 3 my $depth; # nest counter
618 3 my $encoding; # json text encoding
619 3 my $is_valid_utf8; # temp variable
620 3 my $utf8_len; # utf8 byte length
621 # FLAGS
622 3 my $utf8; # must be utf8
623 3 my $max_depth; # max nest nubmer of objects and arrays
624 3 my $max_size;
625 3 my $relaxed;
626 3 my $cb_object;
627 3 my $cb_sk_object;
628  
629 3 my $F_HOOK;
630  
631 3 my $allow_bigint; # using Math::BigInt
632 3 my $singlequote; # loosely quoting
633 3 my $loose; #
634 3 my $allow_barekey; # bareKey
635  
636 # $opt flag
637 # 0x00000001 .... decode_prefix
638 # 0x10000000 .... incr_parse
639  
640 sub PP_decode_json {
641 0 ▶ my ($self, $opt); # $opt is an effective flag during this decode_json.
Subroutine PP_decode_json: not called CC=25 SCAR=63.8
Pod undocumented
642  
643 0 ($self, $text, $opt) = @_;
644  
645 0 ($at, $ch, $depth) = (0, '', 0);
646  
647 0 ▶ if ( !defined $text or ref $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $text or ref $text) 0 0 0
Condition0%0/3
not defined $text or ref $text
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $text or ref $text
not defined $text ref $text
Truth table0%0/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
648 0 decode_error("malformed JSON string, neither array, object, number, string or atom");
649 }
650  
651 0 my $idx = $self->{PROPS};
652  
653 ($utf8, $relaxed, $loose, $allow_bigint, $allow_barekey, $singlequote)
654 0 = @{$idx}[P_UTF8, P_RELAXED, P_LOOSE .. P_ALLOW_SINGLEQUOTE];
655  
656 0 ▶ if ( $utf8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) { } 0 0 0
657 0 ▶ 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 0 0
658 }
659 else {
660 0 utf8::upgrade( $text );
661 }
662  
663 0 $len = length $text;
664  
665 ($max_depth, $max_size, $cb_object, $cb_sk_object, $F_HOOK)
666 0 = @{$self}{qw/max_depth max_size cb_object cb_sk_object F_HOOK/};
667  
668 0 ▶ if ($max_size > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size > 1) 0 0 0
669 3 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
670 0 my $bytes = length $text;
671 0 ▶ decode_error(
Branch: 1 branch
BranchTrueFalseTotal
if $bytes > $max_size 0 0 0
672 sprintf("attempted decode of JSON text of %s bytes size, but max_size is set to %s"
673 , $bytes, $max_size), 1
674 ) if ($bytes > $max_size);
675 }
676  
677 # Currently no effect
678 # should use regexp
679 0 my @octets = unpack('C4', $text);
680 0 ▶ $encoding = ( $octets[0] and $octets[1]) ? 'UTF-8'
Branch: 5 branches
BranchTrueFalseTotal
$octets[0] && $octets[1] ? : 0 0 0
!$octets[0] && $octets[1] ? : 0 0 0
!($octets[0] || $octets[1]) ? : 0 0 0
$octets[2] ? : 0 0 0
!$octets[2] ? : 0 0 0
Condition0%0/9
$octets[0] and $octets[1]
!ll&&!rl&&r
000
not $octets[0] and $octets[1]
!ll&&!rl&&r
000
$octets[0] || $octets[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$octets[0] and $octets[1]
$octets[0] $octets[1]
MC/DC0%0/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 table0%0/3
A and B
A= $octets[0]B= $octets[1]
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $octets[0]B= $octets[1]
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $octets[0]B= $octets[1]
ABresult
1X1
011
000
681 : (!$octets[0] and $octets[1]) ? 'UTF-16BE'
682 : (!$octets[0] and !$octets[1]) ? 'UTF-32BE'
683 : ( $octets[2] ) ? 'UTF-16LE'
684 : (!$octets[2] ) ? 'UTF-32LE'
685 : 'unknown';
686  
687 0 white(); # remove head white space
688  
689 0 my $valid_start = defined $ch; # Is there a first character for JSON structure?
690  
691 0 my $result = value();
692  
693 0 ▶ return undef if ( !$result && ( $opt & 0x10000000 ) ); # for incr_parse
Branch: 1 branch
BranchTrueFalseTotal
if not $result and $opt & 268435456 0 0 0
Condition0%0/3
not $result and $opt & 268435456
!ll&&!rl&&r
000
MC/DC0%0/2
not $result and $opt & 268435456
not $result $opt & 268435456
Truth table0%0/3
not(A) and B
A= not $resultB= $opt & 268435456
ABresult
0X0
100
111
694  
695 0 ▶ decode_error("malformed JSON string, neither array, object, number, string or atom") unless $valid_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid_start 0 0 0
696  
697 0 ▶ if ( !$idx->[ P_ALLOW_NONREF ] and !ref $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($idx->[7] or ref $result) 0 0 0
Condition0%0/3
$idx->[7] or ref $result
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx->[7] or ref $result
$idx->[7] ref $result
Truth table0%0/3
A or B
A= $idx->[7]B= ref $result
ABresult
1X1
011
000
698 0 decode_error(
699 'JSON text must be an object or array (but found number, string, true, false or null,'
700 . ' use allow_nonref to allow this)', 1);
701 }
702  
703 0 ▶ Carp::croak('something wrong.') if $len < $at; # we won't arrive here.
Branch: 1 branch
BranchTrueFalseTotal
if $len < $at 0 0 0
704  
705 0 ▶ my $consumed = defined $ch ? $at - 1 : $at; # consumed JSON text length
Branch: 1 branch
BranchTrueFalseTotal
defined $ch ? : 0 0 0
706  
707 0 white(); # remove tail white space
708  
709 0 ▶ if ( $ch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch) 0 0 0
710 0 ▶ return ( $result, $consumed ) if ($opt & 0x00000001); # all right if decode_prefix
Branch: 1 branch
BranchTrueFalseTotal
if $opt & 1 0 0 0
711 0 decode_error("garbage after JSON object");
712 }
713  
714 0 ▶ ( $opt & 0x00000001 ) ? ( $result, $consumed ) : $result;
Branch: 1 branch
BranchTrueFalseTotal
$opt & 1 ? : 0 0 0
715 }
716  
717  
718 sub next_chr {
719 0 ▶ return $ch = undef if($at >= $len);
Subroutine next_chr: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $at >= $len 0 0 0
720 0 $ch = substr($text, $at++, 1);
721 }
722  
723  
724 sub value {
725 0 ▶ white();
Subroutine value: not called CC=9 SCAR=45.0
Pod undocumented
726 0 ▶ return if(!defined $ch);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ch 0 0 0
727 0 ▶ return object() if($ch eq '{');
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "{" 0 0 0
728 0 ▶ return array() if($ch eq '[');
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "[" 0 0 0
729 0 ▶ return string() if($ch eq '"' or ($singlequote and $ch eq "'"));
Branch: 1 branch
BranchTrueFalseTotal
if $ch eq "\"" or $singlequote and $ch eq "'" 0 0 0
Condition0%0/6
$ch eq "\"" or $singlequote and $ch eq "'"
l!l&&r!l&&!r
000
$singlequote and $ch eq "'"
!ll&&!rl&&r
000
MC/DC0%0/3
$ch eq "\"" or $singlequote and $ch eq "'"
$ch eq "\"" $singlequote $ch eq "'"
Truth table0%0/4
A or B and C
A= $ch eq "\""B= $singlequoteC= $ch eq "'"
ABCresult
1XX1
0111
00X0
0100
730 0 ▶ return number() if($ch =~ /[0-9]/ or $ch eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $ch =~ /[0-9]/ or $ch eq "-" 0 0 0
Condition0%0/3
$ch =~ /[0-9]/ or $ch eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ch =~ /[0-9]/ or $ch eq "-"
$ch =~ /[0-9]/ $ch eq "-"
Truth table0%0/3
A or B
A= $ch =~ /[0-9]/B= $ch eq "-"
ABresult
1X1
011
000
731 0 return word();
732 }
733  
734 sub string {
735 0 ▶ my ($i, $s, $t, $u);
Subroutine string: not called CC=26 SCAR=65.5
Pod undocumented
736 0 my $utf16;
737 0 my $is_utf8;
738  
739 0 ($is_valid_utf8, $utf8_len) = ('', 0);
740  
741 0 $s = ''; # basically UTF8 flag on
742  
743 0 ▶ if($ch eq '"' or ($singlequote and $ch eq "'")){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" or $singlequote and $ch eq "'") 0 0 0
Condition0%0/6
$ch eq "\"" or $singlequote and $ch eq "'"
l!l&&r!l&&!r
000
$singlequote and $ch eq "'"
!ll&&!rl&&r
000
MC/DC0%0/3
$ch eq "\"" or $singlequote and $ch eq "'"
$ch eq "\"" $singlequote $ch eq "'"
Truth table0%0/4
A or B and C
A= $ch eq "\""B= $singlequoteC= $ch eq "'"
ABCresult
1XX1
0111
00X0
0100
744 0 my $boundChar = $ch;
745  
746 0 ▶ OUTER: while( defined(next_chr()) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined next_chr()) 0 0 0
747  
748 0 ▶ if($ch eq $boundChar){
Branch: 2 branches
BranchTrueFalseTotal
if ($ch eq $boundChar) { } 0 0 0
elsif ($ch eq '\\') { } 0 0 0
749 0 next_chr();
750  
751 0 ▶ if ($utf16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf16) 0 0 0
752 0 decode_error("missing low surrogate character in surrogate pair");
753 }
754  
755 0 ▶ utf8::decode($s) if($is_utf8);
Branch: 1 branch
BranchTrueFalseTotal
if $is_utf8 0 0 0
756  
757 0 return $s;
758 }
759 elsif($ch eq '\\'){
760 0 next_chr();
761 0 ▶ if(exists $escapes{$ch}){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $escapes{$ch}) { } 0 0 0
elsif ($ch eq 'u') { } 0 0 0
762 0 $s .= $escapes{$ch};
763 }
764 elsif($ch eq 'u'){ # UNICODE handling
765 0 my $u = '';
766  
767 0 for(1..4){
768 0 $ch = next_chr();
769 0 ▶ last OUTER if($ch !~ /[0-9a-fA-F]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $ch =~ /[0-9a-fA-F]/ 0 0 0
770 0 $u .= $ch;
771 }
772  
773 # U+D800 - U+DBFF
774 0 ▶ 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}/) { } 0 0 0
elsif ($u =~ /^[dD][c-fC-F][0-9a-fA-F]{2}/) { } 0 0 0
775 0 $utf16 = $u;
776 }
777 # U+DC00 - U+DFFF
778 elsif ($u =~ /^[dD][c-fC-F][0-9a-fA-F]{2}/) { # UTF-16 low surrogate?
779 0 ▶ unless (defined $utf16) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $utf16) 0 0 0
780 0 decode_error("missing high surrogate character in surrogate pair");
781 }
782 0 $is_utf8 = 1;
783 0 ▶ $s .= JSON_PP_decode_surrogates($utf16, $u) || next;
Condition0%0/2
JSON_PP_decode_surrogates($utf16, $u) || (next)
l!l
00
MC/DC0%0/1
JSON_PP_decode_surrogates($utf16, $u) || (next)
JSON_PP_decode_surrogates($utf16, $u)
Truth table0%0/2
A
A= JSON_PP_decode_surrogates($utf16, $u)
Aresult
11
00
784 0 $utf16 = undef;
785 }
786 else {
787 0 ▶ if (defined $utf16) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $utf16) 0 0 0
788 0 decode_error("surrogate pair expected");
789 }
790  
791 0 ▶ if ( ( my $hex = hex( $u ) ) > 127 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $hex = hex $u) > 127) { } 0 0 0
792 0 $is_utf8 = 1;
793 0 ▶ $s .= JSON_PP_decode_unicode($u) || next;
Condition0%0/2
JSON_PP_decode_unicode($u) || (next)
l!l
00
MC/DC0%0/1
JSON_PP_decode_unicode($u) || (next)
JSON_PP_decode_unicode($u)
Truth table0%0/2
A
A= JSON_PP_decode_unicode($u)
Aresult
11
00
794 }
795 else {
796 0 $s .= chr $hex;
797 }
798 }
799  
800 }
801 else{
802 0 ▶ unless ($loose) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loose) 0 0 0
803 0 $at -= 2;
804 0 decode_error('illegal backslash escape sequence in string');
805 }
806 0 $s .= $ch;
807 }
808 }
809 else{
810  
811 0 ▶ if ( ord $ch > 127 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $ch > 127) 0 0 0
812 0 ▶ if ( $utf8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) { } 0 0 0
813 0 ▶ unless( $ch = is_valid_utf8($ch) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch = is_valid_utf8($ch)) { } 0 0 0
814 0 $at -= 1;
815 0 decode_error("malformed UTF-8 character in JSON string");
816 }
817 else {
818 0 $at += $utf8_len - 1;
819 }
820 }
821 else {
822 0 utf8::encode( $ch );
823 }
824  
825 0 $is_utf8 = 1;
826 }
827  
828 0 ▶ if (!$loose) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loose) 0 0 0
829 0 ▶ if ($ch =~ /[\x00-\x1f\x22\x5c]/) { # '/' ok
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[\x00-\x1f\x22\x5c]/) 0 0 0
830 0 $at--;
831 0 decode_error('invalid character encountered while parsing JSON string');
832 }
833 }
834  
835 0 $s .= $ch;
836 }
837 }
838 }
839  
840 0 decode_error("unexpected end of string while parsing JSON string");
841 }
842  
843  
844 sub white {
845 0 ▶ while( defined $ch ){
Subroutine white: not called CC=17 SCAR=57.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) 0 0 0
846 0 ▶ if($ch le ' '){
Branch: 2 branches
BranchTrueFalseTotal
if ($ch le ' ') { } 0 0 0
elsif ($ch eq '/') { } 0 0 0
847 0 next_chr();
848 }
849 elsif($ch eq '/'){
850 0 next_chr();
851 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
852 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
853 }
854 ▶ 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
855 0 next_chr();
856 0 while(1){
857 0 ▶ if(defined $ch){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) { } 0 0 0
858 0 ▶ if($ch eq '*'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq '*') { } 0 0 0
859 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
860 0 next_chr();
861 0 last;
862 }
863 }
864 else{
865 0 next_chr();
866 }
867 }
868 else{
869 0 decode_error("Unterminated comment");
870 }
871 }
872 0 next;
873 }
874 else{
875 0 $at--;
876 0 decode_error("malformed JSON string, neither array, object, number, string or atom");
877 }
878 }
879 else{
880 0 ▶ if ($relaxed and $ch eq '#') { # correctly?
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed and $ch eq "#") 0 0 0
Condition0%0/3
$relaxed and $ch eq "#"
!ll&&!rl&&r
000
MC/DC0%0/2
$relaxed and $ch eq "#"
$relaxed $ch eq "#"
Truth table0%0/3
A and B
A= $relaxedB= $ch eq "#"
ABresult
0X0
100
111
881 0 pos($text) = $at;
882 0 $text =~ /\G([^\n]*(?:\r\n|\r|\n|$))/g;
883 0 $at = pos($text);
884 0 next_chr;
885 0 next;
886 }
887  
888 0 last;
889 }
890 }
891 }
892  
893  
894 sub array {
895 0 ▶ my $a = [];
Subroutine array: not called CC=10 SCAR=47.0
Pod undocumented
896  
897 0 ▶ 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 0 0
898 if (++$depth > $max_depth);
899  
900 0 next_chr();
901 0 white();
902  
903 0 ▶ if(defined $ch and $ch eq ']'){
Branch: 1 branch
BranchTrueFalseTotal
if (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
904 0 --$depth;
905 0 next_chr();
906 0 return $a;
907 }
908 else {
909 0 ▶ while(defined($ch)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) 0 0 0
910 0 push @$a, value();
911  
912 0 white();
913  
914 0 ▶ if (!defined $ch) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ch) 0 0 0
915 0 last;
916 }
917  
918 0 ▶ if($ch eq ']'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "]") 0 0 0
919 0 --$depth;
920 0 next_chr();
921 0 return $a;
922 }
923  
924 0 ▶ if($ch ne ','){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch ne ",") 0 0 0
925 0 last;
926 }
927  
928 0 next_chr();
929 0 white();
930  
931 0 ▶ if ($relaxed and $ch eq ']') {
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed and $ch eq "]") 0 0 0
Condition0%0/3
$relaxed and $ch eq "]"
!ll&&!rl&&r
000
MC/DC0%0/2
$relaxed and $ch eq "]"
$relaxed $ch eq "]"
Truth table0%0/3
A and B
A= $relaxedB= $ch eq "]"
ABresult
0X0
100
111
932 0 --$depth;
933 0 next_chr();
934 0 return $a;
935 }
936  
937 }
938 }
939  
940 0 decode_error(", or ] expected while parsing array");
941 }
942  
943  
944 sub object {
945 0 ▶ my $o = {};
Subroutine object: not called CC=18 SCAR=58.3
Pod undocumented
946 0 my $k;
947  
948 0 ▶ 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 0 0
949 if (++$depth > $max_depth);
950 0 next_chr();
951 0 white();
952  
953 0 ▶ if(defined $ch and $ch eq '}'){
Branch: 1 branch
BranchTrueFalseTotal
if (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
954 0 --$depth;
955 0 next_chr();
956 0 ▶ if ($F_HOOK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_HOOK) 0 0 0
957 0 return _json_object_hook($o);
958 }
959 0 return $o;
960 }
961 else {
962 0 ▶ while (defined $ch) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch) 0 0 0
963 0 ▶ $k = ($allow_barekey and $ch ne '"' and $ch ne "'") ? bareKey() : string();
Branch: 1 branch
BranchTrueFalseTotal
$allow_barekey && $ch ne '"' && $ch ne q['] ? : 0 0 0
Condition0%0/6
$allow_barekey and $ch ne "\"" and $ch ne "'"
!ll&&!rl&&r
000
$allow_barekey and $ch ne "\""
!ll&&!rl&&r
000
MC/DC0%0/3
$allow_barekey and $ch ne "\"" and $ch ne "'"
$allow_barekey $ch ne "\"" $ch ne "'"
Truth table0%0/4
A and B and C
A= $allow_barekeyB= $ch ne "\""C= $ch ne "'"
ABCresult
0XX0
10X0
1100
1111
964 0 white();
965  
966 0 ▶ if(!defined $ch or $ch ne ':'){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ch or $ch ne ":") 0 0 0
Condition0%0/3
not defined $ch or $ch ne ":"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ch or $ch ne ":"
not defined $ch $ch ne ":"
Truth table0%0/3
not(A) or B
A= not defined $chB= $ch ne ":"
ABresult
1X1
011
000
967 0 $at--;
968 0 decode_error("':' expected");
969 }
970  
971 0 next_chr();
972 0 $o->{$k} = value();
973 0 white();
974  
975 0 ▶ last if (!defined $ch);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ch 0 0 0
976  
977 0 ▶ if($ch eq '}'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "}") 0 0 0
978 0 --$depth;
979 0 next_chr();
980 0 ▶ if ($F_HOOK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_HOOK) 0 0 0
981 0 return _json_object_hook($o);
982 }
983 0 return $o;
984 }
985  
986 0 ▶ if($ch ne ','){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch ne ",") 0 0 0
987 0 last;
988 }
989  
990 0 next_chr();
991 0 white();
992  
993 0 ▶ if ($relaxed and $ch eq '}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($relaxed and $ch eq "}") 0 0 0
Condition0%0/3
$relaxed and $ch eq "}"
!ll&&!rl&&r
000
MC/DC0%0/2
$relaxed and $ch eq "}"
$relaxed $ch eq "}"
Truth table0%0/3
A and B
A= $relaxedB= $ch eq "}"
ABresult
0X0
100
111
994 0 --$depth;
995 0 next_chr();
996 0 ▶ if ($F_HOOK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_HOOK) 0 0 0
997 0 return _json_object_hook($o);
998 }
999 0 return $o;
1000 }
1001  
1002 }
1003  
1004 }
1005  
1006 0 $at--;
1007 0 decode_error(", or } expected while parsing object/hash");
1008 }
1009  
1010  
1011 sub bareKey { # doesn't strictly follow Standard ECMA-262 3rd Edition
1012 0 ▶ my $key;
Subroutine bareKey: not called CC=2 SCAR=17.9
Pod undocumented
1013 0 ▶ 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]/) 0 0 0
1014 0 $key .= $ch;
1015 0 next_chr();
1016 }
1017 0 return $key;
1018 }
1019  
1020  
1021 sub word {
1022 0 ▶ my $word = substr($text,$at-1,4);
Subroutine word: not called CC=8 SCAR=42.8
Pod undocumented
1023  
1024 0 ▶ if($word eq 'true'){
Branch: 3 branches
BranchTrueFalseTotal
if ($word eq 'true') { } 0 0 0
elsif ($word eq 'null') { } 0 0 0
elsif ($word eq 'fals') { } 0 0 0
1025 0 $at += 3;
1026 0 next_chr;
1027 0 return $JSON::PP::true;
1028 }
1029 elsif($word eq 'null'){
1030 0 $at += 3;
1031 0 next_chr;
1032 0 return undef;
1033 }
1034 elsif($word eq 'fals'){
1035 0 $at += 3;
1036 0 ▶ if(substr($text,$at,1) eq 'e'){
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $at, 1) eq "e") 0 0 0
1037 0 $at++;
1038 0 next_chr;
1039 0 return $JSON::PP::false;
1040 }
1041 }
1042  
1043 0 $at--; # for decode_error report
1044  
1045 0 ▶ decode_error("'null' expected") if ($word =~ /^n/);
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^n/ 0 0 0
1046 0 ▶ decode_error("'true' expected") if ($word =~ /^t/);
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^t/ 0 0 0
1047 0 ▶ decode_error("'false' expected") if ($word =~ /^f/);
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^f/ 0 0 0
1048 0 decode_error("malformed JSON string, neither array, object, number, string or atom");
1049 }
1050  
1051  
1052 sub number {
1053 0 ▶ my $n = '';
Subroutine number: not called CC=37 SCAR=72.5
Pod undocumented
1054 0 my $v;
1055  
1056 # According to RFC4627, hex or oct digts are invalid.
1057 0 ▶ if($ch eq '0'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq 0) 0 0 0
1058 0 my $peek = substr($text,$at,1);
1059 0 my $hex = $peek =~ /[xX]/; # 0 or 1
1060  
1061 0 ▶ if($hex){
Branch: 1 branch
BranchTrueFalseTotal
if ($hex) { } 0 0 0
1062 0 decode_error("malformed number (leading zero must not be followed by another digit)");
1063 0 ($n) = ( substr($text, $at+1) =~ /^([0-9a-fA-F]+)/);
1064 }
1065 else{ # oct
1066 0 ($n) = ( substr($text, $at) =~ /^([0-7]+)/);
1067 0 ▶ if (defined $n and length $n > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n and length $n > 1) 0 0 0
Condition0%0/3
defined $n and length $n > 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n and length $n > 1
defined $n length $n > 1
Truth table0%0/3
A and B
A= defined $nB= length $n > 1
ABresult
0X0
100
111
1068 0 decode_error("malformed number (leading zero must not be followed by another digit)");
1069 }
1070 }
1071  
1072 0 ▶ if(defined $n and length($n)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n and length $n) 0 0 0
Condition0%0/3
defined $n and length $n
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n and length $n
defined $n length $n
Truth table0%0/3
A and B
A= defined $nB= length $n
ABresult
0X0
100
111
1073 0 ▶ if (!$hex and length($n) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hex and length $n == 1) 0 0 0
Condition0%0/3
not $hex and length $n == 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $hex and length $n == 1
not $hex length $n == 1
Truth table0%0/3
not(A) and B
A= not $hexB= length $n == 1
ABresult
0X0
100
111
1074 0 decode_error("malformed number (leading zero must not be followed by another digit)");
1075 }
1076 0 $at += length($n) + $hex;
1077 0 next_chr;
1078 0 ▶ return $hex ? hex($n) : oct($n);
Branch: 1 branch
BranchTrueFalseTotal
$hex ? : 0 0 0
1079 }
1080 }
1081  
1082 0 ▶ if($ch eq '-'){
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "-") 0 0 0
1083 0 $n = '-';
1084 0 next_chr;
1085 0 ▶ if (!defined $ch or $ch !~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ch and $ch =~ /\d/) 0 0 0
Condition0%0/3
defined $ch and $ch =~ /\d/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ch and $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table0%0/3
A and B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1086 0 decode_error("malformed number (no digits after initial minus)");
1087 }
1088 }
1089  
1090 0 ▶ while(defined $ch and $ch =~ /\d/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ch and $ch =~ /\d/) 0 0 0
Condition0%0/3
defined $ch and $ch =~ /\d/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ch and $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table0%0/3
A and B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1091 0 $n .= $ch;
1092 0 next_chr;
1093 }
1094  
1095 0 ▶ if(defined $ch and $ch eq '.'){
Branch: 1 branch
BranchTrueFalseTotal
if (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
1096 0 $n .= '.';
1097  
1098 0 next_chr;
1099 0 ▶ if (!defined $ch or $ch !~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ch && $ch =~ /\d/) { } 0 0 0
Condition0%0/3
defined $ch && $ch =~ /\d/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ch && $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table0%0/3
A && B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1100 0 decode_error("malformed number (no digits after decimal point)");
1101 }
1102 else {
1103 0 $n .= $ch;
1104 }
1105  
1106 0 ▶ while(defined(next_chr) and $ch =~ /\d/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined next_chr() and $ch =~ /\d/) 0 0 0
Condition0%0/3
defined next_chr() and $ch =~ /\d/
!ll&&!rl&&r
000
MC/DC0%0/2
defined next_chr() and $ch =~ /\d/
defined next_chr() $ch =~ /\d/
Truth table0%0/3
A and B
A= defined next_chr()B= $ch =~ /\d/
ABresult
0X0
100
111
1107 0 $n .= $ch;
1108 }
1109 }
1110  
1111 0 ▶ 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") 0 0 0
Condition0%0/6
defined $ch and $ch eq "e" || $ch eq "E"
!ll&&!rl&&r
000
$ch eq "e" or $ch eq "E"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ch and $ch eq "e" || $ch eq "E"
defined $ch $ch eq "e" $ch eq "E"
Truth table0%0/4
A and B or C
A= defined $chB= $ch eq "e"C= $ch eq "E"
ABCresult
0XX0
1000
11X1
1011
1112 0 $n .= $ch;
1113 0 next_chr;
1114  
1115 0 ▶ if(defined($ch) and ($ch eq '+' or $ch eq '-')){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ch and $ch eq '+' || $ch eq '-') { } 0 0 0
elsif (defined $ch and $ch =~ /\d/) { } 0 0 0
Condition0%0/6
defined $ch and $ch eq "+" || $ch eq "-"
!ll&&!rl&&r
000
$ch eq "+" or $ch eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ch and $ch eq "+" || $ch eq "-"
defined $ch $ch eq "+" $ch eq "-"
Truth table0%0/4
A and B or C
A= defined $chB= $ch eq "+"C= $ch eq "-"
ABCresult
0XX0
1000
11X1
1011
1116 0 $n .= $ch;
1117 0 next_chr;
1118 0 ▶ if (!defined $ch or $ch =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ch or $ch =~ /\D/) 0 0 0
Condition0%0/3
not defined $ch or $ch =~ /\D/
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ch or $ch =~ /\D/
not defined $ch $ch =~ /\D/
Truth table0%0/3
not(A) or B
A= not defined $chB= $ch =~ /\D/
ABresult
1X1
011
000
1119 0 decode_error("malformed number (no digits after exp sign)");
1120 }
1121 0 $n .= $ch;
1122 }
1123 ▶ elsif(defined($ch) and $ch =~ /\d/){
Condition0%0/3
defined $ch and $ch =~ /\d/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ch and $ch =~ /\d/
defined $ch $ch =~ /\d/
Truth table0%0/3
A and B
A= defined $chB= $ch =~ /\d/
ABresult
0X0
100
111
1124 0 $n .= $ch;
1125 }
1126 else {
1127 0 decode_error("malformed number (no digits after exp sign)");
1128 }
1129  
1130 0 ▶ while(defined(next_chr) and $ch =~ /\d/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined next_chr() and $ch =~ /\d/) 0 0 0
Condition0%0/3
defined next_chr() and $ch =~ /\d/
!ll&&!rl&&r
000
MC/DC0%0/2
defined next_chr() and $ch =~ /\d/
defined next_chr() $ch =~ /\d/
Truth table0%0/3
A and B
A= defined next_chr()B= $ch =~ /\d/
ABresult
0X0
100
111
1131 0 $n .= $ch;
1132 }
1133  
1134 }
1135  
1136 0 $v .= $n;
1137  
1138 0 ▶ if ($v !~ /[.eE]/ and length $v > $max_intsize) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $v =~ /[.eE]/ and length $v > $max_intsize) { } 0 0 0
elsif ($allow_bigint) { } 0 0 0
Condition0%0/3
not $v =~ /[.eE]/ and length $v > $max_intsize
!ll&&!rl&&r
000
MC/DC0%0/2
not $v =~ /[.eE]/ and length $v > $max_intsize
not $v =~ /[.eE]/ length $v > $max_intsize
Truth table0%0/3
not(A) and B
A= not $v =~ /[.eE]/B= length $v > $max_intsize
ABresult
0X0
100
111
1139 0 ▶ if ($allow_bigint) { # from Adam Sussman
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_bigint) { } 0 0 0
1140 0 require Math::BigInt;
1141 0 return Math::BigInt->new($v);
1142 }
1143 else {
1144 0 return "$v";
1145 }
1146 }
1147 elsif ($allow_bigint) {
1148 0 require Math::BigFloat;
1149 0 return Math::BigFloat->new($v);
1150 }
1151  
1152 0 return 0+$v;
1153 }
1154  
1155  
1156 sub is_valid_utf8 {
1157  
1158 0 ▶ $utf8_len = $_[0] =~ /[\x00-\x7F]/ ? 1
Subroutine is_valid_utf8: not called CC=7 SCAR=40.3
Pod undocumented
Branch: 4 branches
BranchTrueFalseTotal
$_[0] =~ /[\x00-\x7F]/ ? : 0 0 0
$_[0] =~ /[\xC2-\xDF]/ ? : 0 0 0
$_[0] =~ /[\xE0-\xEF]/ ? : 0 0 0
$_[0] =~ /[\xF0-\xF4]/ ? : 0 0 0
1159 : $_[0] =~ /[\xC2-\xDF]/ ? 2
1160 : $_[0] =~ /[\xE0-\xEF]/ ? 3
1161 : $_[0] =~ /[\xF0-\xF4]/ ? 4
1162 : 0
1163 ;
1164  
1165 0 ▶ return unless $utf8_len;
Branch: 1 branch
BranchTrueFalseTotal
unless $utf8_len 0 0 0
1166  
1167 0 my $is_valid_utf8 = substr($text, $at - 1, $utf8_len);
1168  
1169 0 ▶ 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 ? : 0 0 0
1170 [\x00-\x7F]
1171 |[\xC2-\xDF][\x80-\xBF]
1172 |[\xE0][\xA0-\xBF][\x80-\xBF]
1173 |[\xE1-\xEC][\x80-\xBF][\x80-\xBF]
1174 |[\xED][\x80-\x9F][\x80-\xBF]
1175 |[\xEE-\xEF][\x80-\xBF][\x80-\xBF]
1176 |[\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF]
1177 |[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]
1178 |[\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF]
1179 )$/x ) ? $is_valid_utf8 : '';
1180 }
1181  
1182  
1183 sub decode_error {
1184 0 ▶ my $error = shift;
Subroutine decode_error: not called CC=17 SCAR=57.2
Pod undocumented
1185 0 my $no_rep = shift;
1186 0 ▶ my $str = defined $text ? substr($text, $at) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 0 0 0
1187 0 my $mess = '';
1188 0 ▶ my $type = $] >= 5.008 ? 'U*'
Branch: 3 branches
BranchTrueFalseTotal
$] >= 5.008 ? : 0 0 0
$] < 5.006 ? : 0 0 0
utf8::is_utf8($str) ? : 0 0 0
1189 : $] < 5.006 ? 'C*'
1190 : utf8::is_utf8( $str ) ? 'U*' # 5.6
1191 : 'C*'
1192 ;
1193  
1194 0 for my $c ( unpack( $type, $str ) ) { # emulate pv_uni_display() ?
1195 0 ▶ $mess .= $c == 0x07 ? '\a'
Branch: 8 branches
BranchTrueFalseTotal
$c == 7 ? : 0 0 0
$c == 9 ? : 0 0 0
$c == 10 ? : 0 0 0
$c == 13 ? : 0 0 0
$c == 12 ? : 0 0 0
$c < 32 ? : 0 0 0
$c == 92 ? : 0 0 0
$c < 128 ? : 0 0 0
1196 : $c == 0x09 ? '\t'
1197 : $c == 0x0a ? '\n'
1198 : $c == 0x0d ? '\r'
1199 : $c == 0x0c ? '\f'
1200 : $c < 0x20 ? sprintf('\x{%x}', $c)
1201 : $c == 0x5c ? '\\\\'
1202 : $c < 0x80 ? chr($c)
1203 : sprintf('\x{%x}', $c)
1204 ;
1205 0 ▶ if ( length $mess >= 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mess >= 20) 0 0 0
1206 0 $mess .= '...';
1207 0 last;
1208 }
1209 }
1210  
1211 0 ▶ unless ( length $mess ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $mess) 0 0 0
1212 0 $mess = '(end of string)';
1213 }
1214  
1215 Carp::croak (
1216 0 ▶ $no_rep ? "$error" : "$error, at character offset $at (before \"$mess\")"
Branch: 1 branch
BranchTrueFalseTotal
$no_rep ? : 0 0 0
1217 );
1218  
1219 }
1220  
1221  
1222 sub _json_object_hook {
1223 0 ▶ my $o = $_[0];
Subroutine _json_object_hook: not called CC=9 SCAR=45.0
1224 0 my @ks = keys %{$o};
1225  
1226 0 ▶ 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]}) 0 0 0
Condition0%0/9
$cb_sk_object and @ks == 1 and exists $$cb_sk_object{$ks[0]} and ref $$cb_sk_object{$ks[0]}
!ll&&!rl&&r
000
$cb_sk_object and @ks == 1 and exists $$cb_sk_object{$ks[0]}
!ll&&!rl&&r
000
$cb_sk_object and @ks == 1
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
1227 0 my @val = $cb_sk_object->{ $ks[0] }->( $o->{$ks[0]} );
1228 0 ▶ if (@val == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val == 1) 0 0 0
1229 0 return $val[0];
1230 }
1231 }
1232  
1233 0 ▶ my @val = $cb_object->($o) if ($cb_object);
Branch: 1 branch
BranchTrueFalseTotal
if $cb_object 0 0 0
1234 0 ▶ if (@val == 0 or @val > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val == 0 or @val > 1) { } 0 0 0
Condition0%0/3
@val == 0 or @val > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
@val == 0 or @val > 1
@val == 0 @val > 1
Truth table0%0/3
A or B
A= @val == 0B= @val > 1
ABresult
1X1
011
000
1235 0 return $o;
1236 }
1237 else {
1238 0 return $val[0];
1239 }
1240 }
1241  
1242  
1243 sub PP_decode_box {
1244 {
1245 0 ▶ text => $text,
Subroutine PP_decode_box: not called CC=1 SCAR=6.9
Pod documented
1246 at => $at,
1247 ch => $ch,
1248 len => $len,
1249 depth => $depth,
1250 encoding => $encoding,
1251 is_valid_utf8 => $is_valid_utf8,
1252 };
1253 }
1254  
1255 } # PARSE
1256  
1257  
1258 3 sub _decode_surrogates { # from perlunicode
1259 0 ▶ my $uni = 0x10000 + (hex($_[0]) - 0xD800) * 0x400 + (hex($_[1]) - 0xDC00);
Subroutine _decode_surrogates: not called CC=1 SCAR=6.9
1260 0 my $un = pack('U*', $uni);
1261 0 utf8::encode( $un );
1262 0 return $un;
1263 }
1264  
1265  
1266 sub _decode_unicode {
1267 0 ▶ my $un = pack('U', hex shift);
Subroutine _decode_unicode: not called CC=1 SCAR=6.9
1268 0 utf8::encode( $un );
1269 0 return $un;
1270 }
1271  
1272  
1273  
1274  
1275  
1276 ###############################
1277 # Utilities
1278 #
1279  
1280 BEGIN {
1281 3 ▶ eval 'require Scalar::Util';
Subroutine BEGIN: called CC=2 SCAR=14.0
1282 3 ▶ unless($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 3 3
1283 3 *JSON::PP::blessed = \&Scalar::Util::blessed;
1284 3 *JSON::PP::reftype = \&Scalar::Util::reftype;
1285 }
1286 else{ # This code is from Sclar::Util.
1287 # warn $@;
1288 0 eval 'sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }';
1289 *JSON::PP::blessed = sub {
1290 0 ▶ local($@, $SIG{__DIE__}, $SIG{__WARN__});
Subroutine __ANON__: not called CC=2 SCAR=17.9
1291 0 ▶ ref($_[0]) ? eval { $_[0]->a_sub_not_likely_to_be_here } : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
1292 0 };
1293 0 my %tmap = qw(
1294 B::NULL SCALAR
1295 B::HV HASH
1296 B::AV ARRAY
1297 B::CV CODE
1298 B::IO IO
1299 B::GV GLOB
1300 B::REGEXP REGEXP
1301 );
1302 *JSON::PP::reftype = sub {
1303 0 ▶ my $r = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
1304  
1305 0 ▶ return undef unless length(ref($r));
Branch: 1 branch
BranchTrueFalseTotal
unless length ref $r 0 0 0
1306  
1307 0 my $t = ref(B::svref_2object($r));
1308  
1309 return
1310 0 ▶ exists $tmap{$t} ? $tmap{$t}
Branch: 2 branches
BranchTrueFalseTotal
exists $tmap{$t} ? : 0 0 0
length ref $$r ? : 0 0 0
1311 : length(ref($$r)) ? 'REF'
1312 : 'SCALAR';
1313 0 };
1314 }
1315 }
1316  
1317  
1318 # shamely copied and modified from JSON::XS code.
1319  
1320 3 $JSON::PP::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" };
1321 3 $JSON::PP::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" };
1322  
1323 0 ▶sub is_bool { defined $_[0] and UNIVERSAL::isa($_[0], "JSON::PP::Boolean"); }
Subroutine is_bool: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and UNIVERSAL::isa($_[0], "JSON::PP::Boolean") 0 0 0
1324  
1325 0 ▶sub true { $JSON::PP::true }
Subroutine true: not called CC=1 SCAR=6.9
Pod documented
1326 0 ▶sub false { $JSON::PP::false }
Subroutine false: not called CC=1 SCAR=6.9
Pod documented
1327 0 ▶sub null { undef; }
Subroutine null: not called CC=1 SCAR=6.9
Pod documented
1328  
1329 ###############################
1330  
1331 package JSON::PP::Boolean;
1332  
1333  
1334 use overload (
1335 0 ▶ "0+" => sub { ${$_[0]} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1336 0 ▶ "++" => sub { $_[0] = ${$_[0]} + 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1337 0 ▶ "--" => sub { $_[0] = ${$_[0]} - 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1338 3 fallback => 1,
1339 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
1340  
1341  
1342 ###############################
1343  
1344 package JSON::PP::IncrParser;
1345  
1346 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1347  
1348 3 ▶use constant INCR_M_WS => 0; # initial whitespace skipping
Subroutine BEGIN: called CC=1 SCAR=0.0
1349 3 ▶use constant INCR_M_STR => 1; # inside string
Subroutine BEGIN: called CC=1 SCAR=0.0
1350 3 ▶use constant INCR_M_BS => 2; # inside backslash
Subroutine BEGIN: called CC=1 SCAR=0.0
1351 3 ▶use constant INCR_M_JSON => 3; # outside anything, count nesting
Subroutine BEGIN: called CC=1 SCAR=0.0
1352 3 ▶use constant INCR_M_C0 => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1353 3 ▶use constant INCR_M_C1 => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
1354  
1355 3 $JSON::PP::IncrParser::VERSION = '1.01';
1356  
1357 3 ▶my $unpack_format = $] < 5.006 ? 'C*' : 'U*';
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.006 ? : 0 3 3
1358  
1359 sub new {
1360 0 ▶ my ( $class ) = @_;
Subroutine new: not called CC=1 SCAR=6.9
1361  
1362 0 bless {
1363 incr_nest => 0,
1364 incr_text => undef,
1365 incr_parsing => 0,
1366 incr_p => 0,
1367 }, $class;
1368 }
1369  
1370  
1371 sub incr_parse {
1372 0 ▶ my ( $self, $coder, $text ) = @_;
Subroutine incr_parse: not called CC=12 SCAR=50.5
1373  
1374 0 ▶ $self->{incr_text} = '' unless ( defined $self->{incr_text} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"incr_text"} 0 0 0
1375  
1376 0 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 0 0 0
1377 0 ▶ 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"})) 0 0 0
Condition0%0/3
utf8::is_utf8($text) and not utf8::is_utf8($self->{"incr_text"})
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/3
A and not(B)
A= utf8::is_utf8($text)B= not utf8::is_utf8($self->{"incr_text"})
ABresult
0X0
100
111
1378 0 utf8::upgrade( $self->{incr_text} ) ;
1379 0 utf8::decode( $self->{incr_text} ) ;
1380 }
1381 0 $self->{incr_text} .= $text;
1382 }
1383  
1384  
1385 0 my $max_size = $coder->get_max_size;
1386  
1387 0 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 0 0
1388  
1389 0 $self->{incr_mode} = INCR_M_WS;
1390  
1391 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1392 0 my @ret;
1393  
1394 0 $self->{incr_parsing} = 1;
1395  
1396 do {
1397 0 push @ret, $self->_incr_parse( $coder, $self->{incr_text} );
1398  
1399 0 ▶ unless ( !$self->{incr_nest} and $self->{incr_mode} == INCR_M_JSON ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->{"incr_nest"} and $self->{"incr_mode"} == 3) 0 0 0
Condition0%0/3
not $self->{"incr_nest"} and $self->{"incr_mode"} == 3
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"incr_nest"} and $self->{"incr_mode"} == 3
not $self->{"incr_nest"} $self->{"incr_mode"} == 3
Truth table0%0/3
not(A) and B
A= not $self->{"incr_nest"}B= $self->{"incr_mode"} == 3
ABresult
0X0
100
111
1400 0 $self->{incr_mode} = INCR_M_WS;
1401 }
1402  
1403 0 ▶ } until ( !$self->{incr_text} );
Branch: 1 branch
BranchTrueFalseTotal
not $self->{"incr_text"} or do { do { push @ret, $self->_incr_parse($coder, $self->{"incr_text"}); unless (not $self->{"incr_nest"} and $self->{"incr_mode"} == 3) { $self->{"incr_mode"} = 0; } } } 0 0 0
1404  
1405 0 $self->{incr_parsing} = 0;
1406  
1407 0 return @ret;
1408 }
1409 else { # in scalar context
1410 0 $self->{incr_parsing} = 1;
1411 0 my $obj = $self->_incr_parse( $coder, $self->{incr_text} );
1412 0 ▶ $self->{incr_parsing} = 0 if defined $obj; # pointed by Martin J. Evans
Branch: 1 branch
BranchTrueFalseTotal
if defined $obj 0 0 0
1413 0 ▶ return $obj ? $obj : undef; # $obj is an empty string, parsing was completed.
Branch: 1 branch
BranchTrueFalseTotal
$obj ? : 0 0 0
1414 }
1415  
1416 }
1417  
1418 }
1419  
1420  
1421 sub _incr_parse {
1422 0 ▶ my ( $self, $coder, $text, $skip ) = @_;
Subroutine _incr_parse: not called CC=22 SCAR=62.3
1423 0 my $p = $self->{incr_p};
1424 0 my $restore = $p;
1425  
1426 0 my @obj;
1427 0 my $len = length $text;
1428  
1429 0 ▶ if ( $self->{incr_mode} == INCR_M_WS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"incr_mode"} == 0) 0 0 0
1430 0 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 0 0 0
1431 0 my $s = substr( $text, $p, 1 );
1432 0 ▶ $p++ and next if ( 0x20 >= unpack($unpack_format, $s) );
Branch: 1 branch
BranchTrueFalseTotal
if 32 >= unpack($unpack_format, $s) 0 0 0
Condition0%0/5
32 >= unpack($unpack_format, $s) && ($p++ && (next))
!ll&&!rl&&r
000
$p++ and next
!ll
00
MC/DC0%0/2
32 >= unpack($unpack_format, $s) && ($p++ && (next))
32 >= unpack($unpack_format, $s) $p++
Truth table0%0/3
A && B
A= 32 >= unpack($unpack_format, $s)B= $p++
ABresult
0X0
100
111
1433 0 $self->{incr_mode} = INCR_M_JSON;
1434 0 last;
1435 }
1436 }
1437  
1438 0 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 0 0 0
1439 0 my $s = substr( $text, $p++, 1 );
1440  
1441 0 ▶ if ( $s eq '"' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq "\"") 0 0 0
1442 0 ▶ if ( $self->{incr_mode} != INCR_M_STR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'incr_mode'} != 1) { } 0 0 0
1443 0 $self->{incr_mode} = INCR_M_STR;
1444 }
1445 else {
1446 0 $self->{incr_mode} = INCR_M_JSON;
1447 0 ▶ unless ( $self->{incr_nest} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"incr_nest"}) 0 0 0
1448 0 last;
1449 }
1450 }
1451 }
1452  
1453 0 ▶ if ( $self->{incr_mode} == INCR_M_JSON ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"incr_mode"} == 3) 0 0 0
1454  
1455 0 ▶ if ( $s eq '[' or $s eq '{' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($s eq '[' or $s eq '{') { } 0 0 0
elsif ($s eq ']' or $s eq '}') { } 0 0 0
elsif ($s eq '#') { } 0 0 0
Condition0%0/3
$s eq "[" or $s eq "{"
l!l&&r!l&&!r
000
MC/DC0%0/2
$s eq "[" or $s eq "{"
$s eq "[" $s eq "{"
Truth table0%0/3
A or B
A= $s eq "["B= $s eq "{"
ABresult
1X1
011
000
1456 0 ▶ if ( ++$self->{incr_nest} > $coder->get_max_depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"incr_nest"} > $coder->get_max_depth) 0 0 0
1457 0 Carp::croak('json text or perl structure exceeds maximum nesting level (max_depth set too low?)');
1458 }
1459 }
1460 ▶ elsif ( $s eq ']' or $s eq '}' ) {
Condition0%0/3
$s eq "]" or $s eq "}"
l!l&&r!l&&!r
000
MC/DC0%0/2
$s eq "]" or $s eq "}"
$s eq "]" $s eq "}"
Truth table0%0/3
A or B
A= $s eq "]"B= $s eq "}"
ABresult
1X1
011
000
1461 0 ▶ last if ( --$self->{incr_nest} <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"incr_nest"} <= 0 0 0 0
1462 }
1463 elsif ( $s eq '#' ) {
1464 0 ▶ while ( $len > $p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $p) 0 0 0
1465 0 ▶ last if substr( $text, $p++, 1 ) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $p++, 1) eq "\n" 0 0 0
1466 }
1467 }
1468  
1469 }
1470  
1471 }
1472  
1473 0 $self->{incr_p} = $p;
1474  
1475 0 ▶ return if ( $self->{incr_mode} == INCR_M_JSON and $self->{incr_nest} > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"incr_mode"} == 3 and $self->{"incr_nest"} > 0 0 0 0
Condition0%0/3
$self->{"incr_mode"} == 3 and $self->{"incr_nest"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"incr_mode"} == 3 and $self->{"incr_nest"} > 0
$self->{"incr_mode"} == 3 $self->{"incr_nest"} > 0
Truth table0%0/3
A and B
A= $self->{"incr_mode"} == 3B= $self->{"incr_nest"} > 0
ABresult
0X0
100
111
1476  
1477 0 ▶ return '' unless ( length substr( $self->{incr_text}, 0, $p ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length substr($self->{"incr_text"}, 0, $p) 0 0 0
1478  
1479 0 local $Carp::CarpLevel = 2;
1480  
1481 0 $self->{incr_p} = $restore;
1482 0 $self->{incr_c} = $p;
1483  
1484 0 my ( $obj, $tail ) = $coder->PP_decode_json( substr( $self->{incr_text}, 0, $p ), 0x10000001 );
1485  
1486 0 $self->{incr_text} = substr( $self->{incr_text}, $p );
1487 0 $self->{incr_p} = 0;
1488  
1489 0 return $obj or '';
1490 }
1491  
1492  
1493 sub incr_text {
1494 0 ▶ if ( $_[0]->{incr_parsing} ) {
Subroutine incr_text: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"incr_parsing"}) 0 0 0
1495 0 Carp::croak("incr_text can not be called when the incremental parser already started parsing");
1496 }
1497 0 $_[0]->{incr_text};
1498 }
1499  
1500  
1501 sub incr_skip {
1502 0 ▶ my $self = shift;
Subroutine incr_skip: not called CC=1 SCAR=6.9
1503 0 $self->{incr_text} = substr( $self->{incr_text}, $self->{incr_c} );
1504 0 $self->{incr_p} = 0;
1505 }
1506  
1507  
1508 sub incr_reset {
1509 0 ▶ my $self = shift;
Subroutine incr_reset: not called CC=1 SCAR=6.9
1510 0 $self->{incr_text} = undef;
1511 0 $self->{incr_p} = 0;
1512 0 $self->{incr_mode} = 0;
1513 0 $self->{incr_nest} = 0;
1514 0 $self->{incr_parsing} = 0;
1515 }
1516  
1517 ###############################
1518  
1519  
1520 3 1;
1521 __END__
↑ Summary JSON.pm »