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?

Svante
  • 50,694
  • 11
  • 78
  • 122
Ibrahim
  • 1,247
  • 3
  • 13
  • 21
  • 1
    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. – Schwern Nov 15 '10 at 19:47
  • 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. – Ibrahim Nov 15 '10 at 20:22

4 Answers4

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.

Axeman
  • 29,660
  • 2
  • 47
  • 102
  • 1
    Got the syntax :$json->allow_blessed->convert_blessed->encode( $blessed_object ) – Ibrahim Nov 15 '10 at 15:27
  • 5
    Or: to_json($blessed_object,{allow_blessed=>1,convert_blessed=>1}) – Matty K Apr 03 '12 at 02:01
  • 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. – Peter V. Mørch Jun 10 '15 at 08:04
  • 1
    @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. – Axeman Jun 10 '15 at 14:56
  • @Axeman :-) 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](http://search.cpan.org/perldoc?Moose%3a%3aStorage) does. Great! And I'd use [Data::Walk](https://metacpan.org/pod/Data::Walk) with that. – Peter V. Mørch Jun 11 '15 at 15:36
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.

cjm
  • 61,471
  • 9
  • 126
  • 175
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"
}
daxim
  • 39,270
  • 4
  • 65
  • 132
  • 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. – Ibrahim Nov 15 '10 at 15:23
  • @daxim 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) – bob_saginowski Aug 29 '16 at 14:43
  • Subclass JSYNC and change the `info` method to make a distinction between [numbers](http://p3rl.org/Scalar::Util#looks_like_number) and non-numbers when encountering a Perl `scalar` type. – daxim Aug 29 '16 at 15:32
2

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

Ether
  • 53,118
  • 13
  • 86
  • 159
  • 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. – Ibrahim Nov 15 '10 at 20:18
  • @Ibrahim: Moose is compatible with perl5.8.8; I use it with that version of Perl in my own production environments. – Ether Nov 16 '10 at 01:12