| 1 | package Catmandu::Serializer; | ||
| 2 | |||
| 3 | 30 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 30 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 30 | ▶ | use Catmandu::Util qw(require_package); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 30 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 30 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 30 | has serialization_format => | |
| 12 | (is => 'ro', builder => 'default_serialization_format',); | ||
| 13 | |||
| 14 | 30 | has serializer => ( | |
| 15 | is => 'ro', | ||
| 16 | lazy => 1, | ||
| 17 | builder => '_build_serializer', | ||
| 18 | handles => [qw(serialize deserialize)] | ||
| 19 | ); | ||
| 20 | |||
| 21 | 82 | ▶ | sub default_serialization_format {'json'} |
|
Subroutine
default_serialization_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 22 | |||
| 23 | sub _build_serializer { | ||
| 24 | 2 | ▶ | my ($self) = @_; |
|
Subroutine
_build_serializer: called CC=1 SCAR=0.0
| |||
| 25 | 2 | my $pkg = require_package($self->serialization_format, | |
| 26 | 'Catmandu::Serializer'); | ||
| 27 | 2 | $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 | 30 | 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 | ||