Cherry pick everything bar the use parent change from 25d49c2, fixing RT#46680
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize / JSON.pm
CommitLineData
e601adda 1package Catalyst::Action::Serialize::JSON;
2
3use strict;
4use warnings;
5
6use base 'Catalyst::Action';
ebba5325 7use JSON qw(encode_json);
e601adda 8
9sub execute {
10 my $self = shift;
11 my ( $controller, $c ) = @_;
12
faf5c20b 13 my $stash_key = (
07682cbc 14 $controller->{'serialize'} ?
15 $controller->{'serialize'}->{'stash_key'} :
16 $controller->{'stash_key'}
faf5c20b 17 ) || 'rest';
e601adda 18 my $output;
19 eval {
485b5160 20 $output = $self->serialize( $c->stash->{$stash_key} );
e601adda 21 };
22 if ($@) {
23 return $@;
24 }
25 $c->response->output( $output );
26 return 1;
27}
28
485b5160 29sub serialize {
546f2871 30 my $self = shift;
e9073a60 31 encode_json( shift );
546f2871 32}
33
e601adda 341;