fix missing v1.15
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize / JSON.pm
CommitLineData
e601adda 1package Catalyst::Action::Serialize::JSON;
2
930013e6 3use Moose;
4use namespace::autoclean;
e601adda 5
930013e6 6extends 'Catalyst::Action';
14cbe8f1 7use JSON ();
e601adda 8
cc188065 9# VERSION
f465980c 10
14cbe8f1 11has encoder => (
12 is => 'ro',
13 lazy_build => 1,
14);
15
16sub _build_encoder {
17 my $self = shift;
18 return JSON->new->utf8->convert_blessed;
19}
20
e601adda 21sub execute {
22 my $self = shift;
23 my ( $controller, $c ) = @_;
24
faf5c20b 25 my $stash_key = (
07682cbc 26 $controller->{'serialize'} ?
27 $controller->{'serialize'}->{'stash_key'} :
14cbe8f1 28 $controller->{'stash_key'}
faf5c20b 29 ) || 'rest';
7f36b63e 30 my $output = $self->serialize( $c->stash->{$stash_key} );
e601adda 31 $c->response->output( $output );
32 return 1;
33}
34
485b5160 35sub serialize {
546f2871 36 my $self = shift;
14cbe8f1 37 my $data = shift;
38 $self->encoder->encode( $data );
546f2871 39}
40
24748286 41__PACKAGE__->meta->make_immutable;
42
e601adda 431;