line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Serializer; |
2
|
|
|
|
|
|
|
|
3
|
36
|
|
|
36
|
|
121438
|
use Catmandu::Sane; |
|
36
|
|
|
|
|
111
|
|
|
36
|
|
|
|
|
246
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
36
|
|
|
36
|
|
303
|
use Catmandu::Util qw(require_package); |
|
36
|
|
|
|
|
91
|
|
|
36
|
|
|
|
|
1672
|
|
8
|
36
|
|
|
36
|
|
304
|
use Moo::Role; |
|
36
|
|
|
|
|
82
|
|
|
36
|
|
|
|
|
230
|
|
9
|
36
|
|
|
36
|
|
14404
|
use namespace::clean; |
|
36
|
|
|
|
|
102
|
|
|
36
|
|
|
|
|
244
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has serialization_format => |
12
|
|
|
|
|
|
|
(is => 'ro', builder => 'default_serialization_format',); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has serializer => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
lazy => 1, |
17
|
|
|
|
|
|
|
builder => '_build_serializer', |
18
|
|
|
|
|
|
|
handles => [qw(serialize deserialize)] |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
107
|
|
|
107
|
0
|
7229
|
sub default_serialization_format {'json'} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _build_serializer { |
24
|
2
|
|
|
2
|
|
667
|
my ($self) = @_; |
25
|
2
|
|
|
|
|
23
|
my $pkg = require_package($self->serialization_format, |
26
|
|
|
|
|
|
|
'Catmandu::Serializer'); |
27
|
2
|
|
|
|
|
17
|
$pkg->new; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Implementer needs to be create a serializer |
31
|
|
|
|
|
|
|
# sub serialize {} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Implementers needs to be create a deserializer |
34
|
|
|
|
|
|
|
# sub deserialize {} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
1; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
__END__ |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=pod |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 NAME |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Catmandu::Serializer - Base class for all Catmandu Serializers |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 SYNOPSIS |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
package Catmandu::Serializer::Foo; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
use Moo; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub serialize { |
53
|
|
|
|
|
|
|
my ($self,$data) = @_; |
54
|
|
|
|
|
|
|
.. transform the data to a string and return it... |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub deserialize { |
58
|
|
|
|
|
|
|
my ($self,$string) = @_; |
59
|
|
|
|
|
|
|
... transform the string into a perl hash ... |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
package MyPackage; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
use Moo; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
with 'Catmandu::Serializer'; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
package main; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $pkg = MyPackage->new; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $string = $pkg->serialize({ foo => 'bar' }); |
73
|
|
|
|
|
|
|
my $perl = $pkg->deserialize($string); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Using Catmandu::Serializer::Foo |
76
|
|
|
|
|
|
|
my $pkg = MyPackage->new( serialization_format => 'Foo' ); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $string = $pkg->serialize({ foo => 'bar' }); |
79
|
|
|
|
|
|
|
my $perl = $pkg->deserialize($string); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 DESCRIPTION |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This is a convience class to send Perl hashes easily over the wire without having to |
84
|
|
|
|
|
|
|
instantiate a L<Catmandu::Importer> and L<Catmandu::Exporter> which are more suited for |
85
|
|
|
|
|
|
|
processing IO streams. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 serialization_format |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
The name of the package that serializes data. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 serializer |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
An instance of the package that serializes. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 METHODS |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 serialize($perl) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Serialize a perl data structure into a string. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 deserialize($bytes) |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Deserialize bytes into a perl data structure. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 SEE ALSO |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
L<Catmandu::Store::DBI>, |
110
|
|
|
|
|
|
|
L<Catmandu::Serializer::json>, |
111
|
|
|
|
|
|
|
L<Catmandu::Serializer::storabe>, |
112
|
|
|
|
|
|
|
L<Catmandu::Serializer::messagepack> |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=cut |