::JSON::PerRequest;
use Moo;
use CatalystX::InjectComponent;
use Catalyst::View::JSON::_PerRequest;
our $VERSION = 0.009;
our $DEFAULT_JSON_CLASS = 'JSON::MaybeXS';
our $DEFAULT_VIEW_MODEL = 'JSON::
ViewData';
our %JSON_INIT_ARGS = (
utf8 => 1,
convert_blessed => 1);
extends 'Catalyst::View';
with 'Catalyst::Component::InstancePerContext';
has json => (
is=>'ro',
required=>1,
init_arg
$self = shift;
eval "use ${\$self->json_class}; 1" ||
die "Can't use ${\$self->json_class}, $@";
return $self->json_class->new(
$self->json_init_args);
});
sub HANDLE_ENCODE_E
package Catalyst::Model::JSON::ViewData;
use Moo;
extends 'Catalyst::Model';
with 'Catalyst::Component::InstancePerContext';
with 'Data::Perl::Role::Collection::Hash';
sub build_per_context_insta
nce {
my ($self, $c, %args) = @_;
return $self->new(%args);
}
sub TO_JSON { +{shift->elements} }
sub AUTOLOAD {
my ($self, @args) = @_;
my $key = our $AUTOLOAD;
$key =~ s/.*:://;
retur
@args)
: $self->get($key);
}
1;
=head1 NAME
Catalyst::Model::JSON::ViewData - Default model for Catalyst::View::JSON::PerRequest
=head1 SYNOPSIS
sub root :Chained(/) CaptureArgs(0) {
package Catalyst::View::JSON::_PerRequest;
use HTTP::Status;
use Scalar::Util;
sub data {
my ($self, $data) = @_;
if($data) {
if($self->{data}) {
die "Can't set view data attribute if
a) unless ref $data;
#die "Model $data does not do a required method 'TO_JSON'"
# unless $data->can('TO_JSON');
return $self->{data} = $data;
}
} else {
return $self->{da
self->{ctx}->stats->profile(begin => "=> JSON->send". ($status ? "($status)": ''))
if $self->{ctx}->debug;
my $res = $self->{ctx}->response;
my $json = $self->render($self->data);
$res->h