| App-Context documentation | Contained in the App-Context distribution. |
App::Serializer::Ini - Interface for serialization and deserialization
use App;
$context = App->context();
$serializer = $context->service("Serializer"); # or ...
$serializer = $context->serializer();
$data = {
an => 'arbitrary',
collection => [ 'of', 'data', ],
of => {
arbitrary => 'depth',
},
};
$inidata = $serializer->serialize($data);
$data = $serializer->deserialize($inidata);
print $serializer->dump($data), "\n";
A Serializer allows you to serialize a structure of data of arbitrary depth to a scalar and deserialize it back to the structure.
The Ini serializer reads and writes data which conforms to the standards of Windows INI files.
* Throws: App::Exception::Serializer * Since: 0.01
The class is entirely made up of static (class) methods. However, they are each intended to be called as methods on the instance itself.
The constructor is inherited from
App::Service|App::Service/"new()".
* Signature: $inidata = $serializer->serialize($data);
* Param: $data ref
* Return: $inidata 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',
},
};
$inidata = $serializer->serialize($data);
* Signature: $data = $serializer->deserialize($inidata);
* Signature: $data = App::Serializer->deserialize($inidata);
* Param: $data ref
* Return: $inidata text
* Throws: App::Exception::Serializer
* Since: 0.01
Sample Usage:
$context = App->context();
$serializer = $context->service("Serializer"); # or ...
$serializer = $context->serializer();
$data = $serializer->deserialize($inidata);
print $serializer->dump($data), "\n";
The constructor is inherited from
App::Serializer|App::Serializer/"dump()".
* Author: Stephen Adkins <spadkins@gmail.com> * License: This is free software. It is licensed under the same terms as Perl itself.
App::Context|App::Context,
App::Service|App::Service
| App-Context documentation | Contained in the App-Context distribution. |
############################################################################# ## $Id: Ini.pm 6783 2006-08-11 17:43:28Z spadkins $ ############################################################################# package App::Serializer::Ini; $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); if (ref($data) eq "ARRAY") { for ($idx = 0; $idx <= $#$data; $idx++) { $elem = $data->[$idx]; if (!ref($elem)) { $section_data .= "[$section]\n" if (!$section_data && $section); $section_data .= "$idx = $elem\n"; } } for ($idx = 0; $idx <= $#$data; $idx++) { $elem = $data->[$idx]; if (ref($elem)) { $section_data .= $self->_serialize($elem, $section ? "$section.$idx" : $idx); } } } elsif (ref($data)) { foreach $key (sort keys %$data) { $elem = $data->{$key}; if (!ref($elem)) { $section_data .= "[$section]\n" if (!$section_data && $section); $section_data .= "$key = $elem\n"; } } foreach $key (sort keys %$data) { $elem = $data->{$key}; if (ref($elem)) { $section_data .= $self->_serialize($elem, $section ? "$section.$key" : $key); } } } return $section_data; } ############################################################################# # deserialize() #############################################################################
sub deserializex { my ($self, $inidata) = @_; my ($data, $r, @inidata, $line, $branch_name, $branch, $attrib, $value, $idx); $r = App::Reference->new(); # dummy ref (shorthand for static calls) $data = {}; foreach $line (split(/\n/, $inidata)) { next if ($line =~ /^;/); # ignore comments next if ($line =~ /^#/); # ignore comments if ($line =~ /^\[([^\[\]]+)\] *$/) { # i.e. [Repository.default] $branch_name = $1; $branch = $r->get_branch($branch_name,1,$data); } elsif ($line =~ /^ *([^ =]+) *= *(.*)$/) { $attrib = $1; $value = $2; if ($branch) { $r->set($attrib, $value, $branch); } else { $r->set($attrib, $value, $data); } } } return $data; } sub deserialize { my ($self, $inidata) = @_; my ($data, $r, $line, $attrib_base, $attrib, $value); $r = App::Reference->new(); # dummy ref (shorthand for static calls) $data = {}; $attrib_base = ""; foreach $line (split(/\n/, $inidata)) { next if ($line =~ /^;/); # ignore comments next if ($line =~ /^#/); # ignore comments if ($line =~ /^\[([^\[\]]+)\] *$/) { # i.e. [Repository.default] $attrib_base = $1; } if ($line =~ /^ *([^ =]+) *= *(.*)$/) { $attrib = $attrib_base ? "$attrib_base.$1" : $1; $value = $2; $r->set($attrib, $value, $data); } } return $data; } ############################################################################# # dump() #############################################################################
1;