Ibrahim Ibrahim - 4 months ago 97
JSON Question

How to convert Perl objects into JSON and vice versa

I have defined a Point object in a file

Point.pm
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};
}

1;


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

use JSON;
use Point;

Point $p = new Point;
$p->X(20);
$p->Y(30);

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 test.pl line 28


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

Answer

Well the warning tells you what is wrong. JSON does not deal with blessed references (i.e. objects) unless you tell it what to do:

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.

Comments