| 1 | package JSON::UnblessObject; | ||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 3 | ▶ | use parent qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 3 | our $VERSION = "0.02"; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 3 | our @EXPORT_OK = qw( | |||||||||||||||||||
| 10 | unbless_object | ||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 3 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 3 | ▶ | use List::Util qw(any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 3 | ▶ | use overload (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 3 | ▶ | use constant JSON_TYPE_ARRAYOF_CLASS => 'Cpanel::JSON::XS::Type::ArrayOf'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 3 | ▶ | use constant JSON_TYPE_HASHOF_CLASS => 'Cpanel::JSON::XS::Type::HashOf'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 3 | ▶ | use constant JSON_TYPE_ANYOF_CLASS => 'Cpanel::JSON::XS::Type::AnyOf'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 87 | ▶ | sub unbless_object { | ||||||||||||||||||
|
Subroutine
unbless_object: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||
| 24 | 87 | my ($object, $spec) = @_; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 87 | ▶ | return $object unless blessed($object); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 42 | ▶ | if (blessed $spec) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 15 | ▶ | return resolve_json_type_arrayof($object, $spec) if $spec->isa(JSON_TYPE_ARRAYOF_CLASS); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 11 | ▶ | return resolve_json_type_hashof($object, $spec) if $spec->isa(JSON_TYPE_HASHOF_CLASS); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 5 | ▶ | return resolve_json_type_anyof($object, $spec) if $spec->isa(JSON_TYPE_ANYOF_CLASS); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | Carp::croak sprintf("'%s' object not supported spec", $spec); | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 27 | ▶ | if (my $ref = ref $spec) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 21 | ▶ | return resolve_arrayref($object, $spec) if $ref eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 14 | ▶ | return resolve_hashref($object, $spec) if $ref eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 2 | Carp::croak sprintf("'%s' reference not supported spec", $spec); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 6 | return $object; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 11 | ▶ | sub list { | ||||||||||||||||||
|
Subroutine
list: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 48 | 11 | my ($object) = @_; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 11 | ▶ | if (my $to_list = overload::Method($object,'@{}')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 5 | return $to_list->($object); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 6 | ▶ | if ($object->can('next')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 5 | my @list; | |||||||||||||||||||
| 56 | 5 | ▶ | while (defined (my $v = $object->next)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 11 | push @list => $v; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 5 | return \@list; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 1 | Carp::croak sprintf("'%s' object could not be converted to array ref", $object); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 4 | ▶ | sub available_array { | ||||||||||||||||||
|
Subroutine
available_array: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 67 | 4 | my ($object) = @_; | |||||||||||||||||||
| 68 | 4 | ▶ | my $f = overload::Method($object, '@{}') || $object->can('next'); | ||||||||||||||||||
|
Condition100%3/3
overload::Method($object, '@{}') || $object->can('next')
MC/DC100%2/2
overload::Method($object, '@{}') || $object->can('next')
overload::Method($object, '@{}')
$object->can('next')
Truth table100%3/3
A || B
A= overload::Method($object, '@{}')B= $object->can('next')
| |||||||||||||||||||||
| 69 | 4 | return !!$f | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 2 | ▶ | sub available_hash { | ||||||||||||||||||
|
Subroutine
available_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 74 | 2 | my ($object) = @_; | |||||||||||||||||||
| 75 | 2 | my $f = $object->can('JSON_KEYS'); | |||||||||||||||||||
| 76 | 2 | return !!$f; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 7 | ▶ | sub resolve_arrayref { | ||||||||||||||||||
|
Subroutine
resolve_arrayref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 81 | 7 | my ($object, $spec) = @_; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 7 | my @data; | |||||||||||||||||||
| 84 | 7 | my $list = list($object); | |||||||||||||||||||
| 85 | 6 | for my $i (0 .. $#$spec) { | |||||||||||||||||||
| 86 | 6 | my $v = $list->[$i]; | |||||||||||||||||||
| 87 | 6 | my $s = $spec->[$i]; | |||||||||||||||||||
| 88 | 6 | push @data => unbless_object($v, $s); | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | 6 | return \@data; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 12 | ▶ | sub resolve_hashref { | ||||||||||||||||||
|
Subroutine
resolve_hashref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 95 | 12 | my ($object, $spec) = @_; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 12 | my %data; | |||||||||||||||||||
| 98 | 12 | for my $key (keys %$spec) { | |||||||||||||||||||
| 99 | 27 | my $v = $object->$key; | |||||||||||||||||||
| 100 | 27 | my $s = $spec->{$key}; | |||||||||||||||||||
| 101 | 27 | $data{$key} = unbless_object($v, $s) | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | 12 | return \%data; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 4 | ▶ | sub resolve_json_type_arrayof { | ||||||||||||||||||
|
Subroutine
resolve_json_type_arrayof: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 108 | 4 | my ($object, $spec) = @_; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 4 | my $s = $$spec; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 4 | my @data; | |||||||||||||||||||
| 113 | 4 | my $list = list($object); | |||||||||||||||||||
| 114 | 4 | for my $v (@$list) { | |||||||||||||||||||
| 115 | 9 | push @data => unbless_object($v, $s); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | 4 | return \@data; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 6 | ▶ | sub resolve_json_type_hashof { | ||||||||||||||||||
|
Subroutine
resolve_json_type_hashof: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 122 | 6 | my ($object, $spec) = @_; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 6 | my $s = $$spec; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 6 | ▶ | if ($object->can('JSON_KEYS')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 5 | my %data; | |||||||||||||||||||
| 128 | 5 | for my $key ($object->JSON_KEYS) { | |||||||||||||||||||
| 129 | 5 | my $v = $object->$key; | |||||||||||||||||||
| 130 | 5 | $data{$key} = unbless_object($v, $s) | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 5 | return \%data; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||
| 135 | 1 | Carp::croak sprintf("'%s' object could not call JSON_KEYS method", $object); | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 4 | ▶ | sub resolve_json_type_anyof { | ||||||||||||||||||
|
Subroutine
resolve_json_type_anyof: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 141 | 4 | my ($object, $spec) = @_; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 4 | ▶ | my $s = available_array($object) ? $spec->[1] | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 144 | : available_hash($object) ? $spec->[2] | ||||||||||||||||||||
| 145 | : $spec->[0]; | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 4 | return unbless_object($object, $s); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 3 | 1; | |||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||