Commit | Line | Data |
---|---|---|
e601adda | 1 | package Catalyst::Action::Deserialize::JSON; |
2 | ||
930013e6 | 3 | use Moose; |
4 | use namespace::autoclean; | |
e601adda | 5 | |
930013e6 | 6 | extends 'Catalyst::Action'; |
ebba5325 | 7 | use JSON qw( decode_json ); |
e601adda | 8 | |
a042dad0 | 9 | our $VERSION = '0.83'; |
f465980c | 10 | $VERSION = eval $VERSION; |
11 | ||
e601adda | 12 | sub 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 | ||
39 | 1; |