CTK::Serializer - Base class for serialization perl structures
Version 1.01
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;
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
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
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)
my $doc = $sr->serialize( $perl ); my $doc = $sr->document;
This method returns document from last operation serialize or deserialize
my $error = $sr->error;
Returns current error or NULL value ("") if no errors occurred
my $format = $sr->format; my $format = $sr->format( $new_format );
Format accessor
my $content_type = $sr->content_type; my $content_type = $sr->content_type( $format );
Returns MIME-type for format
my @supported_serializers = $sr->get_list;
Returns list of supported serializers (their names)
my $node = $sr->lookup( $format );
Looks for format attributes
This method uses for extension of this base class. See source code
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)
For internal use only. See source code
my $status = $sr->status;
Returns 1 if no errors and 0 if errors occurred
Typical example of use:
die( $sr->error ) unless $sr->status;
my $perl = $sr->deserialize( $doc ); my $perl = $sr->struct;
This method returns structure from last operation serialize or deserialize
Init version
See Changes file
Changes
IO::String, JSON, XML::Simple, YAML::XS
See TODO file
TODO
* none noted
CTK
Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>
Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved
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
LICENSE
To install CTK, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CTK
CPAN shell
perl -MCPAN -e shell install CTK
For more information on module installation, please visit the detailed CPAN module installation guide.