App::Serializer::TextArray - Interface for serialization and deserialization


App-Context documentation Contained in the App-Context distribution.

Index


Code Index:

NAME

Top

App::Serializer::TextArray - Interface for serialization and deserialization

SYNOPSIS

Top

    use App;

    $context = App->context();
    $serializer = $context->service("Serializer");  # or ...
    $serializer = $context->serializer();
    $data = {
        an => 'arbitrary',
        collection => [ 'of', 'data', ],
        of => {
            arbitrary => 'depth',
        },
    };
    $text = $serializer->serialize($data);
    $data = $serializer->deserialize($text);
    print $serializer->dump($data), "\n";

DESCRIPTION

Top

A Serializer allows you to serialize a structure of data of arbitrary depth to a scalar and deserialize it back to the structure.

The TextArray serializer uses a set of vertical bar ("|") delimited lines as a way of serializing a perl array. This serializer is only useful for serializing arrays.

Class: App::Serializer::TextArray

Top

 * Throws: App::Exception::Serializer
 * Since:  0.01

Design

The class is entirely made up of static (class) methods. However, they are each intended to be called as methods on the instance itself.

Constructor Methods:

Top

new()

The constructor is inherited from App::Service|App::Service/"new()".

Public Methods:

Top

serialize()

    * Signature: $text = $serializer->serialize(@data);
    * Param:     @data             any
    * Return:    $text             text
    * Throws:    App::Exception::Serializer
    * Since:     0.01

    Sample Usage: 

    $context = App->context();
    $serializer = $context->service("Serializer");  # or ...
    $serializer = $context->serializer();
    $data = {
        an => 'arbitrary',
        collection => [ 'of', 'data', ],
        of => {
            arbitrary => 'depth',
        },
    };
    $text = $serializer->serialize($data);

deserialize()

    * Signature: @data = $serializer->deserialize($text);
    * Signature: @data = App::Serializer->deserialize($text);
    * Param:     $text          text
    * Return:    @data          any
    * Throws:    App::Exception::Serializer
    * Since:     0.01

    Sample Usage: 

    $context = App->context();
    $serializer = $context->service("Serializer");  # or ...
    $serializer = $context->serializer();
    $data = $serializer->deserialize($text);
    print $serializer->dump($data), "\n";

serialized_content_type()

    * Signature: $serialized_content_type = $service->serialized_content_type();
    * Param:     void
    * Return:    $serialized_content_type   string
    * Throws:    App::Exception
    * Since:     0.01

    Sample Usage: 

    $serialized_content_type = $service->serialized_content_type();

dump()

This method is inherited from App::Serializer|App::Serializer/"dump()".

ACKNOWLEDGEMENTS

Top

 * Author:  Stephen Adkins <spadkins@gmail.com>
 * License: This is free software. It is licensed under the same terms as Perl itself.

SEE ALSO

Top

App::Context|App::Context, App::Service|App::Service


App-Context documentation Contained in the App-Context distribution.
#############################################################################
## $Id: TextArray.pm 6783 2006-08-11 17:43:28Z spadkins $
#############################################################################

package App::Serializer::TextArray;
$VERSION = (q$Revision: 6783 $ =~ /(\d[\d\.]*)/)[0];  # VERSION numbers generated by svn

use App;
use App::Serializer;
@ISA = ( "App::Serializer" );

use strict;

#############################################################################
# CLASS
#############################################################################

#############################################################################
# CONSTRUCTOR METHODS
#############################################################################

#############################################################################
# new()
#############################################################################

#############################################################################
# PUBLIC METHODS
#############################################################################

#############################################################################
# serialize()
#############################################################################

sub serialize {
    my ($self, $array) = @_;
    die "Tried to serialize non-array ($array) with TextArray serializer" if (ref($array) ne "ARRAY");
    my $text = "";
    foreach my $row (@$array) {
       $text .= join("|", map { (defined $_) ? $_ : "undef" } @$row) . "\n";
    }
    return $text;
}

#############################################################################
# deserialize()
#############################################################################

sub deserialize {
    my ($self, $text) = @_;
    my $array = [];
    my ($row, @rows);
    chomp($text);
    @rows = split(/\n/,$text);
    foreach my $line (@rows) {
        $row = [ map { $_ eq "undef" ? undef : $_ } split(/\|/,$line) ];
        push(@$array, $row);
    }
    return($array);
}

#############################################################################
# serialized_content_type()
#############################################################################

sub serialized_content_type {
    'text/plain';
}

#############################################################################
# dump()
#############################################################################

1;