Add new serializer for JSON::XS
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize / JSON.pm
1 #
2 # Catlyst::Action::Serialize::JSON.pm
3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 # Created on: 10/12/2006 03:00:32 PM PDT
5 #
6 # $Id$
7
8 package Catalyst::Action::Serialize::JSON;
9
10 use strict;
11 use warnings;
12
13 use base 'Catalyst::Action';
14 use JSON qw(encode_json);
15
16 sub execute {
17     my $self = shift;
18     my ( $controller, $c ) = @_;
19
20     my $stash_key = (
21             $controller->{'serialize'} ?
22                 $controller->{'serialize'}->{'stash_key'} :
23                 $controller->{'stash_key'} 
24         ) || 'rest';
25     my $output;
26     eval {
27         $output = $self->encode( $c->stash->{$stash_key} );
28     };
29     if ($@) {
30         return $@;
31     }
32     $c->response->output( $output );
33     return 1;
34 }
35
36 sub encode {
37     my $self = shift;
38     encode_json( @_ );
39 }
40
41 1;