如何将DBIx::Class::ResultSet转换为JSON



>我正在尝试从DBIx::Class::ResultSet获取JSON,但出现异常

encountered object 'Sql2Json::Model::DB::Book=HASH(0x6014f88)', 
but neither allow_blessed, convert_blessed nor allow_tags settings are
enabled (or TO_JSON/FREEZE method missing)

控制器类Books.pm

package Sql2Json::Controller::Books;
use Moose;
use namespace::autoclean;
use JSON::XS;
BEGIN { extends 'Catalyst::Controller'; }
my $json = JSON::XS->new;
sub list : Local {
my($self, $c) = @_;
$c->stash(books_rs => $c->model('DB::Book'));
$c->stash(books => [$c->stash->{books_rs}->search({}, {order_by => 'name ASC'})]);
$c->stash(json_data => $json->convert_blessed->encode($c->stash->{books}));
$c->forward('View::JSON');
}
__PACKAGE__->meta->make_immutable;
1;

根据这篇文章,祝福对象的编码就足够了:

$json->convert_blessed->encode($c->stash->{books})

我在这里错过了一些东西吗?

几乎在所有情况下,最好的方法是使用从查询返回的行的get_inflated_column方法。

$books = $c->model('DB::Book');
$c->stash(json_data => [map {$_->get_inflated_columns} $books->all]);
$c->forward('View::JSON');

相关内容

  • 没有找到相关文章

最新更新