首页 > 解决方案 > 如何将 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})

我在这里错过了什么吗?

标签: jsonperlresultsetcatalystdbix-class

解决方案


推荐阅读