8

这个答案中,我找到了一个简单TO_JSON方法的推荐,这是将祝福对象序列化为 JSON 所必需的。

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

有人可以详细解释它是如何工作的吗?

我将其更改为:

sub TO_JSON {
        my $self = shift;         # the object itself – blessed ref
        print STDERR Dumper $self;

        my %h = %{ $self };       # Somehow unblesses $self. WHY???
        print STDERR Dumper \%h;  # same as $self, only unblessed

        return { %h };    # Returns a hashref that contains a hash.
        #return \%h;      # Why not this? Works too…
}

很多问题...... :( 简单地说,我无法理解 3 行 Perl 代码。;(

我需要,TO_JSON但它会过滤掉:

  • 不需要的属性和
  • 也未设置属性(例如,对于那些has_${attr}谓词返回 false 的属性)

这是我的代码——它有效,但我真的不明白为什么 unblessing 有效……</p>

use 5.010;
use warnings;
use Data::Dumper;

package Some;
use Moo;

has $_ => ( is => 'rw', predicate => 1,) for (qw(a1 a2 nn xx));

sub TO_JSON {
    my $self = shift;
    my $href;
    $href->{$_} = $self->$_ for( grep {!/xx/} keys %$self );
    # Same mysterious unblessing. The `keys` automagically filters out
    # “unset” attributes without the need of call of the has_${attr}
    # predicate… WHY?
    return $href;
}

package main;
use JSON;
use Data::Dumper;

my @objs = map { Some->new(a1 => "a1-$_", a2 => "a2-$_", xx=>"xx-$_") } (1..2);
my $data = {arr => \@objs};
#say Dumper $data;
say JSON->new->allow_blessed->convert_blessed->utf8->pretty->encode($data);

编辑:澄清问题:

  • %{ $hRef }解除引用$hRef(获取引用指向的哈希),但为什么要从受祝福的对象引用中获取普通哈希 $self
  • 换句话说,为什么$self是 hashref?
  • 我试图制作一个像这样的哈希切片,@{$self}{ grep {!/xx/} keys %$self}但它没有用。因此我创造了那个可怕TO_JSON的 .
  • 如果$self是 hashref,为什么keys %$self只返回具有值的属性,而不是所有声明的属性(例如 the nntoo - 参见has)?
4

2 回答 2

8
sub TO_JSON { return { %{ shift() } }; }
                     | |  |
                     | |  L_ 1. pull first parameter from `@_`
                     | |        (hashref/blessed or not)
                     | |     
                     | L____ 2. dereference hash (returns key/value list)
                     |
                     L______ 3. return hashref assembled out of list

在您的TO_JSON()函数{ %h }中返回一个浅散列副本,同时\%h返回一个引用%h(不复制)。

于 2014-08-26T14:36:12.430 回答
5

Perl 通过简单地让引用知道它来自哪个包(使用bless)来实现面向对象。知道引用来自Foo包意味着方法实际上是该包中定义的函数。

Perl 允许任何类型的引用得到祝福;不仅仅是哈希引用。祝福哈希引用是很常见的;很多文档都表明正是这样做的;并Moose做到了;但是,可以为数组引用、子例程引用、文件句柄或对标量的引用提供祝福。该语法%{$self}仅适用于哈希引用(祝福与否)。它采用散列引用,并将其作为散列解引用。原始参考可能已被祝福的事实已经丢失。

我需要 TO_JSON 但会过滤掉什么:

  • 不需要的属性
  • 和未设置的属性(例如,对于那些 has_${attr} 谓词返回 false。

在 5.20 之前,散列切片只为您提供值而不是原始散列中的键。您需要键和值。

假设您有一个哈希,并且想要过滤掉undef不在白名单上的值和键,有几个选项。这是我所拥有的,使用该JSON模块:

use strict; # well, I used "use v5.18", but I don't know which version of Perl you're using
use warnings;
use JSON;

my $foo = { foo => undef, bar => 'baz', quux => 5 };
my %whitelist = map { $_, 1 } qw{foo bar};

my %bar = map { $_ => $foo->{$_} }
          grep { defined $foo->{$_} && exists $whitelist{$_} }
          keys %$foo;
print to_json(\%bar) . "\n";
# well, I used say() instead of print(), but I don't know which version of Perl you're using

maps 和greps 不一定很漂亮,但这是我能想到的最简单的方法来过滤掉不在白名单上的键和没有值的元素undef

您可以使用数组切片:

use strict;
use warnings;
use JSON;

my $foo = { foo => undef, bar => 'baz', quux => 5 };
my @whitelist = qw{foo bar};

my %filtered_on_keys;
@filtered_on_keys{@whitelist} = @$foo{@whitelist};

my %bar = map { $_ => $filtered_on_keys{$_} }
          grep { defined $filtered_on_keys{$_} }
          keys %filtered_on_keys;
print to_json(\%bar) . "\n";

或者如果你喜欢循环:

use strict;
use warnings;
use JSON;

my $foo = { foo => undef, bar => 'baz', quux => 5 };
my %whitelist = map { $_ => 1 } qw{foo bar};

my %bar;
while (my ($key, $value) = each %$foo) {
    if (defined $value && exists $whitelist{$key}) {
       $bar{$key} = $value;
    }
}

print to_json(\%bar) . "\n";

现在似乎是引用 Larry wall 的名言的好时机,“Perl 旨在为您提供多种方法来做任何事情,因此请考虑选择最易读的一种。”

但是,我强调了并非所有对象都是哈希值。从对象获取数据的适当方法是通过它的 getter 函数:

use strict;
use warnings;
use JSON;

my $foo = Foo->new({ foo => undef, bar => 'baz', quux => 5 }); # as an example

my %filtered_on_keys;
@filtered_on_keys{qw{foo bar}} = ($foo->get_foo(), $foo->get_bar());

my %bar = map { $_ => $filtered_on_keys{$_} }
          grep { defined $filtered_on_keys{$_} }
          keys %filtered_on_keys;
print to_json(\%bar) . "\n";
于 2014-08-26T19:37:39.830 回答