Commit | Line | Data |
---|---|---|
e601adda | 1 | package Catalyst::Action::Serialize::JSON; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use base 'Catalyst::Action'; | |
ebba5325 | 7 | use JSON qw(encode_json); |
e601adda | 8 | |
9 | sub 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 | 29 | sub serialize { |
546f2871 | 30 | my $self = shift; |
e9073a60 | 31 | encode_json( shift ); |
546f2871 | 32 | } |
33 | ||
e601adda | 34 | 1; |