++ed by:

1 PAUSE user

Al Newkirk
and 1 contributors


Object::Serializer - General Purpose Object Serializer


version 0.000011


    package Point;

    use Moo;
    use parent 'Object::Serializer';

    has 'x' => (is => 'rw');
    has 'y' => (is => 'rw');

    package main;

    my $point = Point->new(x => 10, y => 10);

    # serialize the class instance into a hash
    my $hash = $point->serialize; # { __CLASS__ => 'Point', x => 10, y => 10 }


Getting objects into an ideal format for passing representations in and out of applications can be a real pain. Object::Serializer is a fast and simple pure-perl framework-agnostic type-less none-opinionated light-weight primitive general purpose object serializer which tries to help make object serialization easier. This module is useful in situations when you have blessed objects you wish to produce hash representations from which you can store directly or convert to JSON, YAML, or XML. This module does not currently support deserialization.



The serialize method expects an object and returns a serialized (hashified) version of that object.

    my $hash = $self->serialize;
    my $hash = $self->serialize($object);
    my $hash = $self->serialize($object, marker => undef); # no marker


The serialization_strategy_for method expects a reftype and a sub-routine. This method registers a custom serialization strategy which will be used during the collapsing of the reference type specified.

        REFTYPE => sub { ... }


Object::Serializer can be used as a serializer independently, however, it is primarily designed to be used as a base class for your classes or roles. By default, Object::Serializer doesn't do anything special for you in the way of serialization, in-fact, it is little more than a wrapper around Data::Dumper. Additionally, you can hook into the serialization process by defining your serialization strategy using your own custom serialization routines which will be executed whenever a specific reference type is encountered. The following syntax is what you might use to register your own custom serialization strategy. This example registers a custom serializer that is executed globally whenever a DateTime object is found.

        DateTime => sub { pop->iso8601 }

Additionally, you can register a serialization strategy to be used only when invoked by a specific class. The following syntax is what you might use to register a serialization strategy to be executed only for a specific class:

        DateTime => sub { pop->iso8601 }


Circular references are problematic and should be avoided, you can weaken or otherwise handle them yourself then re-assemble them later as a means toward getting around this.


Al Newkirk <anewkirk@ana.io>


This software is copyright (c) 2013 by Al Newkirk.

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