Sort list of contributors
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / JSON.pm
CommitLineData
e601adda 1package Catalyst::Action::Deserialize::JSON;
2
930013e6 3use Moose;
4use namespace::autoclean;
e601adda 5
930013e6 6extends 'Catalyst::Action';
ebba5325 7use JSON qw( decode_json );
e601adda 8
f465980c 9our $VERSION = '0.81';
10$VERSION = eval $VERSION;
11
e601adda 12sub execute {
13 my $self = shift;
14 my ( $controller, $c, $test ) = @_;
15
16 my $body = $c->request->body;
faf5c20b 17 my $rbody;
18
e601adda 19 if ($body) {
e601adda 20 while (my $line = <$body>) {
21 $rbody .= $line;
22 }
faf5c20b 23 }
24
25 if ( $rbody ) {
ebba5325 26 my $rdata = eval { decode_json( $rbody ) };
e601adda 27 if ($@) {
28 return $@;
29 }
30 $c->request->data($rdata);
31 } else {
32 $c->log->debug(
faf5c20b 33 'I would have deserialized, but there was nothing in the body!')
34 if $c->debug;
e601adda 35 }
36 return 1;
37}
38
391;