| 1 | # ABSTRACT: Add TO_JSON to your packages without the boilerplate | ||||||||||||||||||||||||||||||
| 2 | package Package::JSONable; | ||||||||||||||||||||||||||||||
| 3 | { | ||||||||||||||||||||||||||||||
| 4 | 4 | $Package::JSONable::VERSION = '0.001'; | |||||||||||||||||||||||||||||
| 5 | } | ||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use List::MoreUtils qw(none); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use JSON (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | sub import { | ||||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | 8 | my ( $class, %import_opts ) = @_; | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||
| 17 | 8 | my ( $target ) = caller; | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | 7 | ▶ | my $to_json = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||||
| 20 | 7 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 7 | ▶ | $self = $target unless $self; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||
| 24 | 7 | ▶ | %opts = %import_opts unless %opts; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 7 | my @types = qw/Str Int Num Bool ArrayRef HashRef/; | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | 7 | my %hash; | |||||||||||||||||||||||||||||
| 29 | 7 | foreach my $method ( keys %opts ) { | |||||||||||||||||||||||||||||
| 30 | 25 | my $type = $opts{$method}; | |||||||||||||||||||||||||||||
| 31 | 25 | my @value = $self->$method; | |||||||||||||||||||||||||||||
| 32 | 24 | my ( $value ) = @value; | |||||||||||||||||||||||||||||
| 33 | 24 | my $reftype = reftype $value; | |||||||||||||||||||||||||||||
| 34 | 24 | my $typetype = reftype $type; | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | 24 | ▶ | if ($typetype) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | croak sprintf('Invalid type: "%s"', $typetype) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 38 | if $typetype ne 'CODE'; | ||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | 3 | $hash{$method} = $type->($self, @value); | |||||||||||||||||||||||||||||
| 41 | 3 | next; | |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | croak sprintf('Invalid type: "%s"', $type) | ||||||||||||||||||||||||||||||
| 45 | 20 | ▶ | if none { /^$type$/ } @types; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 19 | ▶ | if (!defined $value && $type ne 'Bool') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value and $type ne "Bool"
MC/DC100%2/2
not defined $value and $type ne "Bool"
not defined $value
$type ne "Bool"
Truth table100%3/3
not(A) and B
A= not defined $valueB= $type ne "Bool"
| |||||||||||||||||||||||||||||||
| 48 | 2 | $hash{$method} = $value; | |||||||||||||||||||||||||||||
| 49 | 2 | next; | |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 17 | ▶ | if ( $type eq 'Str' ) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 53 | 3 | $hash{$method} = $value . ""; | |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||
| 55 | elsif ( $type eq 'Int' ) { | ||||||||||||||||||||||||||||||
| 56 | 2 | $hash{$method} = int $value; | |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||
| 58 | elsif ( $type eq 'Num' ) { | ||||||||||||||||||||||||||||||
| 59 | 2 | $hash{$method} = $value += 0; | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | elsif ( $type eq 'ArrayRef' ) { | ||||||||||||||||||||||||||||||
| 62 | 4 | ▶ | if ($reftype && $reftype eq 'ARRAY') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$reftype and $reftype eq "ARRAY"
MC/DC50%1/2
$reftype and $reftype eq "ARRAY"
$reftype
$reftype eq "ARRAY"
Truth table66%2/3
A and B
A= $reftypeB= $reftype eq "ARRAY"
| |||||||||||||||||||||||||||||||
| 63 | 2 | $hash{$method} = $value; | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||
| 66 | 2 | $hash{$method} = [ @value ]; | |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||
| 69 | elsif ( $type eq 'HashRef' ) { | ||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | if ($reftype && $reftype eq 'HASH') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$reftype and $reftype eq "HASH"
MC/DC50%1/2
$reftype and $reftype eq "HASH"
$reftype
$reftype eq "HASH"
Truth table66%2/3
A and B
A= $reftypeB= $reftype eq "HASH"
| |||||||||||||||||||||||||||||||
| 71 | 2 | $hash{$method} = $value; | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||||
| 74 | 2 | $hash{$method} = { @value }; | |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | elsif ( $type eq 'Bool' ) { | ||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if ( $value ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 79 | 0 | $hash{$method} = JSON::true; | |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||||
| 82 | 2 | $hash{$method} = JSON::false; | |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | 4 | return \%hash; | |||||||||||||||||||||||||||||
| 88 | 8 | }; | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 91 | 8 | *{"${target}::TO_JSON"} = $to_json; | |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | 4 | 1; | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||