Steffen Müller


Data::FlexSerializer - (De-)serialization from/to (compressed) JSON or Storable


  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,


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.



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.

detect_storable, if set, forces Data::FlexSerializer into Storable-compatibility mode. Apart from JSON input, it will also detect whether the provided blob is in valid Storable format. Defaults to off.


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.


The opposite of serialize, doh.


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.


    $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.


Steffen Mueller <>

Ævar Arnfjörð Bjarmason <>

Burak Gürsoy <>

Elizabeth Matthijsen <>

Caio Romão Costa Nascimento <>


This module was originally developed at and for With approval from, this module was generalized and put on CPAN, for which the authors would like to express their gratitude.


 (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