Mojo::UserAgent::Mockable::Serializer - A class that serializes Mojo transactions created by Mojo::UserAgent::Mockable.


version 1.59


    # 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('');
    push @transactions, $ua->get('');
    push @transactions, $ua->get('');

    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');



Serialize 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';
  use warnings 'Mojo::UserAgent::Mock::Serializer';


Deserialize or thaw a previously serialized array of Mojo:Transaction. Arguments:


JSON containing the serialized objects.


Serialize an instance of Mojo::Transaction and write it to the given file or file handle. Takes two arguments:


File or handle to write serialized object to.


Array of Mojo::Transaction to serialize


Read 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:


File containing serialized object


This module emits the following events:


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


    # 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:


    $transaction->on(freeze => sub {
        my $tx = shift;

Emitted immediately before the transaction is serialized.


Emitted 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;


serialize produces, and deserialize expects, JSON data. Transactions are stored as an array of JSON objects (i.e. hashes). Each transaction object has the keys:


The original class of the transaction.


The request portion of the transaction (e.g. "GET /foo/bar ..."). See "Messages" below for encoding details.


The response portion of the transaction (e.g. "200 OK ..."). See "Messages" below for encoding details.


Individual messages are stored as JSON objects (i.e. hashes) with the keys:


The class name of the serialized object. This should be a subclass of Mojo::Message


Array of events with subscribers in the serialized object. These events will be re-emitted after the "thaw" event is emitted, but any subscribers present in the original object will be lost.


The raw HTTP message body.


This module does not serialize any event listeners. This is unlikely to change in future releases.


Kit Peters <>


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