File Coverage

lib/Catalyst/Action/Deserialize/JSON.pm
Criterion Covered Total %
statement 32 33 96.9
branch 10 12 83.3
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 48 51 94.1


line stmt bran cond sub pod time code
1             package Catalyst::Action::Deserialize::JSON;
2             $Catalyst::Action::Deserialize::JSON::VERSION = '1.19';
3 2     2   10 use Moose;
  2         2  
  2         17  
4 2     2   10612 use namespace::autoclean;
  2         4  
  2         19  
5 2     2   143 use Scalar::Util qw(openhandle);
  2         2  
  2         143  
6              
7             extends 'Catalyst::Action';
8 2     2   534 use JSON::MaybeXS qw(JSON);
  2         2702  
  2         579  
9              
10             sub execute {
11 6     6 1 10 my $self = shift;
12 6         14 my ( $controller, $c, $test ) = @_;
13              
14 6         9 my $rbody;
15              
16             # could be a string or a FH
17 6 100       150 if ( my $body = $c->request->body ) {
18 5 50       583 if(openhandle $body) {
19 5         50 seek($body, 0, 0); # in case something has already read from it
20 5         84 while ( defined( my $line = <$body> ) ) {
21 5         24 $rbody .= $line;
22             }
23             } else {
24 0         0 $rbody = $body;
25             }
26             }
27              
28 6 100       126 if ( $rbody ) {
29 5         30 my $json = JSON->new->utf8;
30 5 100       89 if (my $options = $controller->{json_options}) {
31 1         4 foreach my $opt (keys %$options) {
32 1         6 $json->$opt( $options->{$opt} );
33             }
34             }
35 5         9 my $rdata = eval { $json->decode( $rbody ) };
  5         78  
36 5 100       15 if ($@) {
37 2         18 return $@;
38             }
39 3         69 $c->request->data($rdata);
40             } else {
41 1 50       6 $c->log->debug(
42             'I would have deserialized, but there was nothing in the body!')
43             if $c->debug;
44             }
45 4         19 return 1;
46             }
47              
48             __PACKAGE__->meta->make_immutable;
49              
50             1;