JSON.pm

filter: Statementstmt 82.3% 159 / 193 Branchbran 51.0% 48 / 94 Conditioncond 42.8% 12 / 28 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 90.9% 30 / 33 Podpod 71.4% 10 / 14 totaltotal 69.3% 263 / 379 CC 60 SCAR 22.3
CC
60
Cov
70%
property
45.2
__load_xs
16.7
objToJson
9.8
CRAP 9.3
« JSON/backportPP.pm ↑ Summary JSON/backportPP/Compat5005.pm »
1 package JSON;
2  
3  
4 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 68 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 68 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 68 ▶BEGIN { @JSON::ISA = 'Exporter' }
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 68 @JSON::EXPORT = qw(from_json to_json jsonToObj objToJson encode_json decode_json);
10  
11 68 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.7
12 68 $JSON::VERSION = '4.11';
13 68 ▶ $JSON::DEBUG = 0 unless (defined $JSON::DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $JSON::DEBUG 68 0 68
14 68 ▶ $JSON::DEBUG = $ENV{ PERL_JSON_DEBUG } if exists $ENV{ PERL_JSON_DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"PERL_JSON_DEBUG"} 0 68 68
15 }
16  
17 68 my %RequiredVersion = (
18 'JSON::PP' => '2.27203',
19 'JSON::XS' => '2.34',
20 );
21  
22 # XS and PP common methods
23  
24 68 my @PublicMethods = qw/
25 ascii latin1 utf8 pretty indent space_before space_after relaxed canonical allow_nonref
26 allow_blessed convert_blessed filter_json_object filter_json_single_key_object
27 shrink max_depth max_size encode decode decode_prefix allow_unknown
28 /;
29  
30 68 my @Properties = qw/
31 ascii latin1 utf8 indent space_before space_after relaxed canonical allow_nonref
32 allow_blessed convert_blessed shrink max_depth max_size allow_unknown
33 /;
34  
35 68 my @XSOnlyMethods = qw//; # Currently nothing
36  
37 68 my @PublicMethodsSince4_0 = qw/allow_tags/;
38 68 my @PropertiesSince4_0 = qw/allow_tags/;
39  
40 68 my @PPOnlyMethods = qw/
41 indent_length sort_by
42 allow_singlequote allow_bignum loose allow_barekey escape_slash as_nonblessed
43 /; # JSON::PP specific
44  
45  
46 # used in _load_xs and _load_pp ($INSTALL_ONLY is not used currently)
47 68 my $_INSTALL_DONT_DIE = 1; # When _load_xs fails to load XS, don't die.
48 68 my $_ALLOW_UNSUPPORTED = 0;
49 68 my $_UNIV_CONV_BLESSED = 0;
50  
51  
52 # Check the environment variable to decide worker module.
53  
54 68 ▶unless ($JSON::Backend) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($JSON::Backend) 68 0 68
55 68 ▶ $JSON::DEBUG and Carp::carp("Check used worker module...");
Branch: 1 branch
BranchTrueFalseTotal
$JSON::DEBUG and Carp::carp("Check used worker module...") 0 68 68
56  
57 68 ▶ my $backend = exists $ENV{PERL_JSON_BACKEND} ? $ENV{PERL_JSON_BACKEND} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'PERL_JSON_BACKEND'} ? : 68 0 68
58  
59 68 ▶ if ($backend eq '1') {
Branch: 3 branches
BranchTrueFalseTotal
if ($backend eq '1') { } 11 57 68
elsif ($backend eq '0') { } 0 57 57
elsif ($backend eq '2') { } 0 57 57
60 11 $backend = 'JSON::XS,JSON::PP';
61 }
62 elsif ($backend eq '0') {
63 0 $backend = 'JSON::PP';
64 }
65 elsif ($backend eq '2') {
66 0 $backend = 'JSON::XS';
67 }
68 68 $backend =~ s/\s+//g;
69  
70 68 my @backend_modules = split /,/, $backend;
71 68 ▶ while(my $module = shift @backend_modules) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $module = shift @backend_modules) 68 0 68
72 68 ▶ if ($module =~ /JSON::XS/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($module =~ /JSON::XS/) { } 11 57 68
elsif ($module =~ /JSON::PP/) { } 0 57 57
elsif ($module =~ /JSON::backportPP/) { } 57 0 57
73 11 ▶ _load_xs($module, @backend_modules ? $_INSTALL_DONT_DIE : 0);
Branch: 1 branch
BranchTrueFalseTotal
@backend_modules ? : 11 0 11
74 }
75 elsif ($module =~ /JSON::PP/) {
76 0 _load_pp($module);
77 }
78 elsif ($module =~ /JSON::backportPP/) {
79 57 _load_pp($module);
80 }
81 else {
82 0 Carp::croak "The value of environmental variable 'PERL_JSON_BACKEND' is invalid.";
83 }
84 68 ▶ last if $JSON::Backend;
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Backend 68 0 68
85 }
86 }
87  
88  
89 67 ▶sub import {
Subroutine import: called CC=9 SCAR=22.6
90 67 my $pkg = shift;
91 67 my @what_to_export;
92 my $no_export;
93  
94 67 for my $tag (@_) {
95 11 ▶ if ($tag eq '-support_by_pp') {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag eq '-support_by_pp') { } 10 1 11
elsif ($tag eq '-no_export') { } 0 1 1
elsif ($tag eq '-convert_blessed_universally') { } 1 0 1
96 10 ▶ if (!$_ALLOW_UNSUPPORTED++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ALLOW_UNSUPPORTED++) 10 0 10
97 10 ▶ JSON::Backend::XS
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Backend->is_xs 5 5 10
98 ->support_by_pp(@PPOnlyMethods) if ($JSON::Backend->is_xs);
99 }
100 10 next;
101 }
102 elsif ($tag eq '-no_export') {
103 0 $no_export++, next;
104 }
105 elsif ( $tag eq '-convert_blessed_universally' ) {
106 1 my $org_encode = $JSON::Backend->can('encode');
107 1 ▶ eval q|
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_UNIV_CONV_BLESSED++ 1 0 1
108 require B;
109 local $^W;
110 no strict 'refs';
111 *{"${JSON::Backend}\::encode"} = sub {
112 # only works with Perl 5.18+
113 local *UNIVERSAL::TO_JSON = sub {
114 my $b_obj = B::svref_2object( $_[0] );
115 return $b_obj->isa('B::HV') ? { %{ $_[0] } }
116 : $b_obj->isa('B::AV') ? [ @{ $_[0] } ]
117 : undef
118 ;
119 };
120 $org_encode->(@_);
121 };
122 | if ( !$_UNIV_CONV_BLESSED++ );
123 1 next;
124 }
125 0 push @what_to_export, $tag;
126 }
127  
128 67 ▶ return if ($no_export);
Branch: 1 branch
BranchTrueFalseTotal
if $no_export 0 67 67
129  
130 67 __PACKAGE__->export_to_level(1, $pkg, @what_to_export);
131 }
132  
133  
134 # OBSOLETED
135  
136 3 ▶sub jsonToObj {
Subroutine jsonToObj: called CC=3 SCAR=13.5
Pod undocumented
137 3 my $alternative = 'from_json';
138 3 ▶ if (defined $_[0] and UNIVERSAL::isa($_[0], 'JSON')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and UNIVERSAL::isa($_[0], "JSON")) 0 2 2
Condition0%0/3
defined $_[0] and UNIVERSAL::isa($_[0], "JSON")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and UNIVERSAL::isa($_[0], "JSON")
defined $_[0] UNIVERSAL::isa($_[0], "JSON")
Truth table0%0/3
A and B
A= defined $_[0]B= UNIVERSAL::isa($_[0], "JSON")
ABresult
0X0
100
111
139 2 shift @_; $alternative = 'decode';
140 }
141 0 Carp::carp "'jsonToObj' will be obsoleted. Please use '$alternative' instead.";
142 2 return JSON::from_json(@_);
143 };
144  
145 2 ▶sub objToJson {
Subroutine objToJson: called CC=3 SCAR=22.8
Pod undocumented
146 0 my $alternative = 'to_json';
147 0 ▶ if (defined $_[0] and UNIVERSAL::isa($_[0], 'JSON')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and UNIVERSAL::isa($_[0], "JSON")) 2 0 2
Condition0%0/3
defined $_[0] and UNIVERSAL::isa($_[0], "JSON")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and UNIVERSAL::isa($_[0], "JSON")
defined $_[0] UNIVERSAL::isa($_[0], "JSON")
Truth table0%0/3
A and B
A= defined $_[0]B= UNIVERSAL::isa($_[0], "JSON")
ABresult
0X0
100
111
148 0 shift @_; $alternative = 'encode';
149 }
150 0 Carp::carp "'objToJson' will be obsoleted. Please use '$alternative' instead.";
151 0 JSON::to_json(@_);
152 };
153  
154  
155 # INTERFACES
156  
157 8 ▶sub to_json ($@) {
Subroutine to_json: called CC=7 SCAR=19.7
Pod documented
158 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "JSON" or @_ > 2 and $_[0] eq "JSON") 1 7 8
Condition66%4/6
ref $_[0] eq "JSON" or @_ > 2 and $_[0] eq "JSON"
l!l&&r!l&&!r
017
@_ > 2 and $_[0] eq "JSON"
!ll&&!rl&&r
701
MC/DC33%1/3
ref $_[0] eq "JSON" or @_ > 2 and $_[0] eq "JSON"
ref $_[0] eq "JSON" @_ > 2 $_[0] eq "JSON"
Truth table50%2/4
A or B and C
A= ref $_[0] eq "JSON"B= @_ > 2C= $_[0] eq "JSON"
ABCresult
1XX1
0111
00X0
0100
159 ref($_[0]) eq 'JSON'
160 or (@_ > 2 and $_[0] eq 'JSON')
161 ) {
162 1 Carp::croak "to_json should not be called as a method.";
163 }
164 7 my $json = JSON->new;
165  
166 7 ▶ if (@_ == 2 and ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $_[1] eq "HASH") 4 3 7
Condition66%2/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
304
MC/DC50%1/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
167 4 my $opt = $_[1];
168 4 for my $method (keys %$opt) {
169 4 $json->$method( $opt->{$method} );
170 }
171 }
172  
173 7 $json->encode($_[0]);
174 }
175  
176  
177 5 ▶sub from_json ($@) {
Subroutine from_json: called CC=6 SCAR=19.1
Pod documented
178 5 ▶ if ( ref($_[0]) eq 'JSON' or $_[0] eq 'JSON' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "JSON" or $_[0] eq "JSON") 0 5 5
Condition33%1/3
ref $_[0] eq "JSON" or $_[0] eq "JSON"
l!l&&r!l&&!r
005
MC/DC0%0/2
ref $_[0] eq "JSON" or $_[0] eq "JSON"
ref $_[0] eq "JSON" $_[0] eq "JSON"
Truth table33%1/3
A or B
A= ref $_[0] eq "JSON"B= $_[0] eq "JSON"
ABresult
1X1
011
000
179 0 Carp::croak "from_json should not be called as a method.";
180 }
181 5 my $json = JSON->new;
182  
183 5 ▶ if (@_ == 2 and ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $_[1] eq "HASH") 1 4 5
Condition66%2/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
401
MC/DC50%1/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
184 1 my $opt = $_[1];
185 1 for my $method (keys %$opt) {
186 1 $json->$method( $opt->{$method} );
187 }
188 }
189  
190 5 return $json->decode( $_[0] );
191 }
192  
193  
194  
195 8 ▶sub true { $JSON::true }
Subroutine true: called CC=1 SCAR=0.0
Pod documented
196  
197 4 ▶sub false { $JSON::false }
Subroutine false: called CC=1 SCAR=0.0
Pod documented
198  
199 0 ▶sub boolean {
Subroutine boolean: not called CC=2 SCAR=17.9
Pod documented
200 # might be called as method or as function, so pop() to get the last arg instead of shift() to get the first
201 0 ▶ pop() ? $JSON::true : $JSON::false
Branch: 1 branch
BranchTrueFalseTotal
pop() ? : 0 0 0
202 }
203  
204 3 ▶sub null { undef; }
Subroutine null: called CC=1 SCAR=0.0
Pod documented
205  
206  
207 0 ▶sub require_xs_version { $RequiredVersion{'JSON::XS'}; }
Subroutine require_xs_version: not called CC=1 SCAR=6.9
Pod undocumented
208  
209 35 ▶sub backend {
Subroutine backend: called CC=1 SCAR=0.0
Pod documented
210 35 my $proto = shift;
211 35 $JSON::Backend;
212 }
213  
214 #*module = *backend;
215  
216  
217 2 ▶sub is_xs {
Subroutine is_xs: called CC=1 SCAR=0.0
Pod documented
218 2 return $_[0]->backend->is_xs;
219 }
220  
221  
222 2 ▶sub is_pp {
Subroutine is_pp: called CC=1 SCAR=0.0
Pod documented
223 2 return $_[0]->backend->is_pp;
224 }
225  
226  
227 0 ▶sub pureperl_only_methods { @PPOnlyMethods; }
Subroutine pureperl_only_methods: not called CC=1 SCAR=6.9
Pod undocumented
228  
229  
230 36 ▶sub property {
Subroutine property: called CC=10 SCAR=38.1
Pod documented
231 36 my ($self, $name, $value) = @_;
232  
233 36 ▶ if (@_ == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 36 36
elsif (@_ > 3) { } 0 36 36
elsif (@_ == 2) { } 36 0 36
234 0 my %props;
235 0 for $name (@Properties) {
236 0 my $method = 'get_' . $name;
237 0 ▶ if ($name eq 'max_size') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "max_size") 0 0 0
238 0 my $value = $self->$method();
239 0 ▶ $props{$name} = $value == 1 ? 0 : $value;
Branch: 1 branch
BranchTrueFalseTotal
$value == 1 ? : 0 0 0
240 0 next;
241 }
242 0 $props{$name} = $self->$method();
243 }
244 0 return \%props;
245 }
246 elsif (@_ > 3) {
247 0 Carp::croak('property() can take only the option within 2 arguments.');
248 }
249 elsif (@_ == 2) {
250 36 ▶ if ( my $method = $self->can('get_' . $name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can('get_' . $name)) 36 0 36
251 36 ▶ if ($name eq 'max_size') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "max_size") 0 36 36
252 0 my $value = $self->$method();
253 0 ▶ return $value == 1 ? 0 : $value;
Branch: 1 branch
BranchTrueFalseTotal
$value == 1 ? : 0 0 0
254 }
255 36 $self->$method();
256 }
257 }
258 else {
259 0 $self->$name($value);
260 }
261  
262 }
263  
264  
265  
266 # INTERNAL
267  
268 11 ▶sub __load_xs {
Subroutine __load_xs: called CC=7 SCAR=28.2
269 11 my ($module, $opt) = @_;
270  
271 11 ▶ $JSON::DEBUG and Carp::carp "Load $module.";
Branch: 1 branch
BranchTrueFalseTotal
$JSON::DEBUG and Carp::carp("Load $module.") 0 11 11
272 11 ▶ my $required_version = $RequiredVersion{$module} || '';
Condition50%1/2
$RequiredVersion{$module} || ''
l!l
110
MC/DC0%0/1
$RequiredVersion{$module} || ''
$RequiredVersion{$module}
Truth table50%1/2
A
A= $RequiredVersion{$module}
Aresult
11
00
273  
274 11 eval qq|
275 use $module $required_version ();
276 |;
277  
278 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
279 0 ▶ if (defined $opt and $opt & $_INSTALL_DONT_DIE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt and $opt & $_INSTALL_DONT_DIE) 0 0 0
Condition0%0/3
defined $opt and $opt & $_INSTALL_DONT_DIE
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt and $opt & $_INSTALL_DONT_DIE
defined $opt $opt & $_INSTALL_DONT_DIE
Truth table0%0/3
A and B
A= defined $optB= $opt & $_INSTALL_DONT_DIE
ABresult
0X0
100
111
280 0 ▶ $JSON::DEBUG and Carp::carp "Can't load $module...($@)";
Branch: 1 branch
BranchTrueFalseTotal
$JSON::DEBUG and Carp::carp("Can't load $module...($@)") 0 0 0
281 0 return 0;
282 }
283 0 Carp::croak $@;
284 }
285 11 $JSON::BackendModuleXS = $module;
286 11 return 1;
287 }
288  
289 11 ▶sub _load_xs {
Subroutine _load_xs: called CC=2 SCAR=7.0
290 11 my ($module, $opt) = @_;
291 11 ▶ __load_xs($module, $opt) or return;
Branch: 1 branch
BranchTrueFalseTotal
__load_xs($module, $opt) or return 11 0 11
292  
293 11 my $data = join("", <DATA>); # this code is from Jcode 2.xx.
294 11 close(DATA);
295 11 eval $data;
296 11 JSON::Backend::XS->init($module);
297  
298 11 return 1;
299 };
300  
301  
302 62 ▶sub __load_pp {
Subroutine __load_pp: called CC=7 SCAR=19.8
303 62 my ($module, $opt) = @_;
304  
305 62 ▶ $JSON::DEBUG and Carp::carp "Load $module.";
Branch: 1 branch
BranchTrueFalseTotal
$JSON::DEBUG and Carp::carp("Load $module.") 6 62 68
306 68 ▶ my $required_version = $RequiredVersion{$module} || '';
Condition100%2/2
$RequiredVersion{$module} || ''
l!l
557
MC/DC100%1/1
$RequiredVersion{$module} || ''
$RequiredVersion{$module}
Truth table100%2/2
A
A= $RequiredVersion{$module}
Aresult
11
00
307  
308 62 ▶ eval qq| use $module $required_version () |;
Subroutine BEGIN: called CC=1 SCAR=0.0
309  
310 68 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 73 73
311 6 ▶ if ( $module eq 'JSON::PP' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq "JSON::PP") 40 0 40
312 90 ▶ $JSON::DEBUG and Carp::carp "Can't load $module ($@), so try to load JSON::backportPP";
Branch: 1 branch
BranchTrueFalseTotal
$JSON::DEBUG and Carp::carp("Can't load $module ($@), so try to load JSON::backportPP") 0 5 5
313 90 $module = 'JSON::backportPP';
314 6 local $^W; # if PP installed but invalid version, backportPP redefines methods.
315 6 eval qq| require $module |;
316 }
317 11 ▶ Carp::croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
318 }
319 73 $JSON::BackendModulePP = $module;
320 73 return 1;
321 }
322  
323 63 ▶sub _load_pp {
Subroutine _load_pp: called CC=1 SCAR=0.0
324 68 my ($module, $opt) = @_;
325 68 __load_pp($module, $opt);
326  
327 68 JSON::Backend::PP->init($module);
328 };
329  
330 #
331 # Helper classes for Backend Module (PP)
332 #
333  
334 package JSON::Backend::PP;
335  
336 68 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
337 68 my ($class, $module) = @_;
338  
339 # name may vary, but the module should (always) be a JSON::PP
340  
341 68 local $^W;
342 68 ▶ no strict qw(refs); # this routine may be called after JSON::Backend::XS init was called.
Subroutine BEGIN: called CC=1 SCAR=0.0
343 68 *{"JSON::decode_json"} = \&{"JSON::PP::decode_json"};
344 68 *{"JSON::encode_json"} = \&{"JSON::PP::encode_json"};
345 68 *{"JSON::is_bool"} = \&{"JSON::PP::is_bool"};
346  
347 68 $JSON::true = ${"JSON::PP::true"};
348 68 $JSON::false = ${"JSON::PP::false"};
349  
350 68 push @JSON::Backend::PP::ISA, 'JSON::PP';
351 57 push @JSON::ISA, $class;
352 57 $JSON::Backend = $class;
353 57 $JSON::BackendModule = $module;
354 68 my $version = ${"$class\::VERSION"} = $module->VERSION;
355 145 $version =~ s/_//;
356 68 ▶ if ($version < 3.99) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version < 3.99) { } 57 0 57
357 59 push @XSOnlyMethods, qw/allow_tags get_allow_tags/;
358 } else {
359 17 push @Properties, 'allow_tags';
360 }
361  
362 62 for my $method (@XSOnlyMethods) {
363 119 ▶ *{"JSON::$method"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
364 41 Carp::carp("$method is not supported by $module $version.");
365 41 $_[0];
366 119 };
367 }
368  
369 62 return 1;
370 }
371  
372 45 ▶sub is_xs { 0 };
Subroutine is_xs: called CC=1 SCAR=0.0
373 8 ▶sub is_pp { 1 };
Subroutine is_pp: called CC=1 SCAR=0.0
374  
375 #
376 # To save memory, the below lines are read only when XS backend is used.
377 #
378  
379 package JSON;
380  
381 68 1;
382 __DATA__
« JSON/backportPP.pm ↑ Summary JSON/backportPP/Compat5005.pm »