尝试构建一种优雅的方式来将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网络api快速序列化并使复杂的Moose'ish对象可用,即可以从在客户端网络浏览器中运行的JavaScript进行访问。

谢谢!

最佳答案

我决定自己动手。我在这里发布我的代码是为了使其他可能需要快速简便的解决方案的人受益。

为了获得最佳结果,请将此角色添加到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提供了导致该解决方案的 build 性意见,并感谢@nothingmuch提供了单独的帮助和最终代码审查。

关于json - 如何序列化Moose对象,包括嵌套的Moose对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49502456/

10-12 04:05