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

时间:2018-10-22 07:06:00

标签: json perl resultset catalyst dbix-class

我正在尝试从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;

根据此article足以对祝福对象进行编码:

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

我在这里想念东西吗?

0 个答案:

没有答案