File Coverage

blib/lib/Object/Serializer.pm
Criterion Covered Total %
statement 55 61 90.1
branch 4 10 40.0
condition 5 18 27.7
subroutine 12 14 85.7
pod 2 3 66.6
total 78 106 73.5


line stmt bran cond sub pod time code
1             # ABSTRACT: General Purpose Object Serializer
2             package Object::Serializer;
3              
4 2     2   30790 use utf8;
  2         4  
  2         18  
5 2     2   93 use 5.010;
  2         6  
  2         85  
6 2     2   26 use strict;
  2         3  
  2         75  
7 2     2   10 use warnings;
  2         3  
  2         65  
8 2     2   2220 use Data::Dumper ();
  2         19181  
  2         59  
9 2     2   24 use Scalar::Util qw(blessed refaddr);
  2         2  
  2         917  
10             our %TYPES;
11              
12             our $VERSION = '0.000011'; # VERSION
13              
14              
15             sub new {
16 0     0 0 0 bless {}, shift
17             }
18              
19             sub _serialization {
20 4     4   8 my ($self, $options, $reference, $class) = @_;
21 4   33     19 my @registries = (ref($self) || $self, __PACKAGE__);
22              
23 4         21 for my $registry (@registries) {
24 8         15 my $type = $TYPES{$registry};
25 8 50       32 next unless 'HASH' eq ref $type;
26 0         0 my $coercion = $type->{$class};
27 0 0       0 return $coercion->(bless $reference, $class)
28             if 'CODE' eq ref $coercion;
29             }
30              
31             # tag blessed hash references
32 4 100 66     28 $reference->{$options->{marker}} = $class
33             if defined $options->{marker} && 'HASH' eq ref $reference;
34              
35 4         60 return $reference;
36             }
37              
38              
39             sub serialize {
40 2     2 1 3199 my ($self, $object, %options) = @_;
41              
42 2         5 local $Data::Dumper::Terse = 1;
43 2         572 local $Data::Dumper::Indent = 0;
44 2         4 local $Data::Dumper::Useqq = 1;
45 2         3 local $Data::Dumper::Deparse = 1;
46 2         4 local $Data::Dumper::Quotekeys = 0;
47 2         3 local $Data::Dumper::Sortkeys = 1;
48 2         2 local $Data::Dumper::Deepcopy = 1;
49 2         5 local $Data::Dumper::Purity = 0;
50              
51 2         7 my $options = {marker => '__CLASS__', %options};
52 2         13 my $execution = join '::', __PACKAGE__, 'Runtime', refaddr $self;
53              
54 2     2   12 no strict 'refs';
  2         4  
  2         85  
55 2     2   10 no warnings 'redefine';
  2         4  
  2         329  
56              
57 2         12 *{$execution} = sub {
58 4     4   15 my @arguments = @_;
59 4         13 Object::Serializer::_serialization($self, $options, @arguments);
60 2         10 };
61              
62 2   66     23 (my $target = Data::Dumper::Dumper($object // $self)) =~
63             s/bless(?=(?:(?:(?:[^"\\]++|\\.)*+"){2})*+(?:[^"\\]++|\\.)*+$)/$execution/g;
64              
65 2 50   2   10 my $hash = do { no strict; eval "my \$VAR1 = $target\n" } or die $@;
  2         4  
  2         351  
  2         10858  
  2         358  
66              
67 2         9 undef *{$execution};
  2         15  
68 2         43 return $hash;
69             }
70              
71              
72             sub serialization_strategy_for {
73 0     0 1   my ($self, $reftype, $routine) = @_;
74              
75 0 0 0       die "Couldn't register reftype serialization " .
      0        
76             "strategy due to invalid arguments"
77             unless $self && $reftype && 'CODE' eq ref $routine;
78              
79 0   0       return $TYPES{ref($self) || $self}{$reftype} = $routine;
80             }
81              
82              
83             1;
84              
85             __END__