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.