NAME
Data::FlexSerializer - (De-)serialization from/to (compressed) JSON or Storable
SYNOPSIS
use Data::FlexSerializer;
# Strictly handling compressed JSON by default:
my $strict_serializer = Data::FlexSerializer->new;
my @blobs = $strict_serializer->serialize(@perl_datastructures);
my @perl_datastructures = $strict_serializer->deserialize(@blobs);
my $lax_compress = Data::FlexSerializer->new(
detect_compression => 1, # accept uncompressed JSON as well
);
my $weak_compress = Data::FlexSerializer->new(
compression_level => 1, # use fastest compression
);
my $backcompat = Data::FlexSerializer->new(
detect_compression => 1, # check whether the input is compressed
detect_storable => 1, # accept Storable images as input
);
my $no_compression = Data::FlexSerializer->new(
compress_output => 0,
);
DESCRIPTION
This simple OO module implements a serializer/deserializer for the basic builtin Perl data structures (no blessed structures, no filehandles, no regexes, no self-referential structures). It can produce JSON or Storable images. The output can be zlib-compressed or uncompressed depending on settings.
The deserialization phase is more powerful: Depending on the serializer settings, it can be strict in only accepting compressed or uncompressed JSON, or it can auto-detect zlib compression. Additionally, since the purpose of this is to allow painless migration away from storing Storable images persistently, the deserialization code will optionally detect that the input data is a (compressed or uncompressed) Storable image and handle it gracefully. This flexibility comes at a price in performance, so in order to keep the impact low, the default options are more restrictive, see below.
METHODS
new
Constructor. Takes named arguments.
assume_compression
is a flag that makes the deserialization assume that the data will be compressed. It won't have to guess, making the deserialization faster. Defaults to true.
detect_compression
is a flag that also affects only the deserialization step. If set, it'll auto-detect whether the input is compressed. Mutually exclusive with assume_compression
. If detect_compression
is set, but assume_compression
is not explicitly specified, assume_compression
will be disabled (where it otherwise defaults to true).
compress_output
is a flag indicating whether compressed or uncompressed dumps are to be generated during the serialization. Defaults to true.
compression_level
is an integer indicating the compression level (0-9).
output_format
can be either set to the string json
(default) or storable
. It has the obvious effect. Its value can be changed at runtime via the accessor to facilitate having certain output formats in experiments.
serialize
Given a list of things to serialize, this does the job on each of them and returns a list of serialized blobs.
In scalar context, this will return a single serialized blob instead of a list. If called in scalar context, but passed a list of things to serialize, this will croak because the call makes no sense.
deserialize
The opposite of serialize
, doh.
deserialize_from_file
Given a (single!) file name, reads the file contents and deserializes them. Returns the resulting Perl data structure.
Since this works on one file at a time, this doesn't return a list of data structures like deserialize()
does.
serialize_to_file
$serializer->serialize_to_file(
$data_structure => '/tmp/foo/bar'
);
Given a (single!) Perl data structure, and a (single!) file name, serializes the data structure and writes the result to the given file. Returns true on success, dies on failure.
AUTHOR
Steffen Mueller <smueller@cpan.org>
Ævar Arnfjörð Bjarmason <avar@cpan.org>
Burak Gürsoy <burak@cpan.org>
Elizabeth Matthijsen <liz@dijkmat.nl>
ACKNOWLEDGMENT
This module was originally developed at and for booking.com. With approval from booking.com, this module was generalized and put on CPAN, for which the authors would like to express their gratitude.
COPYRIGHT AND LICENSE
(C) 2011, 2012 Steffen Mueller and others. All rights reserved.
This code is available under the same license as Perl version
5.8.1 or higher.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 341:
Non-ASCII character seen before =encoding in 'Ævar'. Assuming UTF-8