blib/lib/JSON/Any.pm

filter: Statementstmt 74.2% 173 / 233 Branchbran 65.7% 50 / 76 Conditioncond 73.3% 11 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 57.1% 28 / 49 Podpod 100% 7 / 7 totaltotal 70.1% 273 / 389 CC 49 SCAR 18.1
CC
49
Cov
72%
import
17.2
__ANON__
12.0
__ANON__
12.0
CRAP 6.1
↑ Summary
1 package JSON::Any; # git description: v1.41-3-g2ae6655
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 our $VERSION = '1.42';
7  
8 12 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: (DEPRECATED) Wrapper Class for the various JSON classes
11 # KEYWORDS: json serialization serialisation wrapper abstraction
12  
13 12 our $UTF8;
14  
15 12 my ( %conf, $handler, $encoder, $decoder );
16 12 ▶use constant HANDLER => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use constant ENCODER => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use constant DECODER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use constant UTF8 => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=1.4
22 %conf = (
23 json_1 => {
24 encoder => 'objToJson',
25 decoder => 'jsonToObj',
26 0 ▶ get_true => sub { return JSON::True(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 ▶ get_false => sub { return JSON::False(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 ▶ create_object => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
29 0 require JSON;
30 0 my ( $self, $conf ) = @_;
31 0 my @params = qw(
32 autoconv
33 skipinvalid
34 execcoderef
35 pretty
36 indent
37 delimiter
38 keysort
39 convblessed
40 selfconvert
41 singlequote
42 quoteapos
43 unmapping
44 barekey
45 );
46 my $obj =
47 0 $handler->new( utf8 => $conf->{utf8} ); ## constructor only
48  
49 0 for my $mutator (@params) {
50 0 ▶ next unless exists $conf->{$mutator};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf->{$mutator} 0 0 0
51 0 $obj = $obj->$mutator( $conf->{$mutator} );
52 }
53  
54 0 $self->[ENCODER] = 'objToJson';
55 0 $self->[DECODER] = 'jsonToObj';
56 0 $self->[HANDLER] = $obj;
57 },
58 },
59 json_pp => {
60 encoder => 'encode_json',
61 decoder => 'decode_json',
62 2 ▶ get_true => sub { return JSON::PP::true(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 2 ▶ get_false => sub { return JSON::PP::false(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 3 ▶ create_object => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
65 3 my ( $self, $conf ) = @_;
66 3 my @params = qw(
67 ascii
68 latin1
69 utf8
70 pretty
71 indent
72 space_before
73 space_after
74 relaxed
75 canonical
76 allow_nonref
77 allow_blessed
78 convert_blessed
79 filter_json_object
80 shrink
81 max_depth
82 max_size
83 loose
84 allow_bignum
85 allow_barekey
86 allow_singlequote
87 escape_slash
88 indent_length
89 sort_by
90 );
91 3 local $conf->{utf8} = !$conf->{utf8}; # it means the opposite
92 3 my $obj = $handler->new;
93  
94 3 for my $mutator (@params) {
95 69 ▶ next unless exists $conf->{$mutator};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf->{$mutator} 66 3 69
96 3 $obj = $obj->$mutator( $conf->{$mutator} );
97 }
98  
99 3 $self->[ENCODER] = 'encode';
100 3 $self->[DECODER] = 'decode';
101 3 $self->[HANDLER] = $obj;
102 },
103 },
104 json_dwiw => {
105 encoder => 'to_json',
106 decoder => 'from_json',
107 0 ▶ get_true => sub { return JSON::DWIW->true; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 ▶ get_false => sub { return JSON::DWIW->false; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 ▶ create_object => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
110 0 my ( $self, $conf ) = @_;
111 0 my @params = qw(bare_keys);
112 0 ▶ croak "JSON::DWIW does not support utf8" if $conf->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"utf8"} 0 0 0
113 0 $self->[ENCODER] = 'to_json';
114 0 $self->[DECODER] = 'from_json';
115 $self->[HANDLER] =
116 0 $handler->new( { map +($_ => $conf->{$_}), @params } );
117 },
118 },
119 json_xs_1 => {
120 encoder => 'to_json',
121 decoder => 'from_json',
122 0 ▶ get_true => sub { return \1; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 ▶ get_false => sub { return \0; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 ▶ create_object => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
125 0 my ( $self, $conf ) = @_;
126  
127 0 my @params = qw(
128 ascii
129 utf8
130 pretty
131 indent
132 space_before
133 space_after
134 canonical
135 allow_nonref
136 shrink
137 max_depth
138 );
139  
140 0 my $obj = $handler->new;
141 0 for my $mutator (@params) {
142 0 ▶ next unless exists $conf->{$mutator};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf->{$mutator} 0 0 0
143 0 $obj = $obj->$mutator( $conf->{$mutator} );
144 }
145 0 $self->[ENCODER] = 'encode';
146 0 $self->[DECODER] = 'decode';
147 0 $self->[HANDLER] = $obj;
148 },
149 },
150 json_xs_2 => {
151 encoder => 'encode_json',
152 decoder => 'decode_json',
153 0 ▶ get_true => sub { return JSON::XS::true(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 ▶ get_false => sub { return JSON::XS::false(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 14 ▶ create_object => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
156 14 my ( $self, $conf ) = @_;
157  
158 14 my @params = qw(
159 ascii
160 latin1
161 utf8
162 pretty
163 indent
164 space_before
165 space_after
166 relaxed
167 canonical
168 allow_nonref
169 allow_blessed
170 convert_blessed
171 filter_json_object
172 shrink
173 max_depth
174 max_size
175 );
176  
177 14 local $conf->{utf8} = !$conf->{utf8}; # it means the opposite
178  
179 14 my $obj = $handler->new;
180 14 for my $mutator (@params) {
181 224 ▶ next unless exists $conf->{$mutator};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf->{$mutator} 197 27 224
182 27 $obj = $obj->$mutator( $conf->{$mutator} );
183 }
184 14 $self->[ENCODER] = 'encode';
185 14 $self->[DECODER] = 'decode';
186 14 $self->[HANDLER] = $obj;
187 },
188 },
189 json_syck => {
190 encoder => 'Dump',
191 decoder => 'Load',
192 0 ▶ get_true => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
193 0 croak "JSON::Syck does not support special boolean values";
194 },
195 0 ▶ get_false => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 croak "JSON::Syck does not support special boolean values";
197 },
198 0 ▶ create_object => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
199 0 my ( $self, $conf ) = @_;
200 0 ▶ croak "JSON::Syck does not support utf8" if $conf->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"utf8"} 0 0 0
201 0 ▶ $self->[ENCODER] = sub { Dump(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 0 ▶ $self->[DECODER] = sub { Load(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 0 $self->[HANDLER] = 'JSON::Syck';
204 }
205 },
206 12 );
207  
208 # JSON.pm v2 has the same API as JSON::PP
209 12 $conf{json_2} = { %{ $conf{json_pp} } };
210 0 ▶ $conf{json_2}{get_true} = sub { return JSON::true(); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
211 0 ▶ $conf{json_2}{get_false} = sub { return JSON::false(); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
212 {
213 12 my $create = $conf{json_2}{create_object};
214 0 ▶ $conf{json_2}{create_object} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 JSON->import( '-support_by_pp', '-no_export' );
216 0 goto &$create;
217 12 };
218 }
219  
220 # JSON.pm v3 and v4 are the same as v2
221 12 $conf{json_3} = { %{ $conf{json_2} } };
222 12 $conf{json_4} = { %{ $conf{json_3} } };
223  
224 # Cpanel::JSON::XS is a fork of JSON::XS (currently)
225 12 $conf{cpanel_json_xs} = { %{ $conf{json_xs_2} } };
226 12 ▶ $conf{cpanel_json_xs}{get_true} = sub { return Cpanel::JSON::XS::true(); };
Subroutine __ANON__: called CC=1 SCAR=0.0
227 12 ▶ $conf{cpanel_json_xs}{get_false} = sub { return Cpanel::JSON::XS::false(); };
Subroutine __ANON__: called CC=1 SCAR=0.0
228  
229 # JSON::XS 3 is almost the same as JSON::XS 2
230 12 $conf{json_xs_3} = { %{ $conf{json_xs_2} } };
231 0 ▶ $conf{json_xs_3}{get_true} = sub { return Types::Serialiser::true(); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
232 0 ▶ $conf{json_xs_3}{get_false} = sub { return Types::Serialiser::false(); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
233  
234 # JSON::XS v4 is the same as v3
235 12 $conf{json_xs_4} = { %{ $conf{json_xs_3} } };
236 }
237  
238 42 ▶sub _make_key {
Subroutine _make_key: called CC=3 SCAR=12.3
239 42 my $handler = shift;
240 42 ( my $key = lc($handler) ) =~ s/::/_/g;
241 42 ▶ if ( 'json_xs' eq $key || 'json' eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("json_xs" eq $key or "json" eq $key) 0 42 42
Condition33%1/3
"json_xs" eq $key or "json" eq $key
l!l&&r!l&&!r
0042
MC/DC0%0/2
"json_xs" eq $key or "json" eq $key
"json_xs" eq $key "json" eq $key
Truth table33%1/3
A or B
A= "json_xs" eq $keyB= "json" eq $key
ABresult
1X1
011
000
242 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
243 0 $key .= "_" . ( split /\./, ${"$handler\::VERSION"} )[0];
244 }
245 42 return $key;
246 }
247  
248 12 my @default = qw(CPANEL XS PP JSON DWIW);
249 12 my @deprecated = qw(Syck);
250  
251 56 ▶sub _module_name {
Subroutine _module_name: called CC=3 SCAR=11.0
252 56 my ($testmod) = @_;
253 56 ▶ return 'Cpanel::JSON::XS' if $testmod eq 'CPANEL';
Branch: 1 branch
BranchTrueFalseTotal
if $testmod eq "CPANEL" 12 44 56
254 44 ▶ return 'JSON' if $testmod eq 'JSON';
Branch: 1 branch
BranchTrueFalseTotal
if $testmod eq "JSON" 12 32 44
255 32 return "JSON::$testmod";
256 }
257  
258 30 ▶sub _try_loading {
Subroutine _try_loading: called CC=4 SCAR=13.9
259 30 my @order = @_;
260 30 ( $handler, $encoder, $decoder ) = ();
261 30 foreach my $mod (@order) {
262 35 my $testmod = _module_name($mod);
263 35 ▶ if (eval "require $testmod; 1") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require $testmod; 1") 13 22 35
264 13 $handler = $testmod;
265 13 my $key = _make_key($handler);
266 13 ▶ next unless exists $conf{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf{$key} 0 13 13
267 13 $encoder = $conf{$key}->{encoder};
268 13 $decoder = $conf{$key}->{decoder};
269 13 last;
270 }
271 }
272 30 return ( $handler, $encoder, $decoder );
273 }
274  
275 29 ▶sub import {
Subroutine import: called CC=13 SCAR=28.4
276 29 my $class = shift;
277 29 my @order = @_;
278  
279 29 ( $handler, $encoder, $decoder ) = ();
280  
281 @order = split /\s/, $ENV{JSON_ANY_ORDER}
282 29 ▶ if !@order and $ENV{JSON_ANY_ORDER};
Branch: 1 branch
BranchTrueFalseTotal
if not @order and $ENV{"JSON_ANY_ORDER"} 3 26 29
Condition100%3/3
not @order and $ENV{"JSON_ANY_ORDER"}
!ll&&!rl&&r
2063
MC/DC100%2/2
not @order and $ENV{"JSON_ANY_ORDER"}
not @order $ENV{"JSON_ANY_ORDER"}
Truth table100%3/3
not(A) and B
A= not @orderB= $ENV{"JSON_ANY_ORDER"}
ABresult
0X0
100
111
283  
284 29 ▶ if (@order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order) { } 23 6 29
285 23 ( $handler, $encoder, $decoder ) = _try_loading(@order);
286 23 ▶ if ( $handler && grep "JSON::$_" eq $handler, @deprecated ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler and grep(("JSON::$_" eq $handler), @deprecated)) 0 23 23
Condition66%2/3
$handler and grep(("JSON::$_" eq $handler), @deprecated)
!ll&&!rl&&r
1580
MC/DC0%0/2
$handler and grep(("JSON::$_" eq $handler), @deprecated)
$handler grep(("JSON::$_" eq $handler), @deprecated)
Truth table66%2/3
A and B
A= $handlerB= grep(("JSON::$_" eq $handler), @deprecated)
ABresult
0X0
100
111
287 0 my @upgrade_to = grep { my $mod = $_; !grep { $mod eq $_ } @deprecated } @order;
288 0 ▶ @upgrade_to = @default if not @upgrade_to;
Branch: 1 branch
BranchTrueFalseTotal
unless @upgrade_to 0 0 0
289 0 carp "Found deprecated package $handler. Please upgrade to ",
290 _module_name_list(@upgrade_to);
291 }
292 }
293 else {
294 6 ( $handler, $encoder, $decoder ) = _try_loading(@default);
295 6 ▶ unless ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 1 5 6
296 1 ( $handler, $encoder, $decoder ) = _try_loading(@deprecated);
297 1 ▶ if ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 0 1 1
298 0 carp "Found deprecated package $handler. Please upgrade to ",
299 _module_name_list(@default);
300 }
301 }
302 }
303  
304 29 ▶ unless ($handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 16 13 29
305 16 ▶ croak "Couldn't find a JSON package. Need ", _module_name_list(@order ? @order : @default);
Branch: 1 branch
BranchTrueFalseTotal
@order ? : 15 1 16
306 }
307 13 ▶ croak "Couldn't find a working decoder method (but found handler $handler ", $handler->VERSION, ")." unless $decoder;
Branch: 1 branch
BranchTrueFalseTotal
unless $decoder 0 13 13
308 13 ▶ croak "Couldn't find a working encoder method (but found handler $handler ", $handler->VERSION, ")." unless $encoder;
Branch: 1 branch
BranchTrueFalseTotal
unless $encoder 0 13 13
309 }
310  
311 16 ▶sub _module_name_list {
Subroutine _module_name_list: called CC=2 SCAR=6.9
312 16 my @list = map _module_name($_), @_;
313 16 my $last = pop @list;
314 return (@list
315 16 ▶ ? (join(', ' => @list), " or $last")
Branch: 1 branch
BranchTrueFalseTotal
@list ? : 2 14 16
316 : $last
317 );
318 }
319  
320 #pod =head1 SYNOPSIS
321 #pod
322 #pod use JSON::Any;
323 #pod my $j = JSON::Any->new;
324 #pod my $json = $j->objToJson({foo=>'bar', baz=>'quux'});
325 #pod my $obj = $j->jsonToObj($json);
326 #pod
327 #pod =head1 DEPRECATION NOTICE
328 #pod
329 #pod The original need for L<JSON::Any> has been solved (quite some time ago
330 #pod actually). If you're producing new code it is recommended to use L<JSON::MaybeXS> which
331 #pod will optionally use L<Cpanel::JSON::XS> for speed purposes.
332 #pod
333 #pod JSON::Any will continue to be maintained for compatibility with existing code
334 #pod (as well as for rare cases where you want L<JSON::DWIW> as a backend),
335 #pod but for new code you should strongly consider using L<JSON::MaybeXS> instead.
336 #pod
337 #pod For more information about the various options and which are preferred, see
338 #pod L<Matt Trout's analysis|http://shadow.cat/blog/matt-s-trout/mstpan-7>.
339 #pod
340 #pod =head1 DESCRIPTION
341 #pod
342 #pod This module tries to provide a coherent API to bring together the various JSON
343 #pod modules currently on CPAN. This module will allow you to code to any JSON API
344 #pod and have it work regardless of which JSON module is actually installed.
345 #pod
346 #pod use JSON::Any;
347 #pod
348 #pod my $j = JSON::Any->new;
349 #pod
350 #pod $json = $j->objToJson({foo=>'bar', baz=>'quux'});
351 #pod $obj = $j->jsonToObj($json);
352 #pod
353 #pod or
354 #pod
355 #pod $json = $j->encode({foo=>'bar', baz=>'quux'});
356 #pod $obj = $j->decode($json);
357 #pod
358 #pod or
359 #pod
360 #pod $json = $j->Dump({foo=>'bar', baz=>'quux'});
361 #pod $obj = $j->Load($json);
362 #pod
363 #pod or
364 #pod
365 #pod $json = $j->to_json({foo=>'bar', baz=>'quux'});
366 #pod $obj = $j->from_json($json);
367 #pod
368 #pod or without creating an object:
369 #pod
370 #pod $json = JSON::Any->objToJson({foo=>'bar', baz=>'quux'});
371 #pod $obj = JSON::Any->jsonToObj($json);
372 #pod
373 #pod On load, JSON::Any will find a valid JSON module in your @INC by looking
374 #pod for them in this order:
375 #pod
376 #pod Cpanel::JSON::XS
377 #pod JSON::XS
378 #pod JSON::PP
379 #pod JSON
380 #pod JSON::DWIW
381 #pod
382 #pod And loading the first one it finds.
383 #pod
384 #pod You may change the order by specifying it on the C<use JSON::Any> line:
385 #pod
386 #pod use JSON::Any qw(DWIW XS CPANEL JSON PP);
387 #pod
388 #pod Specifying an order that is missing modules will prevent those module from
389 #pod being used:
390 #pod
391 #pod use JSON::Any qw(CPANEL PP); # same as JSON::MaybeXS
392 #pod
393 #pod This will check in that order, and will never attempt to load L<JSON::XS>,
394 #pod L<JSON.pm/JSON>, or L<JSON::DWIW>. This can also be set via the C<$ENV{JSON_ANY_ORDER}>
395 #pod environment variable.
396 #pod
397 #pod L<JSON::Syck> has been deprecated by its author, but in the attempt to still
398 #pod stay relevant as a "Compatibility Layer" JSON::Any still supports it. This support
399 #pod however has been made optional starting with JSON::Any 1.19. In deference to a
400 #pod bug request starting with L<JSON.pm|JSON> 1.20, L<JSON::Syck> and other deprecated modules
401 #pod will still be installed, but only as a last resort and will now include a
402 #pod warning.
403 #pod
404 #pod use JSON::Any qw(Syck XS JSON);
405 #pod
406 #pod or
407 #pod
408 #pod $ENV{JSON_ANY_ORDER} = 'Syck XS JSON';
409 #pod
410 #pod At install time, JSON::Any will attempt to install L<JSON::PP> as a reasonable
411 #pod fallback if you do not appear have B<any> backends installed on your system.
412 #pod
413 #pod WARNING: If you call JSON::Any with an empty list
414 #pod
415 #pod use JSON::Any ();
416 #pod
417 #pod It will skip the JSON package detection routines and will die loudly that it
418 #pod couldn't find a package.
419 #pod
420 #pod =head1 WARNING
421 #pod
422 #pod L<JSON::XS> 3.0 or higher has a conflict with any version of L<JSON.pm|JSON> less than 2.90
423 #pod when you use L<JSON.pm|JSON>'s C<-support_by_pp> option, which JSON::Any enables by
424 #pod default.
425 #pod
426 #pod This situation should only come up with JSON::Any if you have L<JSON.pm|JSON> 2.61 or
427 #pod lower B<and> L<JSON::XS> 3.0 or higher installed, and you use L<JSON.pm|JSON>
428 #pod via C<< use JSON::Any qw(JSON); >> or the C<JSON_ANY_ORDER> environment variable.
429 #pod
430 #pod If you run into an issue where you're getting recursive inheritance errors in a
431 #pod L<Types::Serialiser> package, please try upgrading L<JSON.pm|JSON> to 2.90 or higher.
432 #pod
433 #pod =head1 METHODS
434 #pod
435 #pod =head2 C<new>
436 #pod
437 #pod =for stopwords recognised unicode
438 #pod
439 #pod Will take any of the parameters for the underlying system and pass them
440 #pod through. However these values don't map between JSON modules, so, from a
441 #pod portability standpoint this is really only helpful for those parameters that
442 #pod happen to have the same name.
443 #pod
444 #pod The one parameter that is universally supported (to the extent that is
445 #pod supported by the underlying JSON modules) is C<utf8>. When this parameter is
446 #pod enabled, all unicode strings in the input data structure will be preserved as such.
447 #pod Note that this is the B<OPPOSITE> of the meaning of the underlying C<utf8> option
448 #pod in many backends!
449 #pod
450 #pod Also note that the C<allow_blessed> parameter is recognised by all the modules
451 #pod that throw exceptions when a blessed reference is given them meaning that
452 #pod setting it to true works for all modules. Of course, that means that you
453 #pod cannot set it to false intentionally in order to always get such exceptions.
454 #pod
455 #pod The actual output will vary, for example L<JSON> will encode and decode
456 #pod unicode chars (the resulting JSON is not unicode) whereas L<JSON::XS> will emit
457 #pod unicode JSON.
458 #pod
459 #pod =cut
460  
461 17 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
462 17 my $class = shift;
463 17 my $self = bless [], $class;
464 17 my $key = _make_key($handler);
465 17 ▶ if ( my $creator = $conf{$key}->{create_object} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $creator = $conf{$key}{"create_object"}) 17 0 17
466 17 my @config;
467 # undocumented! and yet, people are using this...
468 17 ▶ if ( $ENV{JSON_ANY_CONFIG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"JSON_ANY_CONFIG"}) 4 13 17
469 4 push @config, map split(/=/, $_), split(/,\s*/, $ENV{JSON_ANY_CONFIG});
470 }
471 17 push @config, @_;
472 17 $creator->( $self, my $conf = {@config} );
473 17 $self->[UTF8] = $conf->{utf8};
474 }
475 17 return $self;
476 }
477  
478 #pod =head2 C<handlerType>
479 #pod
480 #pod Takes no arguments, returns a string indicating which JSON Module is in use.
481 #pod
482 #pod =cut
483  
484 2 ▶sub handlerType {
Subroutine handlerType: called CC=1 SCAR=0.0
Pod documented
485 2 my $class = shift;
486 2 $handler;
487 }
488  
489 #pod =head2 C<handler>
490 #pod
491 #pod Takes no arguments, if called on an object returns the internal JSON::*
492 #pod object in use. Otherwise returns the JSON::* package we are using for
493 #pod class methods.
494 #pod
495 #pod =cut
496  
497 11 ▶sub handler {
Subroutine handler: called CC=2 SCAR=7.6
Pod documented
498 11 my $self = shift;
499 11 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 11 0 11
500 11 return $self->[HANDLER];
501 }
502 0 return $handler;
503 }
504  
505 #pod =head2 C<true>
506 #pod
507 #pod Takes no arguments, returns the special value that the internal JSON
508 #pod object uses to map to a JSON C<true> boolean.
509 #pod
510 #pod =cut
511  
512 6 ▶sub true {
Subroutine true: called CC=1 SCAR=0.0
Pod documented
513 6 my $key = _make_key($handler);
514 6 return $conf{$key}->{get_true}->();
515 }
516  
517 #pod =head2 C<false>
518 #pod
519 #pod Takes no arguments, returns the special value that the internal JSON
520 #pod object uses to map to a JSON C<false> boolean.
521 #pod
522 #pod =cut
523  
524 6 ▶sub false {
Subroutine false: called CC=1 SCAR=0.0
Pod documented
525 6 my $key = _make_key($handler);
526 6 return $conf{$key}->{get_false}->();
527 }
528  
529 #pod =head2 C<objToJson>
530 #pod
531 #pod Takes a single argument, a hashref to be converted into JSON.
532 #pod It returns the JSON text in a scalar.
533 #pod
534 #pod =cut
535  
536 52 ▶sub objToJson {
Subroutine objToJson: called CC=10 SCAR=23.5
Pod documented
537 52 my $self = shift;
538 52 my $obj = shift;
539 52 ▶ croak 'must provide object to convert' unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 52 52
540  
541 52 my $json;
542  
543 52 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 26 26 52
544 26 my $method;
545 26 ▶ unless ( ref $self->[ENCODER] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->[1]) { } 0 26 26
546 26 ▶ croak "No $handler Object created!"
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[0] 0 26 26
547 unless exists $self->[HANDLER];
548 26 $method = $self->[HANDLER]->can( $self->[ENCODER] );
549 26 ▶ croak "$handler can't execute $self->[ENCODER]" unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 26 26
550 }
551 else {
552 0 $method = $self->[ENCODER];
553 }
554 26 $json = $self->[HANDLER]->$method($obj);
555 }
556 else {
557 26 $json = $handler->can($encoder)->($obj);
558 }
559  
560 51 ▶ utf8::decode($json)
Branch: 2 branches
BranchTrueFalseTotal
if ref $self ? $self->[3] : $JSON::Any::UTF8 and not utf8::is_utf8($json) and utf8::valid($json) 2 49 51
ref $self ? : 25 26 51
Condition83%5/6
ref $self ? $self->[3] : $JSON::Any::UTF8 and not utf8::is_utf8($json) and utf8::valid($json)
!ll&&!rl&&r
4902
ref $self ? $self->[3] : $JSON::Any::UTF8 and not utf8::is_utf8($json)
!ll&&!rl&&r
37122
MC/DC66%2/3
ref $self ? $self->[3] : $JSON::Any::UTF8 and not utf8::is_utf8($json) and utf8::valid($json)
ref $self ? $self->[3] : $JSON::Any::UTF8 not utf8::is_utf8($json) utf8::valid($json)
Truth table75%3/4
A and not(B) and C
A= ref $self ? $self->[3] : $JSON::Any::UTF8B= not utf8::is_utf8($json)C= utf8::valid($json)
ABCresult
0XX0
10X0
1100
1111
561 if ( ref $self ? $self->[UTF8] : $UTF8 )
562 and !utf8::is_utf8($json)
563 and utf8::valid($json);
564 51 return $json;
565 }
566  
567 #pod =head2 C<to_json>
568 #pod
569 #pod =head2 C<Dump>
570 #pod
571 #pod =head2 C<encode>
572 #pod
573 #pod Aliases for C<objToJson>, can be used interchangeably, regardless of the
574 #pod underlying JSON module.
575 #pod =cut
576  
577 12 *to_json = \&objToJson;
578 12 *Dump = \&objToJson;
579 12 *encode = \&objToJson;
580  
581 #pod =head2 C<jsonToObj>
582 #pod
583 #pod Takes a single argument, a string of JSON text to be converted
584 #pod back into a hashref.
585 #pod
586 #pod =cut
587  
588 40 ▶sub jsonToObj {
Subroutine jsonToObj: called CC=7 SCAR=19.9
Pod documented
589 40 my $self = shift;
590 40 my $obj = shift;
591 40 ▶ croak 'must provide json to convert' unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 40 40
592  
593 # some handlers can't parse single booleans (I'm looking at you DWIW)
594 40 ▶ if ( $obj =~ /^(true|false)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj =~ /^(true|false)$/) 2 38 40
595 2 return $self->$1;
596 }
597  
598 38 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 17 21 38
599 17 my $method;
600 17 ▶ unless ( ref $self->[DECODER] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->[2]) { } 0 17 17
601 17 ▶ croak "No $handler Object created!"
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[0] 0 17 17
602 unless exists $self->[HANDLER];
603 17 $method = $self->[HANDLER]->can( $self->[DECODER] );
604 17 ▶ croak "$handler can't execute $self->[DECODER]" unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 17 17
605 }
606 else {
607 0 $method = $self->[DECODER];
608 }
609 17 return $self->[HANDLER]->$method($obj);
610 }
611 21 $handler->can($decoder)->($obj);
612 }
613  
614 #pod =head2 C<from_json>
615 #pod
616 #pod =head2 C<Load>
617 #pod
618 #pod =head2 C<decode>
619 #pod
620 #pod Aliases for C<jsonToObj>, can be used interchangeably, regardless of the
621 #pod underlying JSON module.
622 #pod
623 #pod =cut
624  
625 12 *from_json = \&jsonToObj;
626 12 *Load = \&jsonToObj;
627 12 *decode = \&jsonToObj;
628  
629 {
630 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
631 12 delete @{__PACKAGE__.'::'}{qw(croak carp)};
632 }
633  
634 12 1;
635  
636 __END__
↑ Summary