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.20';
3 2     2   10 use Moose;
  2         5  
  2         17  
4 2     2   13548 use namespace::autoclean;
  2         5  
  2         25  
5 2     2   150 use Scalar::Util qw(openhandle);
  2         5  
  2         135  
6              
7             extends 'Catalyst::Action';
8 2     2   1690 use JSON::MaybeXS qw(JSON);
  2         23303  
  2         868  
9              
10             sub execute {
11 6     6 1 12 my $self = shift;
12 6         14 my ( $controller, $c, $test ) = @_;
13              
14 6         8 my $rbody;
15              
16             # could be a string or a FH
17 6 100       173 if ( my $body = $c->request->body ) {
18 5 50       666 if(openhandle $body) {
19 5         45 seek($body, 0, 0); # in case something has already read from it
20 5         68 while ( defined( my $line = <$body> ) ) {
21 5         25 $rbody .= $line;
22             }
23             } else {
24 0         0 $rbody = $body;
25             }
26             }
27              
28 6 100       164 if ( $rbody ) {
29 5         23 my $json = JSON->new->utf8;
30 5 100       72 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         8 my $rdata = eval { $json->decode( $rbody ) };
  5         75  
36 5 100       18 if ($@) {
37 2         13 return $@;
38             }
39 3         87 $c->request->data($rdata);
40             } else {
41 1 50       7 $c->log->debug(
42             'I would have deserialized, but there was nothing in the body!')
43             if $c->debug;
44             }
45 4         17 return 1;
46             }
47              
48             __PACKAGE__->meta->make_immutable;
49              
50             1;