File::Serialize - DWIM file serialization/deserialization
version 1.2.0
use File::Serialize { pretty => 1 }; my $data = { foo => 'bar' }; serialize_file '/path/to/file.json' => $data; ...; $data_copy = deserialize_file '/path/to/file.json';
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.
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.
serialize_file
deserialize_file
transerialize_file
use File::Serialize { pretty => 1 };
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.
format
File::Serialize::Serialize::YAML::Tiny
File::Serialize::Serializer::JSON::MaybeXS
File::Serialize::Serializer::TOML
File::Serialize::Serializer::XML::Simple
File::Serialize::Serializer::JSONY
File::Serialize recognizes a set of options that, if applicable, will be passed to the serializer.
Explicitly provides the serializer to use.
my $data = deserialize_file $path, { format => 'json' };
If true, the canonical extension of the serializing format will be appended to the file. Requires the parameter format to be given as well.
# will create 'foo.yml', 'foo.json' and 'foo.toml' serialize_file 'foo', $data, { format => $_, add_extension => 1 } for qw/ yaml json toml /;
The serialization will be formatted for human consumption.
Serializes the data using its canonical representation.
If set to a true value, file will be read/written out using Path::Tiny's slurp_utf8 and spew_utf8 method ( which sets a binmode of :encoding(UTF-8)). Otherwise, Path::Tiny's slurp and spew methods are used.
true
slurp_utf8
spew_utf8
binmode
:encoding(UTF-8)
slurp
spew
Defaults to being true because, after all, this is the twenty-first century.
If set to true, allow to serialize non-ref data.
Defaults to true.
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.
$path
-
serialize_file \my $serialized => $data; print $serialized;
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 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.
$input
transerialize_file 'foo.json' => 'foo.yaml'; # equivalent to serialize_file 'foo.yaml' => deserialize_file 'foo.json'
Each element of the @transformation_chain can be
@transformation_chain
A transformation step. The current data is available both via $_ and as the first argument to the sub, and the transformed data is going to be whatever the sub returns.
$_
my $data = { tshirt => { price => 18 }, hoodie => { price => 50 }, }; transerialize_file $data => sub { my %inventory = %$_; +{ %inventory{ grep { $inventory{$_}{price} <= 20 } keys %inventory } } } => 'inexpensive.json'; # chaining transforms transerialize_file $data => sub { my %inventory = %$_; +{ map { $_ => $inventory{$_}{price} } keys %inventory } } => sub { my %inventory = %$_; +{ %inventory{ grep { $inventory{$_} <= 20 } keys %inventory } } } => 'inexpensive.json'; # same as above, but with Perl 5.20 signatures and List::Util pair* # helpers transerialize_file $data => sub($inventory) { +{ pairmap { $a => $b->{price} } %$inventory } } => sub($inventory) { +{ pairgrep { $b <= 20 } %$inventory } } => 'inexpensive.json';
A hashref of destination file with their options. The current state of the data will be serialized to those destination. If no options need to be passed, the value can be undef.
undef
transerialize_file $data => { 'beginning.json' => { pretty => 1 }, 'beginning.yml' => undef } => sub { ... } => { 'end.json' => { pretty => 1 }, 'end.yml' => undef };
Run the subchains given in @branches on the current data. Must be the last step of the chain.
@branches
my @data = 1..10; transerialize_file \@data => { 'all.json' => undef } => [ [ sub { [ grep { $_ % 2 } @$_ ] } => 'odd.json' ], [ sub { [ grep { not $_ % 2 } @$_ ] } => 'even.json' ], ];
Has to be the final step(s) of the chain. Just like the arguments of serialize_file. $filename can be a string or a Path::Tiny object. $options is optional.
$filename
$options
Has to be the final step of the chain. Will assign the transformed data to $result instead of serializing to a file.
$result
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.
Yanick Champoux <yanick@cpan.org>
This software is copyright (c) 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.
To install File::Serialize, copy and paste the appropriate command in to your terminal.
cpanm
cpanm File::Serialize
CPAN shell
perl -MCPAN -e shell install File::Serialize
For more information on module installation, please visit the detailed CPAN module installation guide.