| App-Context documentation | Contained in the App-Context distribution. |
App::Serializer::Yaml - 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',
},
};
$yaml = $serializer->serialize($data);
$data = $serializer->deserialize($yaml);
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 Yaml serializer uses YAML as the serialized form of the data. It uses the "YAML.pm" module from CPAN to perform the deserialization and serialization.
* 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: $yaml = $serializer->serialize(@data);
* Param: @data any
* Return: $yaml 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',
},
};
$yaml = $serializer->serialize($data);
* Signature: @data = $serializer->deserialize($yaml);
* Signature: @data = App::Serializer->deserialize($yaml);
* Param: $yaml 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($yaml);
print $serializer->dump($data), "\n";
* 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();
This method 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: Yaml.pm 6783 2006-08-11 17:43:28Z spadkins $ ############################################################################# package App::Serializer::Yaml; $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() #############################################################################
use YAML; sub serialize { my ($self, $data) = @_; my ($yaml); if (ref($data) eq "ARRAY") { $yaml = Dump(@$data); } else { $yaml = Dump($data); } return $yaml; } ############################################################################# # deserialize() #############################################################################
sub deserialize { my ($self, $yaml) = @_; my (@data) = Load($yaml); if ($#data > 0) { return(\@data); } else { return($data[0]); } } ############################################################################# # serialized_content_type() #############################################################################
sub serialized_content_type { 'text/yaml'; } ############################################################################# # dump() #############################################################################
1;