| 1 | package MOP4Import::Base::JSON; | ||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 13 | ▶ | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 13 | ▶ | use MOP4Import::Base::Configure -as_base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 13 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 13 | ▶ | use constant USING_CPANEL_JSON_XS => JSON()->isa("Cpanel::JSON::XS"); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | # Only works with Cpanel::JSON::XS. JSON::XS prohibits use of restricted hash. | ||||||||||||||||||||
| 12 | sub TO_JSON { | ||||||||||||||||||||
| 13 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
TO_JSON: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 14 | 1 | +{map(($_ => $self->{$_}), grep {!/^_/} keys %$self)} | |||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | sub cli_json_type { | ||||||||||||||||||||
| 18 | 0 | ▶ | (my MY $self) = @_; | ||||||||||||||||||
|
Subroutine
cli_json_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 19 | 0 | $self->cli_json_type_of($self); | |||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub cli_json_type_of { | ||||||||||||||||||||
| 23 | 26 | ▶ | (my MY $self, my $objOrTypeName) = @_; | ||||||||||||||||||
|
Subroutine
cli_json_type_of: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||
| 24 | 26 | ▶ | $self->JSON_TYPE_HANDLER->lookup_json_type(ref $objOrTypeName || $objOrTypeName); | ||||||||||||||||||
|
Condition33%1/3
ref $objOrTypeName || $objOrTypeName
MC/DC0%0/2
ref $objOrTypeName || $objOrTypeName
ref $objOrTypeName
$objOrTypeName
Truth table33%1/3
A || B
A= ref $objOrTypeNameB= $objOrTypeName
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | sub cli_json_encoder { | ||||||||||||||||||||
| 28 | 24 | ▶ | (my MY $self) = @_; | ||||||||||||||||||
|
Subroutine
cli_json_encoder: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 29 | 24 | my $js = JSON()->new->canonical->allow_nonref; | |||||||||||||||||||
| 30 | 24 | if (USING_CPANEL_JSON_XS) { | |||||||||||||||||||
| 31 | 24 | $js->convert_blessed; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | 24 | $js; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | sub cli_encode_json { | ||||||||||||||||||||
| 37 | 24 | ▶ | (my MY $self, my ($obj, $json_type)) = @_; | ||||||||||||||||||
|
Subroutine
cli_encode_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 38 | 24 | my $json = $self->cli_encode_json_as_bytes($obj, $json_type); | |||||||||||||||||||
| 39 | 24 | $json; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | sub cli_encode_json_as_bytes { | ||||||||||||||||||||
| 43 | 24 | ▶ | (my MY $self, my ($obj, $json_type)) = @_; | ||||||||||||||||||
|
Subroutine
cli_encode_json_as_bytes: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||
| 44 | 24 | my $codec = $self->cli_json_encoder; | |||||||||||||||||||
| 45 | 24 | my @opts; | |||||||||||||||||||
| 46 | 24 | my $json = do { | |||||||||||||||||||
| 47 | 24 | ▶ | if (not USING_CPANEL_JSON_XS) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 48 | $codec->encode($obj); | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | elsif (defined $json_type) { | ||||||||||||||||||||
| 51 | 3 | ▶ | $codec->encode($obj, $self->cli_json_type_of($json_type) // $json_type); | ||||||||||||||||||
|
Condition33%1/3
$self->cli_json_type_of($json_type) // $json_type
MC/DC0%0/2
$self->cli_json_type_of($json_type) // $json_type
$self->cli_json_type_of($json_type)
$json_type
Truth table33%1/3
A // B
A= $self->cli_json_type_of($json_type)B= $json_type
| |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | elsif (my $sub = UNIVERSAL::can($obj, 'TO_JSON')) { | ||||||||||||||||||||
| 54 | 3 | my $conv = $sub->($obj); | |||||||||||||||||||
| 55 | 3 | ▶ | if (ref $conv eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 2 | $codec->encode($conv, $self->cli_json_type_of(ref $obj)); | |||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||
| 58 | 1 | $codec->encode($conv); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | ▶ | elsif (ref $obj and my @opts = $self->cli_json_type_of(ref $obj)) { | |||||||||||||||||||
|
Condition33%1/3
ref $obj and my(@opts) = $self->cli_json_type_of(ref $obj)
MC/DC0%0/2
ref $obj and my(@opts) = $self->cli_json_type_of(ref $obj)
ref $obj
my(@opts) = $self->cli_json_type_of(ref $obj)
Truth table33%1/3
A and B
A= ref $objB= my(@opts) = $self->cli_json_type_of(ref $obj)
| |||||||||||||||||||||
| 62 | 18 | $codec->encode($obj, @opts); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||
| 65 | 0 | $codec->encode($obj); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||
| 68 | 24 | $json; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 13 | 1; | |||||||||||||||||||