|
NAMEMojo::UserAgent::Mockable::Serializer - A class that serializes Mojo transactions created by Mojo::UserAgent::Mockable. VERSIONversion 1.59 SYNOPSIS # This module is not intended to be used directly. Synopsis here is given to show how
# Mojo::UserAgent::Mockable uses the module to record transactions.
use Mojo::UserAgent::Mockable::Serializer;
use Mojo::UserAgent;
use File::Slurper qw(read_text write_text);
my $ua = Mojo::UserAgent->new;
my $serializer = Mojo::UserAgent::Mockable::Serializer->new;
my @transactions;
push @transactions, $ua->get('http://example.com');
push @transactions, $ua->get('http://example.com/object/123');
push @transactions, $ua->get('http://example.com/subobject/456');
my $json = $serializer->serialize(@transactions);
write_text('/path/to/file/json', $json);
# OR
$serializer->store('/path/to/file.json', @transactions);
# Later...
my $json = read_text('/path/to/file.json');
my @reconstituted_transactions = $serializer->deserialize($json);
# OR
#
my @reconstituted_transactions = Mojo::UserAgent::Mockable::Serializer->retrieve('/path/to/file.json');
METHODSserializeSerialize or freeze one or more instances of Mojo::Transaction. Takes an array of transactions to be serialized as the single argument. This method will generate a warning if the instance has any subscribers (see "on" in Mojo::EventEmitter). Suppress this warning with (e.g.): no warnings 'Mojo::UserAgent::Mock::Serializer'; $serializer->serialize(@transactions); use warnings 'Mojo::UserAgent::Mock::Serializer'; deserializeDeserialize or thaw a previously serialized array of <Mojo:Transaction>. Arguments:
storeSerialize an instance of Mojo::Transaction and write it to the given file or file handle. Takes two arguments:
retrieveRead from the specified file or file handle and deserialize one or more instances of Mojo::Transaction from the data read. If a file handle is passed, data will be read until an EOF is received. Arguments:
EVENTSThis module emits the following events: pre_thaw $serializer->on( pre_thaw => sub {
my ($serializer, $slush) = @_;
...
});
Emitted immediately before transactions are deserialized. See "DATA STRUCTURE" below for details of the format of $slush. post_thaw # Note that $transactions is an arrayref here.
$serializer->on( post_thaw => sub {
my ($serializer, $transactions, $slush) = @_;
...
}
Emitted immediately after transactions are deserialized. See "DATA STRUCTURE" below for details of the format of $slush. In addition, each transaction, as well as each message therein, serialized using this module will emit the following events: pre_freeze $transaction->on(freeze => sub {
my $tx = shift;
...
});
Emitted immediately before the transaction is serialized. post_freezeEmitted immediately after the transaction is serialized. See "Messages" for details of the frozen format. $transaction->on(post_freeze => sub {
my $tx = shift;
my $frozen = shift;
...
});
DATA STRUCTUREserialize produces, and deserialize expects, JSON data. Transactions are stored as an array of JSON objects (i.e. hashes). Each transaction object has the keys:
MessagesIndividual messages are stored as JSON objects (i.e. hashes) with the keys:
CAVEATSThis module does not serialize any event listeners. This is unlikely to change in future releases. AUTHORKit Peters <popefelix@cpan.org> COPYRIGHT AND LICENSEThis software is copyright (c) 2022 by Kit Peters. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
|