+ Add rest_serializer_json_options config key useable to set options
+ like relaxed => 1 to be passed to the JSON serializer (Ton Voon)
Make Data::Dumper unserializer safer by using a Safe compartment (Ton Voon)
use namespace::autoclean;
extends 'Catalyst::Action';
-use JSON qw( decode_json );
+use JSON;
our $VERSION = '0.85';
$VERSION = eval $VERSION;
}
if ( $rbody ) {
- my $rdata = eval { decode_json( $rbody ) };
+ my $json = JSON->new->utf8;
+ if (my $options = $controller->{json_options}) {
+ foreach my $opt (keys %$options) {
+ $json->$opt( $options->{$opt} );
+ }
+ }
+ my $rdata = eval { $json->decode( $rbody ) };
if ($@) {
return $@;
}
L<JSON::XS> installed. The C<text/x-json> content type is supported but is
deprecated and you will receive warnings in your log.
+You can also add a hash in your controller config to pass options to the json object.
+For instance, to relax permissions when deserializing input, add:
+ __PACKAGE__->config(
+ json_options => { relaxed => 1 }
+ )
+
=item * C<text/javascript> => C<JSONP>
If a callback=? parameter is passed, this returns javascript in the form of: $callback($serializedJSON);
eval 'use JSON 2.12';
plan skip_all => 'Install JSON 2.12 or later to run this test' if ($@);
-plan tests => 9;
+plan tests => 11;
use_ok 'Catalyst::Test', 'Test::Serialize';
is_deeply($mres_post->content, $exp, "POST data matches");
}
+{
+ my $t = Test::Rest->new('content_type' => 'application/json');
+ my $json_data = '{ "sushi":"is good for monkey", }';
+ my $mres_post = request($t->post(url => '/monkey_put', data => $json_data));
+ ok( ! $mres_post->is_success, "Got expected failed status due to invalid JSON" );
+
+ my $relaxed_post = request( $t->post(url => "/monkey_json_put", data => $json_data));
+ ok( $relaxed_post->is_success, "Got success due to setting relaxed JSON input" );
+}
+
1;
--- /dev/null
+package Test::Serialize::Controller::JSON;
+
+use namespace::autoclean;
+use Moose;
+
+BEGIN { extends qw/Catalyst::Controller::REST/ };
+
+__PACKAGE__->config(
+ 'stash_key' => 'rest',
+ 'json_options' => {
+ relaxed => 1,
+ },
+ 'map' => {
+ 'text/x-json' => 'JSON',
+ },
+);
+
+sub monkey_json_put : Path("/monkey_json_put") : ActionClass('Deserialize') {
+ my ( $self, $c ) = @_;
+ if ( ref($c->req->data) eq "HASH" ) {
+ my $out = ($c->req->data->{'sushi'}||'') . ($c->req->data->{'chicken'}||'');
+ utf8::encode($out);
+ $c->res->output( $out );
+ } else {
+ $c->res->output(1);
+ }
+}
+
+1;