NAME
CTK::Serializer - Base class for serialization perl structures
VERSION
Version 1.01
SYNOPSIS
use CTK::Serializer;
my $sr = CTK::Serializer->new( DEFAULT_FORMAT,
attrs => {
xml => [
{ # For serialize
RootName => "request",
},
{ # For deserialize
ForceArray => 1,
ForceContent => 1,
}
],
json => [
{ # For serialize
utf8 => 0,
pretty => 1,
allow_nonref => 1,
allow_blessed => 1,
},
{ # For deserialize
utf8 => 0,
allow_nonref => 1,
allow_blessed => 1,
},
],
},
);
my $doc = $sr->serialize( xml => { foo => 1, bar => 2}, {
RootName => "request",
});
my $doc = $sr->document;
my $perl = $sr->deserialize( xml => $doc, {
ForceArray => 1,
ForceContent => 1,
});
my $perl = $sr->struct;
my $status = $sr->status; # 0 - Error, 1 - Ok
my $error = $sr->error;
my $MIME_type = $sr->content_type;
DESCRIPTION
This module provides access to serialization mechanism with support extending. Now allowed to use follows formats of serialization in this base class: XML, YAML, JSON and "none" for automatic format detecting
new
my $sr = CTK::Serializer->new( $format );
If specified $format in constructor call then this format sets as default
my $sr = CTK::Serializer->new( $format,
attrs => {
format => [
{...serialize attr...}, # for serialize
{...deserialize attr...}, # for deserialize
],
},
);
If an attribute is specified, the passed values are substituted for the call attributes of the corresponding serializer and deserializer
Supported formats:
XML
-
XML serialization via XML::Simple module
JSON
-
JSON serialization via JSON (JSON::XS if installed) module
YAML
-
YAML serialization via YAML::XS module
Also exists non-formal format "none". This format allows serialization in a perl-dump using the Data::Dumper; deserialization based on the format lookup mechanism based on the data signature.
For Example:
my $sr = CTK::Serializer();
my $perl = $sr->deserialize( $doc );
In this example, the format is detecting automatically
deserialize
my $perl = $sr->deserialize( $format => $doc, $attrs );
my $perl = $sr->deserialize( $doc );
The method returns deserialized structure of the document using specified format. The optional $attrs variable contents attributes of serialization module (hash-ref)
document, doc
my $doc = $sr->serialize( $perl );
my $doc = $sr->document;
This method returns document from last operation serialize or deserialize
error
my $error = $sr->error;
Returns current error or NULL value ("") if no errors occurred
format
my $format = $sr->format;
my $format = $sr->format( $new_format );
Format accessor
content_type
my $content_type = $sr->content_type;
my $content_type = $sr->content_type( $format );
Returns MIME-type for format
get_list
my @supported_serializers = $sr->get_list;
Returns list of supported serializers (their names)
lookup
my $node = $sr->lookup( $format );
Looks for format attributes
register_serializer
This method uses for extension of this base class. See source code
serialize
my $doc = $sr->serialize( $format => $perl, $attrs );
my $doc = $sr->serialize( $perl );
The method returns serialized document of the structure using specified format. The optional $attrs variable contents attributes of serialization module (hash-ref)
stash
For internal use only. See source code
status
my $status = $sr->status;
Returns 1 if no errors and 0 if errors occurred
Typical example of use:
die( $sr->error ) unless $sr->status;
struct, structure
my $perl = $sr->deserialize( $doc );
my $perl = $sr->struct;
This method returns structure from last operation serialize or deserialize
HISTORY
See Changes
file
DEPENDENCIES
IO::String, JSON, XML::Simple, YAML::XS
TO DO
See TODO
file
BUGS
* none noted
SEE ALSO
AUTHOR
Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>
COPYRIGHT
Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved
LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See LICENSE
file and https://dev.perl.org/licenses