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


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

Index


Code Index:

NAME

Top

App::Serializer::Properties - 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',
        },
    };
    $propdata = $serializer->serialize($data);
    $data = $serializer->deserialize($propdata);
    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 Properties serializer reads and writes data which conforms to the standards of Java properties files.

Class: App::Serializer::Properties

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: $propdata = $serializer->serialize($data);
    * Param:     $data              ref
    * Return:    $propdata           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',
        },
    };
    $propdata = $serializer->serialize($data);

deserialize()

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

    Sample Usage: 

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

dump()

The constructor 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: Properties.pm 6783 2006-08-11 17:43:28Z spadkins $
#############################################################################

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

use App;
use App::Serializer;

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

use App::Reference;

use strict;

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

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

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

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

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

sub serialize {
    my ($self, $data) = @_;
    $self->_serialize($data, "");
}

sub _serialize {
    my ($self, $data, $section) = @_;
    my ($section_data, $idx, $key, $elem, $prefix);
    $prefix = "";
    $prefix = "$section." if (defined $section && $section ne "");
    if (ref($data) eq "ARRAY") {
        for ($idx = 0; $idx <= $#$data; $idx++) {
            $elem = $data->[$idx];
            if (!ref($elem)) {
                $section_data .= "$prefix$idx = $elem\n";
            }
            else {
                $section_data .= $self->_serialize($elem, "$prefix$idx");
            }
        }
    }
    elsif (ref($data)) {
        foreach $key (sort keys %$data) {
            $elem = $data->{$key};
            if (!ref($elem)) {
                $section_data .= "$prefix$key = $elem\n";
            }
            else {
                $section_data .= $self->_serialize($elem, "$prefix$key");
            }
        }
    }

    return $section_data;
}

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

sub deserialize {
    my ($self, $propdata) = @_;
    my ($data, $r, $line, $attrib, $value);

    $r = App::Reference->new(); # dummy ref (shorthand for static calls)
    $data = {};

    foreach $line (split(/\n/, $propdata)) {
        next if ($line =~ /^#/);  # ignore comments
        if ($line =~ /^ *([^ =]+) *= *(.*)$/) {
            $attrib = $1;
            $value = $2;
            $r->set($attrib, $value, $data);
        }
    }

    return $data;
}

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

1;