How to convert Perl objects into JSON and vice versa

I have defined a Point object in a file as following:

package Point;
sub new {
    my ($class) = @_;
    my $self = {
        _x => 0,
        _y => 0,
    return bless $self => $class;

sub X {
    my ($self, $x) = @_;
    $self->{_x} = $x if defined $x;
    return $self->{_x};

sub Y {
    my ($self, $y) = @_;
    $self->{_y} = $y if defined $y;
    return $self->{_y};


Now when I use JSON to convert the object to JSON by the following code:

use JSON;
use Point;

Point $p = new Point;

my $json = encode_json $p;

I get the following error:

encountered object 'Point=HASH(0x40017288)', but neither allow_blessed nor convert_blessed settings are enabled at line 28

How do I convert to and from JSON to an object using the JSON module?


The warning tells you most of what is wrong. Unless you tell JSON how to handle blessed references(Perl objects), JSON handles only un-blessed data structures.

You can convert_blessed and you can allow_blessed. For allow_blessed, it says:

If $enable is false (the default), then encode will throw an exception when it encounters a blessed object.

Point is an object class, thus an instance of Point is a blessed reference, and thus the default for JSON is to throw an exception.

If you enable convert_blessed, it will call a TO_JSON method on your object. With simple objects like Point (ones that contain no blessed members), you can do that as easily as:

sub TO_JSON { return { %{ shift() } }; }

If you have to descend a structure, it will get a lot hairier.

Somebody in the comments below said that I didn't cover how to get objects out of JSON.

The basics are simple. So here goes

my $object = bless( JSON->new->decode( $json_string ), 'ClassIWant' );

I mainly covered the part that prevents you from simply serializing a blessed object into JSON.

The basics of deserialization are simple, just like the basics of serialization are simple--once you know the trick. There is no error in the way, there is just the task of finding what you need and blessing it into the right class.

If you want to have code coupled to the objects, then you'll know what has to be blessed and what it will have to be blessed into. If you want totally decoupled code, this is no harder or easier in Perl than it is in JavaScript itself.

You're going to have to serialize a marker in the JSON. If I need something like this, I will insert a '__CLASS__' field into the blessed objects. And when deserializing, I will descend through the structure and bless everything like this:

 bless( $ref, delete $ref->{__CLASS__} );

But as I said, this is no easier or harder in Perl, because JSON presents the same challenge to all languages.

As Schwern suggested in his comment up top, YAML is much better built for serializing and deserializing objects, because it has notation for it. JSON gives you associative arrays or arrays.

Did you try reading the JSON documentation on the allow_blessed and convert_blessed options, as suggested by the error message? That should explain how to convert a Perl object to JSON.

Going the other way is harder, as JSON isn't YAML, and wasn't designed to be deserialized into a class-based object system like Perl's. You could experiment with the filter_json_object or filter_json_single_key_object options, or you could post-process the decoded JSON and create the objects yourself.

You need JSYNC.

use JSYNC;
use Point;
my $p = Point->new;

my $jsync = JSYNC::dump($p, {pretty => 1});

   "!" : "!perl/hash:Point",
   "_x" : "20",
   "_y" : "30"

You may find it useful to convert your classes to Moose and use MooseX::Storage to serialize and deserialize them.

Need Your Help

Configuring v8's memory management to be smart for a node.js process

node.js memory-management garbage-collection v8

We run an XMPP server on node.js, on a machine with around 3.8 GB RAM. Here are the command line parameters we pass while invoking node :

Scaling a Phonegap app for different Android screen sizes/densities?

android css cordova scale dpi

I have a Phonegap app designed to run on Android phones and tablets. The scale of text and images looks fine on a phone, but too small on a 7” tablet.