Unblessing Perl 对象并为 convert_blessed 构造 TO_JSON 方法 [英] Unblessing Perl objects and constructing the TO_JSON method for convert_blessed
问题描述
在 这个答案中,我找到了一个简单的 TO_JSON
方法的建议,这是需要的用于将祝福对象序列化为 JSON.
In this answer I found a recommendation for a simple TO_JSON
method, which is needed for serializing blessed objects to JSON.
sub TO_JSON { return { %{ shift() } }; }
谁能详细解释一下它是如何工作的?
Could anybody please explain in detail how it works?
我改成:
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 代码.;(
Many questions… :( Simply, I’m unable to understand 3-liner Perl code. ;(
我需要 TO_JSON
但它会过滤掉:
I need the TO_JSON
but it will filter out:
- 不需要的属性和
- 也取消设置属性(例如对于那些
has_${attr}
谓词返回 false)
- unwanted attributes and
- unset attributes too (e.g. for those the
has_${attr}
predicate returns false)
这是我的代码 - 它有效,但我真的不明白为什么不祝福有效......
This is my code – it works but I really don't understand why the unblessing works…
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
(获取引用指向的散列),但是为什么从 blessed对象引用$self
?- 换句话说,为什么
$self
是一个 hashref? 我试图制作一个类似于@{$self}{ grep {!/xx/} keys %$self}
的哈希切片,但它没有用.因此我创建了那个可怕的TO_JSON
.- 如果
$self
是 hashref,为什么keys %$self
只返回具有值的属性,而不是所有声明的属性(例如nn
也是 - 看到has
)?
- The
%{ $hRef }
derefences the$hRef
(getting the hash pointed to by the reference), but why get a plain hash from a blessed object reference$self
? - In other words, why the
$self
is a hashref? I tried to make a hash slice like@{$self}{ grep {!/xx/} keys %$self}
but it didn't work. Therefore I created that horribleTO_JSON
.- If the
$self
is a hashref, why thekeys %$self
returns only attributes having a value, and not all declared attributes (e.g. thenn
too – see thehas
)?
推荐答案
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
(不复制).
In your TO_JSON()
function { %h }
returns a shallow hash copy, while \%h
returns a reference to %h
(no copying).
这篇关于Unblessing Perl 对象并为 convert_blessed 构造 TO_JSON 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!