尝试构建一种优雅的方法来使用嵌套的Moose对象序列化Moose对象。例如:
package Asset;
use Moose::Role;
has 'value' => (
isa => 'Int'
);
has 'owner' => (
isa => 'Person',
);
sub as_serializable {
...
}
package Car;
use Moose;
with 'Asset'; # role
has 'mileage' => (
isa => 'Int',
);
has 'driver' => (
isa => 'Person',
);
package House;
use Moose;
with 'Asset'; # role
has 'bathrooms' => (
isa => 'Int'
);
package Person;
use Moose;
has 'name' => (
isa => 'Str',
);
has 'favorite_assets' => (
isa => 'ArrayRef[Asset]', # and so on... and just to complicate things a bit...
lazy => 1
);
我想要的是序列化的一些方法,也许是这样:
use JSON;
my $car = Car();
return JSON::encode_json( $car->as_serializable() );
也许as_serializable()
方法包含一些参数,这些参数指定要扩展的属性(和嵌套属性),并且可能存在一些针对循环扩展的保护,正如我在favorite_assets
属性中暗示的那样。
在我开始自己开始之前,这样的事情是否已经存在?我不得不相信某个地方有人面临这个挑战。我已经查看了整个Moose文档并进行了一些搜索,但没有找到任何明显的信息,但是我又一次仍然是新手。
用例是能够通过http web api快速序列化和制作复杂的Moose'ish对象,即可从客户端Web浏览器中运行的JavaScript访问。
谢谢!
答案 0 :(得分:1)
我决定自己动手。我在这里发布我的代码是为了其他可能需要快速和快速的人的利益。简单的解决方案。
为获得最佳效果,请将此角色添加到您的Moose类树中。
=item as_serializable - Converts self to serializable hashref
INPUT: $schema is a nested hashref of attributes to expand or suppress.
This example expands the 'owner' and 'driver' attributes within Car, further expands the 'favorite_assets' attribute within Person, and suppresses mileage:
my $car = Car();
my $car_serializable = $car->as_serializable({
owner => {},
driver => {
favorite_assets => {}
},
mileage => 0
});
OUTPUT: $hashref
RULES:
1. All scalars are expanded by default, unless they're private (name starts with _)
2. DateTime's are stringified and treated as scalars.
3. HashRefs, ArrayRefs, and Moose objects are not expanded by default.
4. To expand a given attribute, set corresponding $schema node to {}, adding sub-attributes to expand as desired.
5. To suppress expansion/building a given attribute, set corresponding $schema node to 0.
6. HashRefs & ArrayRefs are all-or-none in $schema. No option to pick by specific hash-keys or array-elements.
7. Unless specifically suppressed, all attributes are built even if lazy.
8. Any attribute without a value is skipped.
=cut
sub as_serializable {
my ( $self, $schema ) = @_;
return $self->_serialize_value( $self, $schema || {} );
}
sub _serialize_value {
my ( $self, $value, $schema ) = @_;
# scalar
if ( !ref($value) ) {
return $value;
}
# DateTime as scalar
if ( ref($value) eq 'DateTime' ) {
return ''.$value; #stringify
}
# hashref
if ( ref($value) eq 'HASH' ) {
my $h = {};
foreach my $k (keys %{ $value }) {
$h->{ $k } = $self->_serialize_value( $value->{$k}, $schema );
}
return $h;
}
# arrayref
if ( ref($value) eq 'ARRAY' ) {
return [ map { $self->_serialize_value($_, $schema) } @{ $value } ];
}
# Moose object
if ( blessed($value) && $value->can('meta') ) {
my $h = {};
foreach my $attr ( $value->meta->get_all_attributes ) {
my $name = $attr->name;
if ( exists($schema->{ $name }) && !$schema->{ $name } ) { # suppress expansion (including get_value) if $schema->{ $name } is false
next;
}
my $attr_val = $attr->get_value( $value );
if ( !$attr->has_value( $value ) ) { # suppress attributes with no value
next;
}
if ( $schema->{ $name } || ( !($name =~ /^_/) && ( !ref($attr_val) || (ref($attr_val) eq 'DateTime') ) ) ) { # expand non-private scalars + all attributes specified by $schema
$h->{ $name } = $self->_serialize_value( $attr_val, $schema->{ $name } );
}
}
return $h;
}
# if/as needed, add support for other reference types here...
die "unsupported ref='" . ref($value) . "' required by schema";
}
注意我以自己的方式编写序列化/字符串化DateTime,这特定于我一贯构建DateTime对象的方式。您可能需要为项目更改此项。
感谢@simbabque提出了导致这一解决方案的建设性意见,并感谢@nothingmuch提供的单独协助和最终代码审查。