File Coverage

blib/lib/Dancer2/Serializer/Dumper.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 22 22 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Serializer for handling Dumper data
2              
3             package Dancer2::Serializer::Dumper;
4             $Dancer2::Serializer::Dumper::VERSION = '2.0.1';
5 5     5   324598 use Moo;
  5         17054  
  5         35  
6 5     5   5194 use Carp 'croak';
  5         9  
  5         311  
7 5     5   1153 use Data::Dumper;
  5         17232  
  5         266  
8 5     5   2691 use Safe;
  5         65775  
  5         1346  
9              
10             with 'Dancer2::Core::Role::Serializer';
11              
12             has '+content_type' => ( default => sub {'text/x-data-dumper'} );
13              
14             # helpers
15 2     2 1 86 sub from_dumper { __PACKAGE__->deserialize(@_) }
16              
17 7     7 1 249 sub to_dumper { __PACKAGE__->serialize(@_) }
18              
19             # class definition
20             sub serialize {
21             my ( $self, $entity ) = @_;
22              
23             {
24             local $Data::Dumper::Purity = 1;
25             return Dumper($entity);
26             }
27             }
28              
29             sub deserialize {
30             my ( $self, $content ) = @_;
31              
32             my $cpt = Safe->new;
33              
34             my $res = $cpt->reval("my \$VAR1; $content");
35             croak "unable to deserialize : $@" if $@;
36             return $res;
37             }
38              
39             1;
40              
41             __END__