How to convert Perl objects into JSON and vice versa
Asked Answered
K

4

25

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?

Keegan answered 15/11, 2010 at 14:48 Comment(2)
Unless you specifically need JSON, I would suggest using YAML for this task. JSON doesn't have syntax to indicate something is an object. YAML does. You'll have to manually bolt something together or use a JSON language extension like JSYNC. If all you need to do is serialize a Perl object, use Data::Dumper or Storable.Synaesthesia
I was originally developing the solution in XML but I found that JSON might trim the string because It to other scripts using STDIN, however due complexity and unreadability of JSON, I decided to switch back to XML, which is easy to parse.Keegan
B
22

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.

Bullyrag answered 15/11, 2010 at 15:8 Comment(5)
Got the syntax :$json->allow_blessed->convert_blessed->encode( $blessed_object )Keegan
Or: to_json($blessed_object,{allow_blessed=>1,convert_blessed=>1})Richel
This does not cater to the second part of his question: "and vice versa". The hard, missing part is turning JSON to Perl objects after decoding.Obedient
@PeterV.Mørch, that's not hard. Perl can turn many structured data languages into a hash, and you simply bless that hash into the desired class. What I detailed is actually the hard part, because the JSON rules cause an error when you simply try to convert an object to JSON.Bullyrag
@Bullyrag :-) Depends on context, I guess. I'm looking to use JSON as a transparent encoding on a socket to send objects over, and I don't know the type of these objects beforehand. But I see you've since expanded your question considerably in this regard, suggesting using the magic string __CLASS__ just like MooseX::Storage does. Great! And I'd use Data::Walk with that.Obedient
D
5

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.

Dreadfully answered 15/11, 2010 at 15:5 Comment(0)
S
5

You need JSYNC.

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

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

{
   "!" : "!perl/hash:Point",
   "_x" : "20",
   "_y" : "30"
}
Sophisticated answered 15/11, 2010 at 15:20 Comment(3)
This is a very early release of JSYNC, and should not be used at all unless you know what you are doing. I need a production ready module like JSON.Keegan
@Sophisticated Just tested it and noticed a small problem: the values are json stings but we need json numbers. Is there a way to to build these values as numbers (haven't found anything inside the documentation)Lindi
Subclass JSYNC and change the info method to make a distinction between numbers and non-numbers when encountering a Perl scalar type.Sophisticated
A
2

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

Adabelle answered 15/11, 2010 at 16:9 Comment(2)
I like to use Moose, however the server I am running is using older version of Perl 5.8.8. I am afraid that the server is used in production and upgrading Perl will break some of production scripts.Keegan
@Ibrahim: Moose is compatible with perl5.8.8; I use it with that version of Perl in my own production environments.Adabelle

© 2022 - 2024 — McMap. All rights reserved.