1 package Catalyst::Action::Deserialize::JSON;
4 use namespace::autoclean;
5 use Scalar::Util qw(openhandle);
7 $ENV{'PERL_JSON_BACKEND'} = 2; # Always use compiled JSON::XS
10 extends 'Catalyst::Action';
13 our $VERSION = '1.00';
14 $VERSION = eval $VERSION;
18 my ( $controller, $c, $test ) = @_;
22 # could be a string or a FH
23 if ( my $body = $c->request->body ) {
24 if(openhandle $body) {
25 seek($body, 0, 0); # in case something has already read from it
26 while ( defined( my $line = <$body> ) ) {
35 my $json = JSON->new->utf8;
36 if (my $options = $controller->{json_options}) {
37 foreach my $opt (keys %$options) {
38 $json->$opt( $options->{$opt} );
41 my $rdata = eval { $json->decode( $rbody ) };
45 $c->request->data($rdata);
48 'I would have deserialized, but there was nothing in the body!')
54 __PACKAGE__->meta->make_immutable;