File: Serializer.pm

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (114 lines) | stat: -rw-r--r-- 2,237 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package Catmandu::Serializer;

use Catmandu::Sane;

our $VERSION = '1.2024';

use Catmandu::Util qw(require_package);
use Moo::Role;
use namespace::clean;

has serialization_format =>
    (is => 'ro', builder => 'default_serialization_format',);

has serializer => (
    is      => 'ro',
    lazy    => 1,
    builder => '_build_serializer',
    handles => [qw(serialize deserialize)]
);

sub default_serialization_format {'json'}

sub _build_serializer {
    my ($self) = @_;
    my $pkg = require_package($self->serialization_format,
        'Catmandu::Serializer');
    $pkg->new;
}

# Implementer needs to be create a serializer
# sub serialize {}

# Implementers needs to be create a deserializer
# sub deserialize {}

1;

__END__

=pod

=head1 NAME

Catmandu::Serializer - Base class for all Catmandu Serializers

=head1 SYNOPSIS

    package Catmandu::Serializer::Foo;

    use Moo;

    sub serialize {
        my ($self,$data) = @_;
        .. transform the data to a string and return it...
    }

    sub deserialize {
        my ($self,$string) = @_;
        ... transform the string into a perl hash ...
    }

    package MyPackage;

    use Moo;

    with 'Catmandu::Serializer';

    package main;

    my $pkg = MyPackage->new;

    my $string = $pkg->serialize({ foo => 'bar' });
    my $perl   = $pkg->deserialize($string);

    # Using Catmandu::Serializer::Foo 
    my $pkg = MyPackage->new( serialization_format => 'Foo' );

    my $string = $pkg->serialize({ foo => 'bar' });
    my $perl   = $pkg->deserialize($string);
	
=head1 DESCRIPTION

This is a convience class to send Perl hashes easily over the wire without having to
instantiate a L<Catmandu::Importer> and L<Catmandu::Exporter> which are more suited for
processing IO streams.

=head1 ATTRIBUTES

=head1 serialization_format

The name of the package that serializes data.

=head1 serializer

An instance of the package that serializes.

=head1 METHODS

=head2 serialize($perl)

Serialize a perl data structure into a string.

=head2 deserialize($bytes)

Deserialize bytes into a perl data structure.

=head1 SEE ALSO

L<Catmandu::Store::DBI>, 
L<Catmandu::Serializer::json>,
L<Catmandu::Serializer::storabe>,
L<Catmandu::Serializer::messagepack>

=cut