Help us understand the problem. What is going on with this article?

JSONからPerlオブジェクトを生成する方法

More than 3 years have passed since last update.

まさにこれ。
How to convert Perl objects into JSON and vice versa - Stack Overflow(PerlオブジェクトをJSONに変換するにはどうすれば?またその逆は?)

要点

  • Blessed Objectはそのままencodeしようとするとエラーになるよ

 encountered object 'Person=HASH(0x11d6d40)', but neither allow_blessed nor convert_blessed settings are enabled at sample.pl line 24.

  • allow_blessedが有効だと例外エラーしなくなるよ。代わりにnullが返るよ。
  • convert_blessedを有効にするとBlessed ObjectのTO_JSONが呼ばれてそれを使うよ。
  • sub TO_JSON { return { %{ shift() } }; }という風に書いてハッシュに変換するといいよ。
  • でもこれだと何のObjectかわからないから、__CLASS__フィールドを用意しておくといいよ。
  • JSONをインスタンスに変換するのはPerlに限らず、他の言語でも必要だよ。

実例

#!/usr/bin/perl

package Person {
    use v5.10;
    use warnings;
    use Moo;

    has 'num' => (is => 'rw', default => 22);
    has 'hash' => (is => 'rw', default => sub { { a => 22} } );
    has '__CLASS__' => (is => 'ro', default => __PACKAGE__);

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

use v5.10;
use warnings;
use Data::Dumper;
use JSON;

# Objectを生成
my $obj = Person->new();
say Dumper $obj;

# JSONに変換
my $json_text = JSON->new->utf8->allow_blessed->convert_blessed->encode($obj);
#my $json_text = JSON->new->utf8->allow_blessed->encode($obj);
die "return null" if $json_text eq "null";
say $json_text;
# ↑convert_blessedを付けない場合nullが返る

# JSONからObjectを生成
my $ref = JSON->new->decode($json_text);
say Dumper $ref;
my $obj2 = bless($ref, $ref->{__CLASS__});
say Dumper $obj2;
__END__
$VAR1 = bless( {
                 'hash' => {
                             'a' => 22
                           },
                 '__CLASS__' => 'Person',
                 'num' => '22'
               }, 'Person' );

{"__CLASS__":"Person","hash":{"a":22},"num":"22"}
$VAR1 = {
          'hash' => {
                      'a' => 22
                    },
          '__CLASS__' => 'Person',
          'num' => '22'
        };

$VAR1 = bless( {
                 'hash' => {
                             'a' => 22
                           },
                 '__CLASS__' => 'Person',
                 'num' => '22'
               }, 'Person' );

参考

moose - Unblessing Perl objects and constructing the TO_JSON method for convert_blessed - Stack Overflow https://stackoverflow.com/questions/25508197/unblessing-perl-objects-and-constructing-the-to-json-method-for-convert-blessed

blessされたオブジェクトをJSONに変換 - hounobouno http://koizuss.hatenablog.com/entry/20090605/1244174060

How to convert Perl objects into JSON and vice versa - Stack Overflow https://stackoverflow.com/questions/4185482/how-to-convert-perl-objects-into-json-and-vice-versa

JSON - JSON (JavaScript Object Notation) encoder/decoder - metacpan.org https://metacpan.org/pod/JSON

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away