NAME

File::Serialize - DWIM file serialization/deserialization

VERSION

version 1.5.1

SYNOPSIS

use File::Serialize { pretty => 1 };

my $data = { foo => 'bar' };

serialize_file '/path/to/file.json' => $data;

...;

$data_copy = deserialize_file '/path/to/file.json';

DESCRIPTION

File::Serialize provides a common, simple interface to file serialization -- you provide the file path, the data to serialized, and the module takes care of the rest. Even the serialization format, unless specified explicitly as part of the options, is detected from the file extension.

IMPORT

File::Serialize imports the three functions serialize_file, deserialize_file and transerialize_file into the current namespace. A default set of options can be set for both by passing a hashref as an argument to the 'use' statement.

use File::Serialize { pretty => 1 };

SUPPORTED SERIALIZERS

File::Serialize will pick the serializer to use based on the extension of the filename or the explicitly given format. If several serializers are registered for the format, the available serializer with the highest precedence number will be used.

OPTIONS

File::Serialize recognizes a set of options that, if applicable, will be passed to the serializer.

FUNCTIONS

serialize_file $path, $data, $options

my $data = { foo => 'bar' };

serialize_file '/path/to/file.json' => $data;

If the $path is '-', the serialized data will be printed to STDOUT. If it a scalar ref, the serialized data will be assigned to that variable.

serialize_file \my $serialized => $data;

print $serialized;

deserialize_file $path, $options

my $data = deserialize_file '/path/to/file.json';

If the $path is '-', the serialized data will be read from STDIN. If it a scalar ref, the serialized data will be read from that variable.

my $json = '{"foo":1}';
my $data = deserialize_file \$json;

transerialize_file $input, @transformation_chain

transerialize_file is a convenient wrapper that allows you to deserialize a file, apply any number of transformations to its content and re-serialize the result.

$input can be a filename, a Path::Tiny object or the raw data structure to be worked on.

transerialize_file 'foo.json' => 'foo.yaml';

# equivalent to
serialize_file 'foo.yaml' => deserialize_file 'foo.json'

Each element of the @transformation_chain can be

ADDING A SERIALIZER

Serializers are added by creating a File::Serialize::Serializer::* class that implement the File::Serialize::Serializer role. See the documentation for the role for more details.

AUTHOR

Yanick Champoux yanick@cpan.org endorse

COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2019, 2017, 2016, 2015 by Yanick Champoux.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.