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.1.0';
5 5     5   446721 use Moo;
  5         18008  
  5         39  
6 5     5   5403 use Carp 'croak';
  5         11  
  5         312  
7 5     5   1240 use Data::Dumper;
  5         17690  
  5         343  
8 5     5   2851 use Safe;
  5         54006  
  5         1234  
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 62 sub from_dumper { __PACKAGE__->deserialize(@_) }
16              
17 7     7 1 269 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__