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