| 1 | 1 | ▶ | use 5.008009; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | package Types::JsonCoercions; | ||||||||||||
| 6 | |||||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||
| 8 | 1 | our $VERSION = '0.001'; | |||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use Type::Library 1.004 | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | -base, | |||||||||||
| 12 | 1 | -declare => qw( ArrayRefJ HashRefJ RefJ StrJ ); | |||||||||||
| 13 | 1 | ▶ | use Types::Standard 1.004 qw( Str Ref ArrayRef HashRef ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 1 | our $JSON; | |||||||||||
| 16 | |||||||||||||
| 17 | 4 | ▶ | sub _code_to_load_package { | ||||||||||
|
Subroutine
_code_to_load_package: called CC=2 SCAR=6.9
| |||||||||||||
| 18 | 4 | my ( $me, $pkg, $keyword ) = ( shift, @_ ); | |||||||||||
| 19 | 4 | ▶ | $keyword ||= 'do'; | ||||||||||
|
Condition100%2/2
$keyword ||= "do"
MC/DC100%1/1
$keyword ||= "do"
$keyword
Truth table100%2/2
A
A= $keyword
| |||||||||||||
| 20 | 4 | return sprintf( | |||||||||||
| 21 | '%s { require %s; q[%s] }', | ||||||||||||
| 22 | $keyword, | ||||||||||||
| 23 | $pkg, | ||||||||||||
| 24 | $pkg, | ||||||||||||
| 25 | ); | ||||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 2 | ▶ | sub _code_for_json_encoder { | ||||||||||
|
Subroutine
_code_for_json_encoder: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 2 | my ( $me ) = ( shift ); | |||||||||||
| 30 | 2 | return sprintf( | |||||||||||
| 31 | '$%s::JSON ||= ( %s or %s )->new', | ||||||||||||
| 32 | $me, | ||||||||||||
| 33 | $me->_code_to_load_package( 'JSON::MaybeXS', 'eval' ), | ||||||||||||
| 34 | $me->_code_to_load_package( 'JSON::PP' ), | ||||||||||||
| 35 | ); | ||||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | 1 | my $meta = __PACKAGE__->meta; | |||||||||||
| 39 | |||||||||||||
| 40 | 1 | my $ToJson = $meta->add_coercion( { | |||||||||||
| 41 | name => 'ToJSON', | ||||||||||||
| 42 | frozen => 1, | ||||||||||||
| 43 | type_coercion_map => [ | ||||||||||||
| 44 | Ref, sprintf( q{ ( %s )->encode( $_ ) }, __PACKAGE__->_code_for_json_encoder ), | ||||||||||||
| 45 | ], | ||||||||||||
| 46 | } ); | ||||||||||||
| 47 | |||||||||||||
| 48 | 1 | my $FromJson = $meta->add_coercion( { | |||||||||||
| 49 | name => 'FromJSON', | ||||||||||||
| 50 | frozen => 1, | ||||||||||||
| 51 | type_coercion_map => [ | ||||||||||||
| 52 | Str, sprintf( q{ ( %s )->decode( $_ ) }, __PACKAGE__->_code_for_json_encoder ), | ||||||||||||
| 53 | ], | ||||||||||||
| 54 | } ); | ||||||||||||
| 55 | |||||||||||||
| 56 | 1 | $meta->add_type( { | |||||||||||
| 57 | name => StrJ, | ||||||||||||
| 58 | parent => Str->plus_coercions( $ToJson ), | ||||||||||||
| 59 | coercion => 1, | ||||||||||||
| 60 | } ); | ||||||||||||
| 61 | |||||||||||||
| 62 | $meta->add_type( { | ||||||||||||
| 63 | name => RefJ, | ||||||||||||
| 64 | parent => Ref->plus_coercions( $FromJson ), | ||||||||||||
| 65 | coercion => 1, | ||||||||||||
| 66 | 0 | ▶ | constraint_generator => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 67 | 0 | Ref->of( @_ )->plus_coercions( $FromJson ); | |||||||||||
| 68 | }, | ||||||||||||
| 69 | 1 | } ); | |||||||||||
| 70 | |||||||||||||
| 71 | $meta->add_type( { | ||||||||||||
| 72 | name => HashRefJ, | ||||||||||||
| 73 | parent => HashRef->plus_coercions( $FromJson ), | ||||||||||||
| 74 | coercion => 1, | ||||||||||||
| 75 | 0 | ▶ | constraint_generator => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 76 | 0 | HashRef->of( @_ )->plus_coercions( $FromJson ); | |||||||||||
| 77 | }, | ||||||||||||
| 78 | 1 | } ); | |||||||||||
| 79 | |||||||||||||
| 80 | $meta->add_type( { | ||||||||||||
| 81 | name => ArrayRefJ, | ||||||||||||
| 82 | parent => ArrayRef->plus_coercions( $FromJson ), | ||||||||||||
| 83 | coercion => 1, | ||||||||||||
| 84 | 1 | ▶ | constraint_generator => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 85 | 1 | ArrayRef->of( @_ )->plus_coercions( $FromJson ); | |||||||||||
| 86 | }, | ||||||||||||
| 87 | 1 | } ); | |||||||||||
| 88 | |||||||||||||
| 89 | 1 | 1; | |||||||||||
| 90 | |||||||||||||
| 91 | __END__ | ||||||||||||